Skip to content

Utilities Reference

view.util

EnvConv = Union[str, int, bool, dict] module-attribute

__all__ = ('run', 'env', 'enable_debug', 'timestamp', 'extract_path') module-attribute

enable_debug()

Enable debug mode.

env(key: str, *, tp: type[EnvConv] = str) -> EnvConv

Get and parse an environment variable.

Parameters:

Name Type Description Default
key str

Environment variable to access.

required
tp type[EnvConv]

Type to convert to.

str
Example
from view import new_app, env

app = new_app()

@app.get("/")
def index():
    return env("FOO")

app.run()

extract_path(path: str) -> App

Extract an App instance from a path.

run(app_or_path: str | App) -> None

Run a view app. Should not be used over App.run() middleware: list[__Middleware],

Parameters:

Name Type Description Default
app_or_path str | App

App object or path to run.

required

timestamp(tm: DateTime | None = _Now) -> str

RFC 1123 Compliant Timestamp

Parameters:

Name Type Description Default
tm datetime | None

Date object to create a timestamp for. Now by default.

_Now

view.typecodes

T = TypeVar('T') module-attribute

__all__ = ('TCValidator', 'compile_type') module-attribute

TCValidator

Bases: TCPublic, Generic[T]

Class for holding a typecode to be validated against.

codes: Iterable[TypeInfo] = codes instance-attribute

tp: type[T] = tp instance-attribute

__init__(tp: type[T], codes: Iterable[TypeInfo]) -> None

cast(obj: object) -> T

Cast an object to the type.

Raises:

Type Description
TypeValidationError

The object is not compatible with the type.

check_type(obj: object) -> TypeGuard[T]

Check if an object is the type.

is_compatible(obj: object) -> bool

Check if an object is compatible with the type (including with casting).

compile_type(tp: type[T]) -> TCValidator[T]

Compile a type to a type validation object.