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.17039.zip (7.6 kB view details)

Uploaded Source

Built Distribution

mo_kwargs-1.0.17039-py2.7.egg (5.6 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for mo-kwargs-1.0.17039.zip
Algorithm Hash digest
SHA256 902b716d206762c5437ed82d4024c22dc419846c5d284e703a329f3162a1ea10
MD5 e3b5f6e7003ddeac7084cbf083006c49
BLAKE2b-256 9fe9f34b71a3ca4848fbb7d3c9b6171e86d0f5e933f5e815a20777723675a1d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mo_kwargs-1.0.17039-py2.7.egg
Algorithm Hash digest
SHA256 121bfe11f4851e72b1b4ef9d2be0f8936831675523aa538ca28082f6a6295e2c
MD5 59fae176697c25ac9cae55cb421a7baa
BLAKE2b-256 08d62e71d1b153a9e7b8f5f9a60ee10d446b23dc74de3df2cafa4485ae07efc0

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