API Reference
cwtch.cwtch
is_cwtch_model(cls) -> bool
is_cwtch_view(cls) -> bool
field(default: Any = _MISSING, *, default_factory: _Missing[Callable] = _MISSING, init: bool = True, init_alias: Unset[str] = UNSET, asdict_alias: Unset[str] = UNSET, repr: Unset[Literal[False]] = UNSET, compare: Unset[bool] = UNSET, property: Unset[Literal[True]] = UNSET, validate: Unset[bool] = UNSET, metadata: Unset[dict] = UNSET, kw_only: Unset[dict] = UNSET) -> Any
Return an object to identify dataclass fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
The default value of the field. |
_MISSING
|
default_factory
|
_Missing[Callable]
|
A 0-argument function called to initialize a field's value. |
_MISSING
|
init
|
bool
|
If init is true, the field will be a parameter to the class's |
True
|
init_alias
|
Unset[str]
|
Field alias for init. |
UNSET
|
asdict_alias
|
Unset[str]
|
Field alis for asdict. |
UNSET
|
repr
|
Unset[Literal[False]]
|
If repr is true, the field will be included in the object's repr(). |
UNSET
|
compare
|
Unset[bool]
|
If compare is true, the field will be used in comparison functions. |
UNSET
|
property
|
Unset[Literal[True]]
|
If true fiels will became a property. |
UNSET
|
validate
|
Unset[bool]
|
Validate or not. |
UNSET
|
metadata
|
Unset[dict]
|
If specified, must be a mapping which is stored but not otherwise examined by dataclass. |
UNSET
|
kw_only
|
Unset[dict]
|
If kw_only true, the field will become a keyword-only parameter to |
UNSET
|
It is an error to specify both default and default_factory.
Source code in cwtch/cwtch.py
dataclass(cls=None, *, slots: Unset[bool] = UNSET, kw_only: Unset[bool] = UNSET, env_prefix: Unset[str | Sequence[str]] = UNSET, env_source: Unset[Callable] = UNSET, validate: Unset[bool] = UNSET, add_disable_validation_to_init: Unset[bool] = UNSET, show_input_value_on_error: Unset[bool] = UNSET, extra: Unset[Literal['ignore', 'forbid']] = UNSET, repr: Unset[bool] = UNSET, eq: Unset[bool] = UNSET, recursive: Unset[bool | Sequence[str]] = UNSET, handle_circular_refs: Unset[bool] = UNSET) -> Callable[[Type[T]], Type[T]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slots
|
Unset[bool]
|
If true, |
UNSET
|
kw_only
|
Unset[bool]
|
If kw_only is true, then by default all fields are keyword-only. |
UNSET
|
env_prefix
|
Unset[str | Sequence[str]]
|
Prefix(or list of prefixes) for environment variables. |
UNSET
|
env_source
|
Unset[Callable]
|
Environment variables source factory. By default os.environ. |
UNSET
|
validate
|
Unset[bool]
|
If false, validation will be disabled for the entire class. |
UNSET
|
add_disable_validation_to_init
|
Unset[bool]
|
Add disable_validation keywoard argument to |
UNSET
|
extra
|
Unset[Literal['ignore', 'forbid']]
|
Ignore or forbid extra arguments passed to init. |
UNSET
|
repr
|
Unset[bool]
|
If true, a |
UNSET
|
eq
|
Unset[bool]
|
If true, an |
UNSET
|
recursive
|
Unset[bool | Sequence[str]]
|
... |
UNSET
|
handle_circular_refs
|
Unset[bool]
|
If true, cwtch will handle circular references.. |
UNSET
|
Source code in cwtch/cwtch.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
view(base_cls, name: Unset[str] = UNSET, *, base: Unset[Type] = UNSET, attach: Unset[bool | Type] = UNSET, include: Unset[Sequence[str]] = UNSET, exclude: Unset[Sequence[str]] = UNSET, slots: Unset[bool] = UNSET, kw_only: Unset[bool] = UNSET, env_prefix: Unset[str | Sequence[str]] = UNSET, env_source: Unset[Callable] = UNSET, validate: Unset[bool] = UNSET, add_disable_validation_to_init: Unset[bool] = UNSET, extra: Unset[Literal['ignore', 'forbid']] = UNSET, repr: Unset[bool] = UNSET, eq: Unset[bool] = UNSET, recursive: Unset[bool | Sequence[str]] = UNSET, handle_circular_refs: Unset[bool] = UNSET) -> Callable[[Type[T]], Type[T]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Unset[str]
|
View name. |
UNSET
|
base
|
Unset[Type]
|
Class to use as source. |
UNSET
|
attach
|
Unset[bool | Type]
|
If true, view will be attached to base cls. |
UNSET
|
include
|
Unset[Sequence[str]]
|
List of fields to include in view. |
UNSET
|
exclude
|
Unset[Sequence[str]]
|
List of fields to exclude from view. |
UNSET
|
slots
|
Unset[bool]
|
If true, |
UNSET
|
kw_only
|
Unset[bool]
|
If kw_only is true, then by default all fields are keyword-only. |
UNSET
|
env_prefix
|
Unset[str | Sequence[str]]
|
Prefix(or list of prefixes) for environment variables. If UNSET value from base view model will be used. |
UNSET
|
env_source
|
Unset[Callable]
|
Environment variables source factory. If UNSET value from base view model will be used. |
UNSET
|
validate
|
Unset[bool]
|
Validate or not fields. If UNSET value from base view model will be used. |
UNSET
|
add_disable_validation_to_init
|
Unset[bool]
|
Add disable_validation keywoard argument to |
UNSET
|
extra
|
Unset[Literal['ignore', 'forbid']]
|
Ignore or forbid extra arguments passed to init. If UNSET value from base view model will be used. |
UNSET
|
repr
|
Unset[bool]
|
If true, a |
UNSET
|
eq
|
Unset[bool]
|
If true, an |
UNSET
|
recursive
|
Unset[bool | Sequence[str]]
|
... |
UNSET
|
handle_circular_refs
|
Unset[bool]
|
Handle or not circular refs. If UNSET value from base view model will be used. |
UNSET
|
Source code in cwtch/cwtch.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
from_attributes(cls, obj: Any, data: dict | None = None, exclude: Sequence | None = None, suffix: str | None = None, reset_circular_refs: bool | None = None)
Build model from attributes of other object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object from which to build. |
required |
data
|
dict | None
|
Additional data to build. |
None
|
exclude
|
Sequence | None
|
List of fields to exclude. |
None
|
suffix
|
str | None
|
Fields suffix. |
None
|
reset_circular_refs
|
bool | None
|
Reset circular references to None. |
None
|
Source code in cwtch/cwtch.py
asdict(inst, include: Sequence[str] | None = None, exclude: Sequence[str] | None = None, exclude_none: bool | None = None, exclude_unset: bool | None = None, context: dict | None = None) -> dict
Return cwtch model as dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inst
|
cwtch model. |
required | |
include
|
Sequence[str] | None
|
List of field names to include. |
None
|
exclude
|
Sequence[str] | None
|
List of field names to exclude. |
None
|
exclude_none
|
bool | None
|
If true, fields with None value will be excluded. |
None
|
exclude_unset
|
bool | None
|
If true, unset fields will be excluded. |
None
|
context
|
dict | None
|
If specified, must be a mapping. |
None
|
Source code in cwtch/cwtch.py
dumps_json(inst, encoder: Callable[[Any], Any] | None = None, context: dict | None = None)
Dump cwtch model to json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder
|
Callable[[Any], Any] | None
|
Custom JSON encoder as callable. |
None
|
context
|
dict | None
|
If specified, must be a mapping. |
None
|
Source code in cwtch/cwtch.py
validate_args(fn: Callable, args: tuple, kwds: dict) -> tuple[tuple, dict]
Helper to convert and validate function arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
tuple
|
function positional arguments. |
required |
kwds
|
dict
|
function keyword arguments. |
required |
Source code in cwtch/cwtch.py
validate_call(fn)
Decorator to convert and validate function arguments.
clone(base_cls, *, include: Unset[Sequence[str]] = UNSET, exclude: Unset[Sequence[str]] = UNSET, slots: Unset[bool] = UNSET, kw_only: Unset[bool] = UNSET, env_prefix: Unset[str | Sequence[str]] = UNSET, env_source: Unset[Callable] = UNSET, validate: Unset[bool] = UNSET, add_disable_validation_to_init: Unset[bool] = UNSET, extra: Unset[Literal['ignore', 'forbid']] = UNSET, repr: Unset[bool] = UNSET, eq: Unset[bool] = UNSET, handle_circular_refs: Unset[bool] = UNSET) -> Callable[[Type[T]], Type[T]]
TODO
Source code in cwtch/cwtch.py
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 | |
cwtch.metadata
Validator
Bases: ValidatorBefore, ValidatorAfter
Validator class.
Attributes:
| Name | Type | Description |
|---|---|---|
json_schema |
dict
|
Additional custom JSON schema. |
before |
Callable
|
Validator to validate input data before base validation. |
after |
Callable
|
Validator to validate value after base validation. |
Ge
Bases: _Ge
Validator to check that the input data is greater than or equal to the specified value.
Example:
Annotated[int, Ge(1)]
Gt
Bases: _Gt
Validator to check if input is greater than specified value.
Example:
Annotated[int, Gt(1)]
Le
Bases: _Le
Validator to check that the input data is less than or equal to the specified value.
Example:
Annotated[int, Le(1)]
Lt
Bases: _Lt
Validator to check if input is less than specified value.
Example:
Annotated[int, Lt(1)]
MinLen
Bases: _MinLen
Validator to check that the length of the input data is greater than or equal to the specified value.
Example:
Annotated[str, MinLen(1)]
MaxLen
Bases: _MaxLen
Validator to check that the length of the input data is less than or equal to the specified value.
Example:
Annotated[str, MaxLen(1)]
Len
Bases: _Len
Validator to check that the input length is within the specified range.
Example:
Annotated[str, Len(1, 10)]
MinItems
Bases: _MinItems
Validator to check that the number of elements in the input is greater than or equal to the specified value.
Example:
Annotated[list, MinItems(1)]
MaxItems
Bases: _MaxItems
Validator to check that the number of elements in the input is less than or equal to the specified value.
Example:
Annotated[list, MaxItems(1)]
Match
Bases: ValidatorAfter
Validator to check that an input value matches a regular expression.
Example:
Annotated[str, Match(r".*")]
ToLower
Bases: _ToLowerA, _ToLowerB
Convert input to lower case.
Attributes:
| Name | Type | Description |
|---|---|---|
mode |
Literal['before', 'after']
|
Validation mode, before or after base validation. Default: after. |
Example:
Annotated[str, ToLower()]
ToUpper
Bases: _ToUpperA, _ToUpperB
Convert input to upper case.
Attributes:
| Name | Type | Description |
|---|---|---|
mode |
Literal['before', 'after']
|
Validation mode, before or after base validation. Default: after. |
Example:
Annotated[str, ToUpper()]
UrlConstraints
Bases: ValidatorAfter
URL constraints.
Attributes:
| Name | Type | Description |
|---|---|---|
schemes |
list[str] | None
|
List of valid schemes. |
ports |
list[int] | None
|
list of valid ports. |
Example:
Annotated[Url, UrlConstraints(schemes=["http", "https"])]
JsonLoads
Bases: ValidatorBefore
Validator to try load value from json.
Example:
Annotated[list[int], JsonLoads()]
Strict
Bases: ValidatorBefore
Validator to strict input type.
Example:
Annotated[int, Strict(int)]
EmailValidator
Bases: ValidatorAfter
Email address validator.
cwtch.types
UNSET = UNSET
module-attribute
Type to mark value as unset.
Number = int | float
module-attribute
Number type. Example:
i: Number = 1
f: Number = 1.1
Positive = Annotated[T, Ge(1)]
module-attribute
Positive type(Generic). Example:
i: Positive[int] = 1
n: Positive[Number] = 1.1
NonNegative = Annotated[T, Ge(0)]
module-attribute
Non negative type(Generic). Example:
i: NonNegative[int] = 0
n: NonNegative[Number] = 0.0
NonEmpty = Annotated[T, MinItems(1)]
module-attribute
Non empty container. Example:
l: NonEmpty[list] = [1]
NonZeroLen = Annotated[T, MinLen(1)]
module-attribute
Non zero length object. Example:
l: NonZeroLen[str] = "a"
LowerStr = Annotated[str, ToLower()]
module-attribute
Lower case string. Example:
s: LowerStr = "a"
UpperStr = Annotated[str, ToUpper()]
module-attribute
Upper case string. Example:
s: LowerStr = "A"
HttpUrl = Annotated[Url, UrlConstraints(schemes=['http', 'https'])]
module-attribute
Type for HTTP URL.
SecretHttpUrl = Annotated[SecretUrl, UrlConstraints(schemes=['http', 'https'])]
module-attribute
Type for secret HTTP URL.
WebsocketpUrl = Annotated[Url, UrlConstraints(schemes=['ws', 'wss'])]
module-attribute
Type for websocket URL.
FileUrl = Annotated[Url, UrlConstraints(schemes=['file'])]
module-attribute
Type for file URL.
FtpUrl = Annotated[Url, UrlConstraints(schemes=['ftp'])]
module-attribute
Type for FTP URL.
Strict
Bases: Generic[T]
Example:
b: Strict[bool] = True
Source code in cwtch/types.py
SecretBytes
Bases: bytes
Type to represent secret bytes.
Source code in cwtch/types.py
SecretStr
Bases: str
Type to represent secret string.
Source code in cwtch/types.py
Url
Bases: str, _UrlMixIn
Type to represent URL.
Source code in cwtch/types.py
SecretUrl
Bases: str, _UrlMixIn
Type to represent secret URL.
Source code in cwtch/types.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |