This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
SENTINEL = object() | |
def run_once(func): | |
"""Decorator which ensures that a function is only executed a single time.""" | |
class Scope: | |
locals = {"result": SENTINEL} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import warnings | |
from importlib import import_module | |
from importlib.metadata import requires | |
from packaging.requirements import Requirement | |
MY_LIB = "my_lib" | |
CURRENT_EXTRA = "my_extra" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Iterable | |
import itertools | |
class Stream: | |
def __init__(self, stream: Iterable): | |
self.stream = stream | |
def __iter__(self): | |
yield from self.stream |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ast | |
import inspect | |
class InitAnnotations(ast.NodeVisitor): | |
def __init__(self, locals_, globals_): | |
self.globals = globals_ | |
self.locals = locals_ | |
self.annotations = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Literal | |
from fastapi import FastAPI, Depends | |
from pydantic import BaseModel | |
import uvicorn | |
app = FastAPI() | |
class Request(BaseModel): | |
data: str |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MY_APP_APP__PASSWORD="secret_foo" | |
MY_APP_DB__PASSWORD="secret_bar" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel | |
Sensitive = ... | |
class Config(BaseModel): | |
user: str | |
password: Sensitive[str] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from pydantic import BaseModel, validator | |
class FirstLevel(BaseModel): | |
bar: "SecondLevel" | |
foo: int | |
@validator("foo") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# adding widgets to my app | |
import my_app | |
def foo(user_id: int, num_widgets: int): | |
user = my_app.get_user(user_id) | |
widgets = [] | |
for _ in range(num_widgets): | |
widgets.append(my_app.create_widget(user)) | |
my_app.bootstrap(widgets) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI | |
from pydantic import BaseModel, BaseConfig | |
app = FastAPI() | |
BaseConfig.arbitrary_types_allowed = True | |
class Foo: | |
def __init__(self, x: int): | |
self.x = x |