Skip to main content

apply_defaults

Apply default values to functions.

Makes configuration easy! Application settings come from a config file into your code cleanly.

pip install apply_defaults

@apply_config

This decorator applies options from a ConfigParser object.

from apply_defaults import apply_config
from configparser import ConfigParser

config = ConfigParser()
config.read_dict({"general": {"option": True}})

@apply_config(config)
def func(option: bool = False) -> bool:
    return option

The option parameter takes the value from the configuration.

>>> func()
True

Override the configuration by passing a value.

>>> func(option=False)
False

If the option is not in the configuration, the default value from the parameter list is used.

>>> config.remove_option("general", "option")
>>> func()
False

ConfigParser's options are strings. Type hints in the function signature allow the apply_config decorator to cast options to the desired type. Alternatively cast the value yourself.

@apply_self

This decorator applies attributes from the bound object.

from apply_defaults import apply_self

class MyObject:
    def __init__(self):
        self.option = True

    @apply_self
    def func(self, option=False):
        return option

The parameter takes the value from the bound object, i.e. self.option.

>>> obj = MyObject()
>>> obj.func()
True

Override by passing a value.

>>> obj.func(option=False)
False

If the attribute is not in the bound object, the default value from the parameter list is used.

>>> del obj.option
>>> obj.func()
False

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.6.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file apply_defaults-0.1.6.tar.gz.

File metadata

  • Download URL: apply_defaults-0.1.6.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.12

File hashes

Hashes for apply_defaults-0.1.6.tar.gz
Algorithm Hash digest
SHA256 3773de3491b94c0fe44310f1a85888389cdc71e1544b343bce0d2bd6991acea5
MD5 62c8c20d46bd5ebad773a98ddfbe1576
BLAKE2b-256 42317daf0d1111b29405d25b7677c8c681f6de7aff19d44b54f57ce5c789af24

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.6 This release

1 file

0.1.5

1 file

0.1.4

1 file

0.1.3

1 file

0.1.2

1 file

0.1.1

1 file

0.1.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page