https://github.com/zincware/znfields
https://github.com/zincware/znfields
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zincware/znfields
- Owner: zincware
- License: apache-2.0
- Created: 2024-08-02T12:00:32.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-08-04T20:08:23.000Z (10 months ago)
- Last Synced: 2025-08-04T22:55:56.759Z (10 months ago)
- Language: Python
- Size: 75.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/zincware)
[](https://badge.fury.io/py/znfields)
[](https://coveralls.io/github/zincware/znfields?branch=main)
# znfields
Provide a `getter` and `setter` for `dataclasses.fields` to allow e.g. for lazy
evaluation or field content validation.
```bash
pip install znfields
```
## Example
The `znfields.field` supports all arguments from `dataclasses.field` with the
additional `getter` argument.
```python
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)
```