mo-kwargs-7.685.25166
7.685.25166
7.678.25061
7.672.25036
7.671.25036
7.659.25005
7.647.24166
7.632.24139
7.631.24139
7.627.24136
7.623.24125
7.622.24125
7.621.24125
7.615.24125
7.614.24125
7.613.24121
7.611.24120
7.606.24115
7.605.24115
7.603.24115
7.602.24115
7.601.24115
7.600.24114
7.596.24113
7.595.24112
7.594.24112
7.593.24112
7.592.24112
7.584.24095
7.582.24095
7.578.24081
7.574.24078
7.570.24076
7.565.24076
7.564.24076
7.563.24076
7.562.24075
7.551.24062
7.550.24062
7.547.24058
7.543.24046
7.541.24038
7.531.24035
7.530.24035
7.527.24034
7.520.24032
7.519.24031
7.518.24031
7.515.24030
7.508.24028
7.502.24024
7.492.24021
7.476.24007
7.475.24004
7.455.23316
7.450.23304
7.440.23265
7.434.23241
7.433.23235
7.417.23168
7.409.23162
7.403.23150
7.378.23124
7.368.23092
7.365.23080
7.358.23070
7.341.23006
7.340.23006
7.685.25166
7.678.25061
7.672.25036
7.671.25036
7.659.25005
7.647.24166
7.632.24139
7.631.24139
7.627.24136
7.623.24125
7.622.24125
7.621.24125
7.615.24125
7.614.24125
7.613.24121
7.611.24120
7.606.24115
7.605.24115
7.603.24115
7.602.24115
7.601.24115
7.600.24114
7.596.24113
7.595.24112
7.594.24112
7.593.24112
7.592.24112
7.584.24095
7.582.24095
7.578.24081
7.574.24078
7.570.24076
7.565.24076
7.564.24076
7.563.24076
7.562.24075
7.551.24062
7.550.24062
7.547.24058
7.543.24046
7.541.24038
7.531.24035
7.530.24035
7.4.21313
7.331.23005
7.310.22362
7.279.22339
7.278.22339
7.271.22339
7.270.22339
7.252.22317
7.251.22317
7.250.22317
7.249.22317
7.238.22316
7.230.22310
7.213.22307
7.173.22126
7.163.22119
7.148.22086
7.142.22057
7.137.22057
7.136.22057
7.132.22039
7.125.22022
7.124.22022
7.114.22021
5.23.21182
5.22.21182
5.18.21182
4.22.21108
3.93.20259
3.88.20218
3.82.20197
3.77.20190
3.54.20059
3.5.19316
3.47.20042
3.38.20029
3.20.19344
3.15.19317
2.53.19239
2.45.19103
2.43.19055
2.32.19026
2.31.19025
2.26.18331
2.18.18240
2.16.18199
2.14.18155
2.13.18154
1.7.18072
1.6.18072
توضیحات
Object destructuring of function parameters for Python!
| ویژگی | مقدار |
|---|---|
| سیستم عامل | - |
| نام فایل | mo-kwargs-7.685.25166 |
| نام | mo-kwargs |
| نسخه کتابخانه | 7.685.25166 |
| نگهدارنده | [] |
| ایمیل نگهدارنده | [] |
| نویسنده | Kyle Lahnakoski |
| ایمیل نویسنده | kyle@lahnakoski.com |
| آدرس صفحه اصلی | https://github.com/klahnakoski/mo-kwargs |
| آدرس اینترنتی | https://pypi.org/project/mo-kwargs/ |
| مجوز | MPL 2.0 |
# More KWARGS!
Object destructuring of function parameters for Python!
[](https://pypi.org/project/mo-kwargs/)
[](https://github.com/klahnakoski/mo-kwargs/actions/workflows/build.yml)
[](https://coveralls.io/github/klahnakoski/mo-kwargs?branch=dev)
[](https://pepy.tech/project/mo-kwargs)
## Motivation
Javascript has [object destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#object_destructuring), and it can be used for function parameters. This has a couple of benefts over Python's keyword arguments:
* Extra caller parameters are ignored (eg `f({a, b, c})`)
* Duplicate parameters are handled elegantly (eg `f({a, a})`)
The `mo-kwargs` library provides this functionality with the `@override` decorator, with additional benefits:
* required parameters throw an error if missing, just like regular Python
* all parameters, even ones not in the argument list, are passed in the optional `kwargs` parameter
The `@override` decorator adds a `kwargs` argument which can be passed a dict of call parameters; but unlike `**kwargs`, it will not raise duplicate key exceptions.
## Provide default values
We decorate the `login()` function with `@override`. `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
نیازمندی
| مقدار | نام |
|---|---|
| - | mo-dots==10.685.25166 |
| xtr | mo-testing>=8.674.25037; |
| xtr | mo-times>=5.672.25036; |
| xtr | mo-json>=6.672.25036; |
| xtr | mo-threads>=6.672.25036; |
نحوه نصب
نصب پکیج whl mo-kwargs-7.685.25166:
pip install mo-kwargs-7.685.25166.whl
نصب پکیج tar.gz mo-kwargs-7.685.25166:
pip install mo-kwargs-7.685.25166.tar.gz