An extension to the python dataclasses package that enables you to push the *args/*xargs
Project description
dataclasses-xargs
An extension to the dataclasses library allowing you to push your unnamed parameters to a specific field.
Installation
pip3 install dataclasses-xargs
Usage Example
from dataclasses_xargs import dataclass
@dataclass
class FailTest(object):
a: int = None
b: str = None
c: list = None
@dataclass(xarg_field="c")
class SuccessTest(object):
a: int = None
b: str = None
c: list = None
# When xarg_field is not defined, dataclasses work as normal and will throw an error if you pass in double parameters
try:
FailTest(1, 2, 3, 4, 5, a=1, b="b")
except TypeError as e:
assert e.args[0] == "FailTest.__init__() got multiple values for argument 'a'"
# When defined, and the xarg_field is not passed as a named parameter, all unnamed parameters will be pushed to the xarg_field.
# Note that the xarg_field still needs to be defined in the class, see above.
dcl = SuccessTest(1, 2, 3, 4, 5, a=1, b="b")
print(dcl)
"""
SuccessTest(a=1, b='b', c=(1, 2, 3, 4, 5))
"""
assert dcl.a == 1
assert dcl.b == "b"
assert dcl.c == (1, 2, 3, 4, 5)
dcl = SuccessTest(1, 2, 3, 4, 5, a=1, b="b", c=[0, 0, 0])
print(dcl)
"""
SuccessTest(a=1, b='b', c=[0, 0, 0])
"""
assert dcl.a == 1
assert dcl.b == "b"
assert dcl.c == [0, 0, 0]
exit(0)
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
dataclasses_xargs-1.0.2.tar.gz
(19.9 kB
view details)
Built Distribution
File details
Details for the file dataclasses_xargs-1.0.2.tar.gz
.
File metadata
- Download URL: dataclasses_xargs-1.0.2.tar.gz
- Upload date:
- Size: 19.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d32a07afbe8368b47ab672572a391f3483e5d9c8ffa226cce41699bdc3070f4 |
|
MD5 | 54c6d818efffbcdbb4cdb0cfd370f10d |
|
BLAKE2b-256 | c21048897485316205da3feeae02348415513b1d73a8f5a4eb29fc21b851d456 |
File details
Details for the file dataclasses_xargs-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: dataclasses_xargs-1.0.2-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11e31dfad81faa477f2e26c3f63fafb0ed16ae47eace42800b6db280c91d86b5 |
|
MD5 | 34db8f9d1430497657d176646462e9a5 |
|
BLAKE2b-256 | 3bc808567c8e3113fba85aff30a35896c4951238d1d947641faaca4b4ca76102 |