Skip to content

zincware/znfields

Repository files navigation

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)