kwandl: Keyword arguments handled
Installation
Install kwandl using pip:
python3 -m pip install kwandl
To install the most recent development version directly from the GitHub repository run:
python3 -m pip install git+https://github.com/egpbos/kwandl.git
Usage
kwandl is essentially a box of magic tricks to make working with kwargs smoother.
Forward passing only relevant kwargs
Say you have a function top which takes **kwargs and passes them on to ding and boop:
def top(**kwargs):
ding(**kwargs)
boop(**kwargs)
Only problem is: ding and boop have different sets of keyword arguments:
def ding(dingo=1, dinga=2):
...
def boop(boopie=3, boonk=4):
...
This means top(dingo="blurg", boonk=None) will fail with a TypeError, because boonk will be passed to ding and dingo will be passed to boop, both of which are invalid.
kwandl solves this for you with one simple decorator:
@kwandl.forward
def top(**kwargs):
ding(**kwargs)
boop(**kwargs)
This Just Works™.
It does so by modifying the abstract syntax tree (AST) of the function calls with kwargs as argument so that kwargs is effectively replaced by kwandl.get_kwargs_applicable_to_function(ding, kwargs) (or boop instead of ding depending on the calling function).
An alternative way to use this kwandl functionality then is to use get_kwargs_applicable_to_function directly:
def top(**kwargs):
ding(**kwandl.get_kwargs_applicable_to_function(ding, kwargs))
boop(**kwandl.get_kwargs_applicable_to_function(boop, kwargs))
Note, however, that @kwandl.forward does a bit more: it also checks whether kwargs contains keyword arguments that are a match to neither ding nor boop and raises a TypeError if so.
A complete alternative notation for @kwandl.forward would then be:
def top(**kwargs):
ding_kwargs = kwandl.get_kwargs_applicable_to_function(ding, kwargs)
boop_kwargs = kwandl.get_kwargs_applicable_to_function(boop, kwargs)
unexpected_keywords = set(kwargs) - set(ding_kwargs) - set(boop_kwargs)
if unexpected_keywords:
raise TypeError(f"top() got an unexpected keyword argument '{unexpected_keywords.pop()}'")
ding(**ding_kwargs)
boop(**boop_kwargs)
The motivation behind this package should now become clearer: the amount of boilerplate necessary to solve what in my mind should not be such a huge problem is ... well, huge.
Documentation
For more details, see the full documentation on Readthedocs.
Contributing
If you want to contribute to the development of kwandl,
have a look at the contribution guidelines.
Credits
This package was created with Cookiecutter and the NLeSC/python-template.
Metadata
Release files for kwandl 0.2.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 | |
|---|---|---|---|
| kwandl-0.2.0.tar.gz | 12.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kwandl-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.8 kB
Release files / kwandl-0.2.0.tar.gz
| Download URL | kwandl-0.2.0.tar.gz |
|---|---|
| Size | 12.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7f74b52e8840ed38f6dce20f690ca09ecbbba35a27956fe757e45ead2ea74dd0
|
|
BLAKE2b-256 checksum How to use checksums |
a0c3a023d7dc6eba74ceb209a7e525689c4e6cfd08909212db94738e34101e72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12
|
Release files / kwandl-0.2.0-py3-none-any.whl
| Download URL | kwandl-0.2.0-py3-none-any.whl |
|---|---|
| Size | 11.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5e495fcf26850cd02c5673b28eb3f9ac6ff4ef22bff1e03caa5e0f3f545a9d8f
|
|
BLAKE2b-256 checksum How to use checksums |
99227461ae7e16ac14eb733615f854afaeba2b5ca719bd9ccc9f767dbeb1ec4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12
|