🗂 Take the edge off `dataclass` 🗂
Project description
🗂 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 thedataclasses
module -
xmod
-ed for less cruft (sodatacls
is the same asdatacls.dataclass
) -
The default class is
datacls.immutable
wherefrozen=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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
datacls-4.8.0.tar.gz
(3.3 kB
view details)
Built Distribution
File details
Details for the file datacls-4.8.0.tar.gz
.
File metadata
- Download URL: datacls-4.8.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.11 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 608c8250e6af609a8291f9c748b494713254fdc88352f0611743ab421de51c64 |
|
MD5 | 4500d6c34c7b1ae15cae3e3619c0122e |
|
BLAKE2b-256 | 5f59b095a3a726bd8f4b32b94706c0f440f0791537d894eca03c8a01ec073995 |
File details
Details for the file datacls-4.8.0-py3-none-any.whl
.
File metadata
- Download URL: datacls-4.8.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.11 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b2f0f526e1162b3e946267e7ec7d445659a94500045c8ab62e4d846d3d3ae73 |
|
MD5 | ed14fc122a64aa4dbc6055ce4275bd32 |
|
BLAKE2b-256 | bb08f829c22e948c8790a5b215df5c9d8c6882df0dc7bde2cc91aae535856755 |