More KWARGS!
| Branch | Status |
|---|---|
| master | |
| dev |
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. It is mostly used for class constructors, but any method can benefit. The @overrides decorator adds a kwargs parameter which can be given a template of default parameters; but unlike **kwargs, it will not raise duplicate key exceptions.
Provide default values
We decorate the login() function with @override. In this case, 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
Metadata
Release files for mo-kwargs 7.124.22022
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mo-kwargs-7.124.22022.tar.gz | 10.9 kB | Details |
Release files / mo-kwargs-7.124.22022.tar.gz
| Download URL | mo-kwargs-7.124.22022.tar.gz |
|---|---|
| Size | 10.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bafc3c50cec92c639e7d7f4f8f4c595c068f9f9c6b411ce0ebeb9c287d7877cd
|
|
BLAKE2b-256 checksum How to use checksums |
fc27c2b637583713809b70e717ac3cc01640734f88566be1c5036a727979d247
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8
|