Skip to main content

More KWARGS! Let call parameters override kwargs

Project description

Extensive use of dependency injection, plus managing the configuration for each of the components being injected, can result in some spectacularly complex system configuration. One way to reduce the complexity is to use configuration templates that contain useful defaults, and simply overwrite the properties that need to be changed for the new configuration. @override has been created to provide this templating system for Python function calls (primarily class constructors).

@override will decorate a function to accept a kwargs parameter which is just like **kwargs, but the call parameters will override the properties in kwargs, rather than raise duplicate key exceptions.

Example

We decorate the login() function with @override. In this case, username is a required parameter, and password will default to None. The kwargs parameter should always default to None so that it’s not required.

@override
def login(username, password=None, kwargs=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")

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-1.0.17085.zip (7.8 kB view details)

Uploaded Source

Built Distribution

mo_kwargs-1.0.17085-py2.7.egg (3.7 kB view details)

Uploaded Source

File details

Details for the file mo-kwargs-1.0.17085.zip.

File metadata

  • Download URL: mo-kwargs-1.0.17085.zip
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mo-kwargs-1.0.17085.zip
Algorithm Hash digest
SHA256 3e368128e12f22909de222d51a80e651b7ebef43d025604c7b44961846662f55
MD5 4d91565a22fbeb792cdab0293a6497fe
BLAKE2b-256 db991fedf27929e9dfab97b2dc19397545530121d3c4d22ba772dd83137b2ecf

See more details on using hashes here.

File details

Details for the file mo_kwargs-1.0.17085-py2.7.egg.

File metadata

File hashes

Hashes for mo_kwargs-1.0.17085-py2.7.egg
Algorithm Hash digest
SHA256 a22750fb66755bb4739b4f1304b03ef95354b19675cfb948c2a259e98556c77a
MD5 32fea25ff21d15e3a9136030f8d6ac84
BLAKE2b-256 c4d8bec9b21d554f6ed095c9adf07b157f7e98bd9a3f3f0b0645a22ca82c0772

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