Skip to main content

Lazy evaluation of dataclass fields

Project description

zincware PyPI version Coverage Status

znfields

Provide a getter and setter for dataclasses.fields to allow e.g. for lazy evaluation or field content validation.

pip install znfields

Example

The znfields.field supports all arguments from dataclasses.field with the additional getter argument.

import dataclasses
import znfields

def getter(self, name) -> str:
    return f"{name}:{self.__dict__[name]}"

def setter(self, name, value) -> None:
    if not isinstance(value, float):
        raise ValueError(f"Value {value} is not a float")
    self.__dict__[name] = value

@dataclasses.dataclass
class MyModel(znfields.Base):
    parameter: float = znfields.field(getter=getter, setter=setter)

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page