diff --git a/mo_kwargs/__init__.py b/mo_kwargs/__init__.py index 49dba98..ea232a0 100644 --- a/mo_kwargs/__init__.py +++ b/mo_kwargs/__init__.py @@ -11,14 +11,13 @@ from functools import update_wrapper -from mo_dots import get_logger, is_data, to_data, zip as dict_zip, set_default +from mo_dots import get_logger, is_data, to_data from mo_future import ( get_function_arguments, get_function_defaults, get_function_name, text, is_text) -from mo_logs import Except KWARGS = str("kwargs") @@ -50,9 +49,9 @@ def output(func): } def raise_error(e, a, k): - packed = set_default(dict(zip(params, a)), k) + packed = k.copy() + packed.update(dict(zip(params, a))) err = text(e) - e = Except.wrap(e) if err.startswith(func_name) and ( "takes at least" in err or "takes exactly " in err @@ -97,10 +96,10 @@ def w_bound_method(*given_args, **given_kwargs): elif kwargs in given_kwargs and is_data(given_kwargs[kwargs]): # PUT args INTO given_kwargs a, k = params_pack( - params, defaults, given_kwargs[kwargs], dict_zip(params, given_args), given_kwargs + params, defaults, given_kwargs[kwargs], dict(zip(params, given_args)), given_kwargs ) else: - a, k = params_pack(params, defaults, dict_zip(params, given_args), given_kwargs) + a, k = params_pack(params, defaults, dict(zip(params, given_args)), given_kwargs) try: return func(*a, **k) except TypeError as e: @@ -117,11 +116,11 @@ def w_kwargs(*given_args, **given_kwargs): elif kwargs in given_kwargs and is_data(given_kwargs[kwargs]): # PUT given_args INTO given_kwargs a, k = params_pack( - params, defaults, given_kwargs[kwargs], dict_zip(params, given_args), given_kwargs + params, defaults, given_kwargs[kwargs], dict(zip(params, given_args)), given_kwargs ) else: # PULL kwargs OUT INTO PARAMS - a, k = params_pack(params, defaults, dict_zip(params, given_args), given_kwargs) + a, k = params_pack(params, defaults, dict(zip(params, given_args)), given_kwargs) try: return func(*a, **k) except TypeError as e: diff --git a/requirements.txt b/requirements.txt index 154f516..4f9fa79 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ -mo-future>3 -mo-dots>3 -mo-logs>3 +mo-future +mo-dots diff --git a/setup.py b/setup.py index 1c68030..6b38e14 100644 --- a/setup.py +++ b/setup.py @@ -8,13 +8,13 @@ classifiers=["Development Status :: 4 - Beta","Topic :: Software Development :: Libraries","Topic :: Software Development :: Libraries :: Python Modules","License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)"], description='More KWARGS! Let call parameters override kwargs', include_package_data=True, - install_requires=["mo-dots>=3.77.20190","mo-future>=3.71.20168","mo-logs>=3.75.20189"], + install_requires=["mo-dots>=3.78.20194","mo-future>=3.71.20168"], license='MPL 2.0', long_description='\n# More KWARGS!\n\n|Branch |Status |\n|------------|---------|\n|master | [![Build Status](https://travis-ci.org/klahnakoski/mo-kwargs.svg?branch=master)](https://travis-ci.org/klahnakoski/mo-kwargs) |\n|dev | [![Build Status](https://travis-ci.org/klahnakoski/mo-kwargs.svg?branch=dev)](https://travis-ci.org/klahnakoski/mo-kwargs) [![Coverage Status](https://coveralls.io/repos/github/klahnakoski/mo-kwargs/badge.svg?branch=dev)](https://coveralls.io/github/klahnakoski/mo-kwargs?branch=dev) |\n\n\n\n## Motivation\n\nExtensive 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. \n\n`@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.\n\n## Provide default values\n\nWe decorate the `login()` function with `@override`. In this case, `username` is a required parameter, and `password` will default to `None`. \n\n @override\n def login(username, password=None):\n pass\n\nDefine some `dicts` for use with our `kwargs` parameter:\n\n creds = {"userame": "ekyle", "password": "password123"}\n alt_creds = {"username": "klahnakoski"}\n\n\nThe simplest case is when we use `kwargs` with no overrides\n\n login(kwargs=creds)\n # SAME AS\n login(**creds)\n # SAME AS\n login(username="ekyle", password="password123")\n\nYou may override any property in `kwargs`: In this case it is `password`\n\n login(password="123", kwargs=creds)\n # SAME AS\n login(username="ekyle", password="123")\n\nThere is no problem with overriding everything in `kwargs`:\n\n login(username="klahnakoski", password="asd213", kwargs=creds)\n # SAME AS\n login(username="klahnakoski", password="asd213")\n\nYou may continue to use `**kwargs`; which provides a way to overlay one parameter template (`creds`) with another (`alt_creds`)\n\n login(kwargs=creds, **alt_creds)\n # SAME AS\n login(username="klahnakoski", password="password123")\n\n## Handle too many parameters\n\nSometimes 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. \n\n creds = {"username": "ekyle", "password": "password123", "port":9000}\n def login(username, password=None):\n print(kwargs.get("port"))\n\nWithout `mo-kwargs`, passing the `creds` dictionary directly to `login()` would raise a key error\n\n >>> login(**creds)\n Traceback (most recent call last):\n File "", line 1, in \n TypeError: login() got an unexpected keyword argument \'port\'\n \nThe traditional solution is to pass the parameters explicitly:\n\n login(username=creds.username, password=creds.password)\n\nbut 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:\n\n @override\n def login(username, password=None):\n pass\n \n login(kwargs=creds)\n # SAME AS\n login(**creds)\n\n## Package all parameters\n\nYour method can accept `kwargs` as a parameter. If it does, ensure it defaults to `None` so that it\'s not required.\n\n @override\n def login(username, password=None, kwargs=None):\n print(kwargs.get("username"))\n print(kwargs.get("port"))\n\n`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.\n\n >>> creds = {"username": "ekyle", "password": "password123", "port":9000}\n >>> login(**creds)\n ekyle\n 9000\n', long_description_content_type='text/markdown', name='mo-kwargs', packages=["mo_kwargs"], url='https://github.com/klahnakoski/mo-kwargs', - version='3.77.20190', + version='3.82.20197', zip_safe=False ) \ No newline at end of file diff --git a/setuptools.json b/setuptools.json index 59a2416..e09731c 100644 --- a/setuptools.json +++ b/setuptools.json @@ -9,7 +9,7 @@ ], "description": "More KWARGS! Let call parameters override kwargs", "include_package_data": true, - "install_requires": ["mo-dots>=3.77.20190", "mo-future>=3.71.20168", "mo-logs>=3.75.20189"], + "install_requires": ["mo-dots>=3.78.20194", "mo-future>=3.71.20168"], "license": "MPL 2.0", "long_description": { "$concat": [ @@ -121,6 +121,6 @@ "name": "mo-kwargs", "packages": ["mo_kwargs"], "url": "https://github.com/klahnakoski/mo-kwargs", - "version": "3.77.20190", + "version": "3.82.20197", "zip_safe": false } \ No newline at end of file