Apply values to optional params
Project description
# apply_defaults
Applies a set of values to a function's optional parameters, if nothing was
passed to them.
In a typical function, the passed value takes precedence, and if no value is
passed it takes the default value specified in the parameter list is used.
This adds another layer between those two. So the precedence becomes:
1. Passed values, otherwise
2. The values specified by the decorator, otherwise
3. The default value.
The values can come from the bound object, or a configuration file.
`apply_self` applies attributes from the bound object ("self"):
```python
from apply_defaults import apply_self
class MyObject:
def __init__(self):
self.foo = "foo"
@apply_self
def method(self, foo=None):
return foo
>>> # If 'foo' is not passed, the param is assigned the value of self.foo.
>>> MyObject().method()
'foo'
>>> # Overriding @apply_self by specifying a value for 'foo'.
>>> MyObject().method(foo="bar")
'bar'
```
`apply_config` applies the options from a ConfigParser:
```python
from apply_defaults import apply_config
from configparser import ConfigParser
config = ConfigParser()
@apply_config(config)
def my_func(foo=None)
return foo
>>> # There is no configuration yet, so my_func returns the optional
>>> # parameter's default value.
>>> my_func()
None
>>> config.read_dict({"general": {"foo": "foo"}})
>>> # If 'foo' is not passed, the param is assigned the value of 'foo' in the
>>> # configuration.
>>> my_func()
'foo'
>>> # Override @apply_config by specifying a value for 'foo'.
>>> my_func(foo="bar")
'bar'
```
Applies a set of values to a function's optional parameters, if nothing was
passed to them.
In a typical function, the passed value takes precedence, and if no value is
passed it takes the default value specified in the parameter list is used.
This adds another layer between those two. So the precedence becomes:
1. Passed values, otherwise
2. The values specified by the decorator, otherwise
3. The default value.
The values can come from the bound object, or a configuration file.
`apply_self` applies attributes from the bound object ("self"):
```python
from apply_defaults import apply_self
class MyObject:
def __init__(self):
self.foo = "foo"
@apply_self
def method(self, foo=None):
return foo
>>> # If 'foo' is not passed, the param is assigned the value of self.foo.
>>> MyObject().method()
'foo'
>>> # Overriding @apply_self by specifying a value for 'foo'.
>>> MyObject().method(foo="bar")
'bar'
```
`apply_config` applies the options from a ConfigParser:
```python
from apply_defaults import apply_config
from configparser import ConfigParser
config = ConfigParser()
@apply_config(config)
def my_func(foo=None)
return foo
>>> # There is no configuration yet, so my_func returns the optional
>>> # parameter's default value.
>>> my_func()
None
>>> config.read_dict({"general": {"foo": "foo"}})
>>> # If 'foo' is not passed, the param is assigned the value of 'foo' in the
>>> # configuration.
>>> my_func()
'foo'
>>> # Override @apply_config by specifying a value for 'foo'.
>>> my_func(foo="bar")
'bar'
```
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
apply_defaults-0.1.0.tar.gz
(2.0 kB
view details)
File details
Details for the file apply_defaults-0.1.0.tar.gz
.
File metadata
- Download URL: apply_defaults-0.1.0.tar.gz
- Upload date:
- Size: 2.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8325354bc10cbd5c570cd5112b1f887dd2f267bc2605b15adc2b16a2cef8f134 |
|
MD5 | eeb26f365c75fb732c0534ccc81f21a7 |
|
BLAKE2b-256 | 8ee59dca6b7c4a63f18b2e0d5f8b0c6a016c0625a0ee0c0a929ab23e5ddeae76 |