# datclass
python dataclass nested & extra attrs
Extending the official [dataclass](https://docs.python.org/zh-cn/3/library/dataclasses.html) to support nested and extended fields.
## Install
[![PyPI](https://img.shields.io/pypi/v/datclass)](https://pypi.org/project/datclass/) [![python version](https://img.shields.io/pypi/pyversions/datclass)](https://pypi.org/project/datclass/) [![Downloads](https://static.pepy.tech/personalized-badge/datclass?period=total&units=international_system&left_color=black&right_color=orange&left_text=Downloads)](https://pepy.tech/project/datclass)
```sh
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
```py
from dataclasses import dataclass
from typing import List
# Default `dataclass` supports nested and extended fields. Missing fields will be logged.
from datclass import DatClass
# Custom `DatClass`
# from datclass import get_datclass
# Missing fields will not be logged.
# DatClass = get_datclass(log=False)
@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)
# Saving a data class to a file.
user1.to_file('user.json')
# Adding some control parameters.
user1.to_file('user.json', indent=4, ignore_none=True, sort_keys=True)
# Creating a data class from a dict.
user2 = User(**{'name': 'bar', 'age': 20})
# To convert a data class to a dictionary, you can support extended fields.
# You can also use the official asdict function, but it cannot export extended fields.
dict1 = user2.to_dict()
# 'ignore_none' is used to ignore values that are None.
dict2 = user2.to_dict(ignore_none=True)
# Creating a data class from a string.
user3 = User.from_str('{"name": "baz", "age": 22}')
# Convert the data class to a JSON string.
dict3 = user3.to_str()
dict4 = user3.to_str(indent=4, ignore_none=True)
# Creating a data class from a file.
user4 = User.from_file('user.json')
# Nested data classes
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)
# Extending fields
user = {'name': 'foo', 'age': 18, 'sex': 1}
user5 = User(**user)
assert user5.to_dict() == user
```
### Example 2
```py
from dataclasses import dataclass
from typing import ClassVar, Dict
from datclass import DatClass
@dataclass
class User(DatClass):
name: str
age: int
attr_123: str
# Renaming fields.
__rename_attrs__: ClassVar[Dict[str, str]] = {
'Name': 'name',
'Age': 'age',
'123#$^%^%*': 'attr_123'
}
# This way of writing is also acceptable.
# __rename_attrs__ = {
# '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.
```sh
$ datclass -h
```
### Example 1
Input `user.json`
```json
{
"Name": "foo",
"Age": 18,
"123#$^%^%*": "test rename attrs"
}
```
Execute the command
```sh
$ datclass -o user.py user.json
```
Output user.py
```py
from dataclasses import dataclass
from datclass import DatClass
@dataclass
class Object(DatClass):
a_123: str = None # rename from '123#$^%^%*'
age: int = None # rename from 'Age'
name: str = None # rename from 'Name'
__rename_attrs__ = {
'Name': 'name',
'Age': 'age',
'123#$^%^%*': 'a_123',
}
```
---
欢迎反馈和建议
Feedback and suggestions welcome
https://github.com/foyoux/datclass/issues/new
Raw data
{
"_id": null,
"home_page": "",
"name": "datclass",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "dataclass,dataclasses,utils,nested,extra,attrs,json",
"author": "foyoux",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/28/92/8da5e24d9a5ee663d6fddb70631777d0ce6cbcc0f4a3b54900719d4a82e6/datclass-0.2.27.tar.gz",
"platform": null,
"description": "# datclass\n\npython dataclass nested & extra attrs\n\nExtending the official [dataclass](https://docs.python.org/zh-cn/3/library/dataclasses.html) to support nested and extended fields.\n\n## Install\n\n[![PyPI](https://img.shields.io/pypi/v/datclass)](https://pypi.org/project/datclass/) [![python version](https://img.shields.io/pypi/pyversions/datclass)](https://pypi.org/project/datclass/) [![Downloads](https://static.pepy.tech/personalized-badge/datclass?period=total&units=international_system&left_color=black&right_color=orange&left_text=Downloads)](https://pepy.tech/project/datclass)\n\n```sh\npip install -U datclass\npip install git+ssh://git@github.com/foyoux/datclass.git\npip install git+https://github.com/foyoux/datclass.git\n```\n\n## Usage example\n\n### Example 1\n\n```py\nfrom dataclasses import dataclass\nfrom typing import List\n\n# Default `dataclass` supports nested and extended fields. Missing fields will be logged.\nfrom datclass import DatClass\n\n\n# Custom `DatClass`\n# from datclass import get_datclass\n# Missing fields will not be logged.\n# DatClass = get_datclass(log=False)\n\n\n@dataclass\nclass User(DatClass):\n name: str\n age: int\n\n\n@dataclass\nclass Group(DatClass):\n name: str\n users: List[User]\n\n\nif __name__ == '__main__':\n user1 = User(name='foo', age=18)\n # Saving a data class to a file.\n user1.to_file('user.json')\n # Adding some control parameters.\n user1.to_file('user.json', indent=4, ignore_none=True, sort_keys=True)\n\n # Creating a data class from a dict.\n user2 = User(**{'name': 'bar', 'age': 20})\n # To convert a data class to a dictionary, you can support extended fields.\n # You can also use the official asdict function, but it cannot export extended fields.\n dict1 = user2.to_dict()\n # 'ignore_none' is used to ignore values that are None.\n dict2 = user2.to_dict(ignore_none=True)\n\n # Creating a data class from a string.\n user3 = User.from_str('{\"name\": \"baz\", \"age\": 22}')\n # Convert the data class to a JSON string.\n dict3 = user3.to_str()\n dict4 = user3.to_str(indent=4, ignore_none=True)\n\n # Creating a data class from a file.\n user4 = User.from_file('user.json')\n\n # Nested data classes\n grp = Group(name='group1', users=[user1, user2, user3, user4])\n grp.to_file('group.json', indent=4, ignore_none=True, sort_keys=True)\n\n for user in grp.users:\n print(user.name, user.age)\n\n # Extending fields\n user = {'name': 'foo', 'age': 18, 'sex': 1}\n user5 = User(**user)\n assert user5.to_dict() == user\n\n```\n\n### Example 2\n\n```py\nfrom dataclasses import dataclass\nfrom typing import ClassVar, Dict\n\nfrom datclass import DatClass\n\n\n@dataclass\nclass User(DatClass):\n name: str\n age: int\n attr_123: str\n\n # Renaming fields.\n __rename_attrs__: ClassVar[Dict[str, str]] = {\n 'Name': 'name',\n 'Age': 'age',\n '123#$^%^%*': 'attr_123'\n }\n\n # This way of writing is also acceptable.\n # __rename_attrs__ = {\n # 'Name': 'name',\n # 'Age': 'age',\n # '123#$^%^%*': 'attr_123',\n # }\n\n\nif __name__ == '__main__':\n s = '{\"Name\": \"foo\", \"Age\": 18, \"123#$^%^%*\": \"test rename attrs\"}'\n user = User.from_str(s)\n assert user.to_str() == s\n\n```\n\n## Automatically generate `DataClass`\n\nSee the `datclass` command for details.\n\n```sh\n$ datclass -h\n\n```\n\n### Example 1\n\nInput `user.json`\n\n```json\n{\n \"Name\": \"foo\",\n \"Age\": 18,\n \"123#$^%^%*\": \"test rename attrs\"\n}\n```\n\nExecute the command\n\n```sh\n$ datclass -o user.py user.json\n\n```\n\nOutput user.py\n\n```py\nfrom dataclasses import dataclass\n\nfrom datclass import DatClass\n\n\n@dataclass\nclass Object(DatClass):\n a_123: str = None # rename from '123#$^%^%*'\n age: int = None # rename from 'Age'\n name: str = None # rename from 'Name'\n\n __rename_attrs__ = {\n 'Name': 'name',\n 'Age': 'age',\n '123#$^%^%*': 'a_123',\n }\n\n```\n\n---\n\n\u6b22\u8fce\u53cd\u9988\u548c\u5efa\u8bae\n\nFeedback and suggestions welcome\n\nhttps://github.com/foyoux/datclass/issues/new\n",
"bugtrack_url": null,
"license": "",
"summary": "python package dataclass utils",
"version": "0.2.27",
"project_urls": {
"Bug Tracker": "https://github.com/foyoux/datclass/issues",
"Homepage": "https://github.com/foyoux/datclass",
"Source": "https://github.com/foyoux/datclass"
},
"split_keywords": [
"dataclass",
"dataclasses",
"utils",
"nested",
"extra",
"attrs",
"json"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fb7370bf055428f48cbbd5f2417255f2266a48ce73aa901d013189d1fdba0463",
"md5": "14b6df3ee5155868593d6a9800bc73ed",
"sha256": "7917753423c86b196f2e2dd3eacee5391592fea2a8f1a85ff1bed2de2da2cec5"
},
"downloads": -1,
"filename": "datclass-0.2.27-py3-none-any.whl",
"has_sig": false,
"md5_digest": "14b6df3ee5155868593d6a9800bc73ed",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 12512,
"upload_time": "2023-12-12T10:16:01",
"upload_time_iso_8601": "2023-12-12T10:16:01.088151Z",
"url": "https://files.pythonhosted.org/packages/fb/73/70bf055428f48cbbd5f2417255f2266a48ce73aa901d013189d1fdba0463/datclass-0.2.27-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "28928da5e24d9a5ee663d6fddb70631777d0ce6cbcc0f4a3b54900719d4a82e6",
"md5": "05aac3ab5ce25fa2d8605a1bac2332b3",
"sha256": "9decef06f32e624938be3355131750fb4d53a9fe04d902bbfa098cc5b10bf79d"
},
"downloads": -1,
"filename": "datclass-0.2.27.tar.gz",
"has_sig": false,
"md5_digest": "05aac3ab5ce25fa2d8605a1bac2332b3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 12910,
"upload_time": "2023-12-12T10:16:03",
"upload_time_iso_8601": "2023-12-12T10:16:03.307756Z",
"url": "https://files.pythonhosted.org/packages/28/92/8da5e24d9a5ee663d6fddb70631777d0ce6cbcc0f4a3b54900719d4a82e6/datclass-0.2.27.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-12 10:16:03",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "foyoux",
"github_project": "datclass",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "datclass"
}