Skip to main content

fair-software badge build

kwandl: Keyword arguments handled

Installation

To install kwandl from the GitHub repository, do:

git clone https://github.com/egpbos/kwandl.git
cd kwandl
python3 -m pip install .

Usage

kwandl is essentially a box of magic tricks to make working with kwargs smoother.

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.replace_kwargs_in_calls
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.replace_kwargs_in_calls 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.replace_kwargs_in_calls 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

Documentation Status

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kwandl-0.1.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kwandl-0.1.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file kwandl-0.1.0.tar.gz.

File metadata

  • Download URL: kwandl-0.1.0.tar.gz
  • Upload date:
  • Size: 11.0 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.8 tqdm/4.63.0 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for kwandl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 16e685beb75b6e368926e874b061ee9104e445d2eee7483d0b8d7b03336ec798
MD5 b6c6efcaf386d84716b39cd12b979b80
BLAKE2b-256 a9754f8c5c950ab2daa0adef4f0d597ca34ff503dd7729e71894c0434b8f974b

See more details on using hashes here.

File details

Details for the file kwandl-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: kwandl-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 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.8 tqdm/4.63.0 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for kwandl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e79d160c2a85ad92024e104182ce36834583dc6ea4be0702cfbdc8dbaed05e3d
MD5 2eb77016651492eac5951040893bbb9a
BLAKE2b-256 248af1ba0d6a03bcf7e0e5c5400b09ff23f95a4d3c1e282e375a7e4811ce1a90

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page