Skip to main content

More KWARGS! Let call parameters override kwargs

Project description

More KWARGS!

Motivation

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 then overwrite the properties that need to be changed for the desired 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-2.32.19026.tar.gz (3.9 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: mo-kwargs-2.32.19026.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.14

File hashes

Hashes for mo-kwargs-2.32.19026.tar.gz
Algorithm Hash digest
SHA256 e6503c7bf68a23161bd09c494361b79ee3b27c4ea1f159104bce2598dc7e6131
MD5 83af140afdaa66af8a6c5ebb3d78e1bc
BLAKE2b-256 1027340abd399d37ea9329b98d335fdecb6ee82c81d9dafb8b624432caeddabc

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