Skip to main content

Object destructuring of function parameters for Python!

Project description

More KWARGS!

Object destructuring of function parameters for Python!

PyPI Latest Release Build Status Coverage Status Downloads

Motivation

Javascript has object destructuring, and it can be used for function parameters. This has a couple of benefts over Python's keyword arguments:

  • Extra caller parameters are ignored (eg f({a, b, c}))
  • Duplicate parameters are handled elegantly (eg f({a, a}))

The mo-kwargs library provides this functionality with the @override decorator, with additional benefits:

  • required parameters throw an error if missing, just like regular Python
  • all parameters, even ones not in the argument list, are passed in the optional kwargs parameter

The @override decorator adds a kwargs argument which can be passed a dict of call parameters; but unlike **kwargs, it will not raise duplicate key exceptions.

Provide default values

We decorate the login() function with @override. username is a required parameter, and password will default to None.

    @override
    def login(username, password=None):
        pass

Define some dicts for use with our kwargs parameter:

    creds = {"userame": "ekyle", "password": "password123"}
    alt_creds = {"username": "klahnakoski"}

The simplest case is when we use kwargs with no overrides

    login(kwargs=creds)
    # SAME AS
    login(**creds)
    # SAME AS
    login(username="ekyle", password="password123")

You may override any property in kwargs: In this case it is password

    login(password="123", kwargs=creds)
    # SAME AS
    login(username="ekyle", password="123")

There is no problem with overriding everything in kwargs:

    login(username="klahnakoski", password="asd213", kwargs=creds)
    # SAME AS
    login(username="klahnakoski", password="asd213")

You may continue to use **kwargs; which provides a way to overlay one parameter template (creds) with another (alt_creds)

    login(kwargs=creds, **alt_creds)
    # SAME AS
    login(username="klahnakoski", password="password123")

Handle too many parameters

Sometimes your method parameters come from a configuration file, or some other outside source which is outside your control. There may be more parameters than your method is willing to accept.

    creds = {"username": "ekyle", "password": "password123", "port":9000}
    def login(username, password=None):
         print(kwargs.get("port"))

Without mo-kwargs, passing the creds dictionary directly to login() would raise a key error

    >>> login(**creds)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: login() got an unexpected keyword argument 'port'

The traditional solution is to pass the parameters explicitly:

    login(username=creds.username, password=creds.password)

but that can get get tedious when done often, or the parameter list get long. mo-kwargs allows you to pass the whole dictionary to the kwargs parameter; only the parameters used by the method are used:

    @override
    def login(username, password=None):
        pass
     
    login(kwargs=creds)
    # SAME AS
    login(username=creds.username, password=creds.password)

Package all parameters

Your method can accept kwargs as a parameter. If it does, ensure it defaults to None so that it's not required.

    @override
    def login(username, password=None, kwargs=None):
        print(kwargs.get("username"))
        print(kwargs.get("port"))

kwargs will always be a dict, possibly empty, with the full set of parameters. This is different from using **kwargs which contains only the remainder of the keyword parameters.

    >>> creds = {"username": "ekyle", "password": "password123", "port":9000}
    >>> login(**creds)
    ekyle
    9000

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

mo-kwargs-7.550.24062.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

mo_kwargs-7.550.24062-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file mo-kwargs-7.550.24062.tar.gz.

File metadata

  • Download URL: mo-kwargs-7.550.24062.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.6

File hashes

Hashes for mo-kwargs-7.550.24062.tar.gz
Algorithm Hash digest
SHA256 eea5f33c24f842aee7b7f3fcb2a1c5e2e90abc9a1897cc1de4204559819a3fac
MD5 41c710a613f756bc9f264c2a2425fc59
BLAKE2b-256 115c7b82a8f4d01702b84a464b3940833d23be4404b4e3952e429262db07d5f3

See more details on using hashes here.

File details

Details for the file mo_kwargs-7.550.24062-py3-none-any.whl.

File metadata

File hashes

Hashes for mo_kwargs-7.550.24062-py3-none-any.whl
Algorithm Hash digest
SHA256 2e604ef2453513896e1d050de1ba9a3e35a94435e4401e190eac57e866f530f4
MD5 888708f2a2bd076b24edd90ffc70b081
BLAKE2b-256 a4cfc7f0d1d79a5d6fc6fb0ce54c268e9f1f296ae11d31b7d17416d4dc755ed8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page