DictClass
Auto-generated documentation for dynamo_query.dictclasses.dictclass module.
- dynamo-query / Modules / Dynamo Query / Dictclasses / DictClass
DictClass
class DictClass(dict):
def __init__(*args: Dict[(str, Any)], **kwargs: Any) -> None:
Dict-based dataclass.
Examples
class UserRecord(DictClass):
# required fields
name: str
# optional fields
company: str = "Amazon"
age: Optional[int] = None
def __post_init__(self):
# do any post-init operations here
self.age = self.age or 35
# add extra computed field
@DictClass.compute_key("min_age")
def _compute_min_age(self) -> int:
return 18
# sanitize value on set
@DictClass.sanitize_key("age")
def _sanitize_key_age(self, value: int) -> int:
return max(self.age, 18)
record = UserRecord(name="Jon")
record["age"] = 30
record.age = 30
record.update({"age": 30})
dict(record) # {"name": "Jon", "company": "Amazon", "age": 30, "min_age": 18}
DictClass().__post_init__
def __post_init__() -> None:
Override this method for post-init operations
DictClass.compute_key
@staticmethod
def compute_key(key: str) -> KeyComputer:
See also
DictClass().sanitize
def sanitize(**kwargs: Any) -> None:
Sanitize all set fields.
Arguments
kwargs
- Arguments for sanitize_key_{key}
DictClass.sanitize_key
@staticmethod
def sanitize_key(key: str) -> KeySanitizer:
See also
DictClass().update
def update(*args: Dict[(str, Any)], **kwargs: ignore) -> None:
Override of original dict.update
method to apply _set_item
rules.