Keyword arguments handled
Project description
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.
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 kwandl-0.2.0.tar.gz.
File metadata
- Download URL: kwandl-0.2.0.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f74b52e8840ed38f6dce20f690ca09ecbbba35a27956fe757e45ead2ea74dd0
|
|
| MD5 |
cb26f88d2395c4a8b283868e0e134507
|
|
| BLAKE2b-256 |
a0c3a023d7dc6eba74ceb209a7e525689c4e6cfd08909212db94738e34101e72
|
File details
Details for the file kwandl-0.2.0-py3-none-any.whl.
File metadata
- Download URL: kwandl-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e495fcf26850cd02c5673b28eb3f9ac6ff4ef22bff1e03caa5e0f3f545a9d8f
|
|
| MD5 |
d451a35e46787cbaa1350a8c4a81235e
|
|
| BLAKE2b-256 |
99227461ae7e16ac14eb733615f854afaeba2b5ca719bd9ccc9f767dbeb1ec4b
|