🗂 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 thedataclassesmodule -
xmod-ed for less cruft (sodataclsis the same asdatacls.dataclass) -
The default class is
datacls.immutablewherefrozen=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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datacls-4.9.0.tar.gz.
File metadata
- Download URL: datacls-4.9.0.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b70b5274fd68a59cbf6ad806cdc41965eb9021e4e91b5e5c83c7b7249aeda08f
|
|
| MD5 |
3ccb096fb208a9289a45050e1b04db94
|
|
| BLAKE2b-256 |
7d6f146fb58b0f2360672ff40911c86caa91baf2ba86f1c445f632755675381b
|
File details
Details for the file datacls-4.9.0-py3-none-any.whl.
File metadata
- Download URL: datacls-4.9.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1bd0187704b7974aa135680305ce880b738702871de1f9548a8d7c97683bfc8
|
|
| MD5 |
ca54108ca0b42189b68a7b811cd297ac
|
|
| BLAKE2b-256 |
7091879fe6fadbfa0f68cd980f9ad73f92ea0ceedb9d1d7408d9188017294b58
|