datclass
python dataclass nested & extra attrs
Extending the official dataclass to support nested and extended fields.
Install
pip install -U datclass
pip install git+ssh://git@github.com/foyoux/datclass.git
pip install git+https://github.com/foyoux/datclass.git
Usage example
Example 1
from dataclasses import dataclass
from typing import List
from datclass import DatClass
@dataclass
class User(DatClass):
name: str
age: int
@dataclass
class Group(DatClass):
name: str
users: List[User]
if __name__ == '__main__':
user1 = User(name='foo', age=18)
user1.to_file('user.json')
user1.to_file('user.json', indent=4, ignore_none=True, sort_keys=True)
user2 = User(**{'name': 'bar', 'age': 20})
dict1 = user2.to_dict()
dict2 = user2.to_dict(ignore_none=True)
user3 = User.from_str('{"name": "baz", "age": 22}')
dict3 = user3.to_str()
dict4 = user3.to_str(indent=4, ignore_none=True)
user4 = User.from_file('user.json')
grp = Group(name='group1', users=[user1, user2, user3, user4])
grp.to_file('group.json', indent=4, ignore_none=True, sort_keys=True)
for user in grp.users:
print(user.name, user.age)
user = {'name': 'foo', 'age': 18, 'sex': 1}
user5 = User(**user)
assert user5.to_dict() == user
Example 2
from dataclasses import dataclass
from typing import ClassVar, Dict
from datclass import DatClass
@dataclass
class User(DatClass):
name: str
age: int
attr_123: str
__rename_attrs__: ClassVar[Dict[str, str]] = {
'Name': 'name',
'Age': 'age',
'123#$^%^%*': 'attr_123'
}
if __name__ == '__main__':
s = '{"Name": "foo", "Age": 18, "123#$^%^%*": "test rename attrs"}'
user = User.from_str(s)
assert user.to_str() == s
Automatically generate DataClass
See the datclass
command for details.
$ datclass -h
Example 1
Input user.json
{
"Name": "foo",
"Age": 18,
"123#$^%^%*": "test rename attrs"
}
Execute the command
$ datclass -o user.py user.json
Output user.py
from dataclasses import dataclass
from datclass import DatClass
@dataclass
class Object(DatClass):
a_123: str = None
age: int = None
name: str = None
__rename_attrs__ = {
'Name': 'name',
'Age': 'age',
'123#$^%^%*': 'a_123',
}
欢迎反馈和建议
Feedback and suggestions welcome
https://github.com/foyoux/datclass/issues/new