Skip to the content.

DictClass

Auto-generated documentation for dynamo_query.dictclasses.dictclass module.

DictClass

[find in source code]

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__

[find in source code]

def __post_init__() -> None:

Override this method for post-init operations

DictClass.compute_key

[find in source code]

@staticmethod
def compute_key(key: str) -> KeyComputer:

See also

DictClass().sanitize

[find in source code]

def sanitize(**kwargs: Any) -> None:

Sanitize all set fields.

Arguments

DictClass.sanitize_key

[find in source code]

@staticmethod
def sanitize_key(key: str) -> KeySanitizer:

See also

DictClass().update

[find in source code]

def update(*args: Dict[(str, Any)], **kwargs: ignore) -> None:

Override of original dict.update method to apply _set_item rules.