GithubHelp home page GithubHelp logo

fagan2888 / apply_defaults Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bcb/apply_defaults

0.0 1.0 0.0 23 KB

Helps pull configuration into a project.

License: MIT License

Python 100.00%

apply_defaults's Introduction

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

apply_defaults's People

Contributors

bcb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.