kwargs_obj
Class that maps .__init__(self, **kwargs) to attributes.
Documentation
This module can be used to automatically set values from **kwargs to attributes, and also to dissable setting of unset attributes.
Examples
Map **kwargs to attributes
Here you can see, how to map **kwargs to your attributes:
class Xex(KwargsObj):
def __init__(self, **kwargs):
self.something = None
self.something_else = None
self._kwargs_to_attributes(kwargs)
This will allow to pass parameters which sets something_else and something_different:
>>> x = Xex(something=True)
>>> x.something
True
Setting of unset attributes is dissabled:
>>> x = Xex(asd=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in __init__
File "kwargs_obj/kwargs_obj.py", line 61, in _kwargs_to_attributes
"Can't set %s parameter - it is not defined here!" % key
ValueError: Can't set asd parameter - it is not defined here!
Disable setting of unset attributes
There is also modified .__setattr__()` method, which disables to set new attributes. This may be good idea for data containers.
Modified .__setattr__() functionality can be triggered by setting the ._all_set attribute:
class Xex(KwargsObj):
def __init__(self):
self.something = None
self.something_else = None
self._all_set = True
It will be now impossible to set new attributes, which may be good for preventing typos:
>>> x = Xex()
>>> x.asd = True
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "kwargs_obj/kwargs_obj.py", line 50, in __setattr__
raise ValueError("%s is not defined in this class!" % name)
ValueError: asd is not defined in this class!
But you can still redefine already defined attributes:
>>> x.something = True
>>>
Changelog
1.0.0
Added tests.
Added documentation.
Package uploaded to pypi.
0.1.0
Project created.
Metadata
Release files for kwargs_obj 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kwargs_obj-1.0.0.tar.gz | 4.2 kB | Without provenance | Details for kwargs_obj-1.0.0.tar.gz |
Release files / kwargs_obj-1.0.0.tar.gz
| Download URL | kwargs_obj-1.0.0.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
acf094120268beb105eae4b28126697abdf3db39387fd2e85c75de2b615097ba
|
|
BLAKE2b-256 checksum How to use checksums |
3973c16fa20b1c36c7eb3ac9eb4e6a48bff25311987482726e95c5220a5f2ad4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for kwargs_obj 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.