# 🗂 `datacls`: take the edge off `dataclass` 🗂
`dataclasses` is almost perfect.
`datacls` is a tiny, thin wrapper around `dataclass.dataclasses` making it
a bit more self-contained, reflective, and saving a bit of typing.
`datacls` is exactly like `dataclass`, except:
* Adds three new instance methods: `asdict()`, `astuple()`, `replace()`,
and one new class method, `fields()`, all taken from the `dataclasses`
module
* `xmod`-ed for less cruft (so `datacls` is the same as `datacls.dataclass`)
* The default class is `datacls.immutable` where `frozen=True`.
## Example
import datacls
@datacls
class One:
one: str = 'one'
two: int = 2
three: dict = datacls.field(dict)
# `One` has three instance methods: asdict(), astuple(), replace()
o = One()
assert o.asdict() == {'one': 'one', 'two': 2, 'three': {}}
import dataclasses
assert dataclasses.asdict(o) == o.asdict()
assert o.astuple() == ('one', 2, {})
o2 = o.replace(one='seven', three={'nine': 9})
assert o2 == One('seven', 2, {'nine': 9})
# `One` has one new class method: fields()
assert [f.name for f in One.fields()] == ['one', 'two', 'three']
# @datacls is immutable.
try:
o.one = 'three'
except AttributeError:
pass
else:
raise AttributeError('Was mutable!')
# Usec @datacls.mutable or @datacls(frozen=False)
# for mutable classes
@datacls.mutable
class OneMutable:
one: str = 'one'
two: int = 2
three: Dict = datacls.field(dict)
om = OneMutable()
om.one = 'three'
assert str(om) == "OneMutable(one='three', two=2, three={})"
# These four new methods won't break your old dataclass by mistake:
@datacls
class Overloads:
one: str = 'one'
asdict: int = 1
astuple: int = 1
fields: int = 1
replace: int = 1
o = Overloads()
assert ov.one == 'one'
assert ov.asdict == 1
assert ov.astuple == 1
assert ov.fields == 1
assert ov.replace == 1
# You can still access the methods as functions on `datacls`:
assert (
datacls.asdict(ov) ==
{'asdict': 1, 'astuple': 1, 'fields': 1, 'one': 'one', 'replace': 1}
)
### [API Documentation](https://rec.github.io/datacls#datacls--api-documentation)
Raw data
{
"_id": null,
"home_page": "https://github.com/rec/datacls",
"name": "datacls",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "",
"author": "Tom Ritchford",
"author_email": "tom@swirly.com",
"download_url": "https://files.pythonhosted.org/packages/5f/59/b095a3a726bd8f4b32b94706c0f440f0791537d894eca03c8a01ec073995/datacls-4.8.0.tar.gz",
"platform": null,
"description": "# \ud83d\uddc2 `datacls`: take the edge off `dataclass` \ud83d\uddc2\n\n`dataclasses` is almost perfect.\n\n`datacls` is a tiny, thin wrapper around `dataclass.dataclasses` making it\na bit more self-contained, reflective, and saving a bit of typing.\n\n`datacls` is exactly like `dataclass`, except:\n\n * Adds three new instance methods: `asdict()`, `astuple()`, `replace()`,\n and one new class method, `fields()`, all taken from the `dataclasses`\n module\n\n * `xmod`-ed for less cruft (so `datacls` is the same as `datacls.dataclass`)\n\n * The default class is `datacls.immutable` where `frozen=True`.\n\n## Example\n\n import datacls\n\n @datacls\n class One:\n one: str = 'one'\n two: int = 2\n three: dict = datacls.field(dict)\n\n # `One` has three instance methods: asdict(), astuple(), replace()\n\n o = One()\n assert o.asdict() == {'one': 'one', 'two': 2, 'three': {}}\n\n import dataclasses\n assert dataclasses.asdict(o) == o.asdict()\n\n assert o.astuple() == ('one', 2, {})\n\n o2 = o.replace(one='seven', three={'nine': 9})\n assert o2 == One('seven', 2, {'nine': 9})\n\n # `One` has one new class method: fields()\n\n assert [f.name for f in One.fields()] == ['one', 'two', 'three']\n\n # @datacls is immutable.\n\n try:\n o.one = 'three'\n except AttributeError:\n pass\n else:\n raise AttributeError('Was mutable!')\n\n # Usec @datacls.mutable or @datacls(frozen=False)\n # for mutable classes\n\n @datacls.mutable\n class OneMutable:\n one: str = 'one'\n two: int = 2\n three: Dict = datacls.field(dict)\n\n om = OneMutable()\n om.one = 'three'\n assert str(om) == \"OneMutable(one='three', two=2, three={})\"\n\n # These four new methods won't break your old dataclass by mistake:\n @datacls\n class Overloads:\n one: str = 'one'\n asdict: int = 1\n astuple: int = 1\n fields: int = 1\n replace: int = 1\n\n o = Overloads()\n\n assert ov.one == 'one'\n assert ov.asdict == 1\n assert ov.astuple == 1\n assert ov.fields == 1\n assert ov.replace == 1\n\n # You can still access the methods as functions on `datacls`:\n assert (\n datacls.asdict(ov) ==\n {'asdict': 1, 'astuple': 1, 'fields': 1, 'one': 'one', 'replace': 1}\n )\n\n\n### [API Documentation](https://rec.github.io/datacls#datacls--api-documentation)\n",
"bugtrack_url": null,
"license": "",
"summary": "\ud83d\uddc2 Take the edge off `dataclass` \ud83d\uddc2",
"version": "4.8.0",
"project_urls": {
"Documentation": "https://rec.github.io/datacls",
"Homepage": "https://github.com/rec/datacls",
"Repository": "https://github.com/rec/datacls"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bb08f829c22e948c8790a5b215df5c9d8c6882df0dc7bde2cc91aae535856755",
"md5": "ed14fc122a64aa4dbc6055ce4275bd32",
"sha256": "3b2f0f526e1162b3e946267e7ec7d445659a94500045c8ab62e4d846d3d3ae73"
},
"downloads": -1,
"filename": "datacls-4.8.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ed14fc122a64aa4dbc6055ce4275bd32",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 4332,
"upload_time": "2024-01-25T13:29:25",
"upload_time_iso_8601": "2024-01-25T13:29:25.439572Z",
"url": "https://files.pythonhosted.org/packages/bb/08/f829c22e948c8790a5b215df5c9d8c6882df0dc7bde2cc91aae535856755/datacls-4.8.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5f59b095a3a726bd8f4b32b94706c0f440f0791537d894eca03c8a01ec073995",
"md5": "4500d6c34c7b1ae15cae3e3619c0122e",
"sha256": "608c8250e6af609a8291f9c748b494713254fdc88352f0611743ab421de51c64"
},
"downloads": -1,
"filename": "datacls-4.8.0.tar.gz",
"has_sig": false,
"md5_digest": "4500d6c34c7b1ae15cae3e3619c0122e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 3349,
"upload_time": "2024-01-25T13:29:28",
"upload_time_iso_8601": "2024-01-25T13:29:28.244510Z",
"url": "https://files.pythonhosted.org/packages/5f/59/b095a3a726bd8f4b32b94706c0f440f0791537d894eca03c8a01ec073995/datacls-4.8.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-25 13:29:28",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rec",
"github_project": "datacls",
"travis_ci": true,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "datacls"
}