API справочник
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, 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
|
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]]
|
|
UNSET
|
validate
|
Unset[bool]
|
|
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]
|
Validate or not fields. |
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]
|
Handle or not circular refs. |
UNSET
|
Source code in cwtch/cwtch.py
223 224 225 226 227 228 229 230 231 232 233 234 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 |
|
view(base_cls, name: Unset[str] = UNSET, *, attach: Unset[bool] = 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
|
attach
|
Unset[bool]
|
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
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 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 |
|
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.
cwtch.metadata
Validator
Bases: TypeMetadata
Validator object.
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. |
Source code in cwtch/metadata.py
Ge
Bases: TypeMetadata
Validator to check that the input data is greater than or equal to the specified value.
Example:
Annotated[int, Ge(1)]
Source code in cwtch/metadata.py
Gt
Bases: TypeMetadata
Validator to check if input is greater than specified value.
Example:
Annotated[int, Gt(1)]
Source code in cwtch/metadata.py
Le
Bases: TypeMetadata
Validator to check that the input data is less than or equal to the specified value.
Example:
Annotated[int, Le(1)]
Source code in cwtch/metadata.py
Lt
Bases: TypeMetadata
Validator to check if input is less than specified value.
Example:
Annotated[int, Lt(1)]
Source code in cwtch/metadata.py
MinLen
Bases: TypeMetadata
Validator to check that the length of the input data is greater than or equal to the specified value.
Example:
Annotated[str, MinLen(1)]
Source code in cwtch/metadata.py
MaxLen
Bases: TypeMetadata
Validator to check that the length of the input data is less than or equal to the specified value.
Example:
Annotated[str, MaxLen(1)]
Source code in cwtch/metadata.py
Len
Bases: TypeMetadata
Validator to check that the input length is within the specified range.
Example:
Annotated[str, Len(1, 10)]
Source code in cwtch/metadata.py
MinItems
Bases: TypeMetadata
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)]
Source code in cwtch/metadata.py
MaxItems
Bases: TypeMetadata
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)]
Source code in cwtch/metadata.py
Match
Bases: TypeMetadata
Validator to check that an input value matches a regular expression.
Example:
Annotated[str, Match(r".*")]
Source code in cwtch/metadata.py
UrlConstraints
Bases: TypeMetadata
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"])]
Source code in cwtch/metadata.py
JsonLoads
Bases: TypeMetadata
Validator to try load value from json.
Example:
Annotated[list[int], JsonLoads()]
Source code in cwtch/metadata.py
ToLower
Bases: TypeMetadata
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()]
Source code in cwtch/metadata.py
ToUpper
Bases: TypeMetadata
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()]
Source code in cwtch/metadata.py
Strict
Bases: TypeMetadata
Validator to strict input type.
Example:
Annotated[int, Strict(int)]
Source code in cwtch/metadata.py
EmailValidator
Bases: TypeMetadata
Email address validator.
Source code in cwtch/metadata.py
cwtch.types
UNSET = UNSET
module
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(Generict). 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"
StrictInt = Annotated[int, Strict(int)]
module-attribute
Strict integer. Example:
i: StrictInt = 1
StrictFloat = Annotated[float, Strict(float)]
module-attribute
Strict float. Example:
f: StrictFloat = 1.1
StrictNumber = StrictInt | StrictFloat
module-attribute
Strict number. Example:
n: StrictNumber = 1.1
StrictStr = Annotated[str, Strict(str)]
module-attribute
Strict string. Example:
s: StrictString = "a"
StrictBool = Annotated[bool, Strict(bool)]
module-attribute
Strict bool. Example:
b: StrictBool = True
HttpUrl = Annotated[Url, UrlConstraints(shemes=['http', 'https'])]
module-attribute
Type for HTTP URL.
SecretHttpUrl = Annotated[SecretUrl, UrlConstraints(shemes=['http', 'https'])]
module-attribute
Type for secret HTTP URL.
WebsocketpUrl = Annotated[Url, UrlConstraints(shemes=['ws', 'wss'])]
module-attribute
Type for websocket URL.
FileUrl = Annotated[Url, UrlConstraints(shemes=['file'])]
module-attribute
Type for file URL.
FtpUrl = Annotated[Url, UrlConstraints(shemes=['ftp'])]
module-attribute
Type for FTP URL.
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
307 308 309 310 311 312 313 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 |
|