Code Monkey home page Code Monkey logo

decorated_options's Introduction

Overview

tests build
package PyPI Package latest release PyPI Wheel Supported versions Supported implementations

Function decorator to make argument passing saner.

I often have to write a function which runs a simulation/learning task which I need to run for several different parameters. This initially is manageable, but then slowly configuration creep starts to happen: I keep adding more and more parameters to the functions which run the simulations and keep making my old code more and more fragile.

I wrote decorated_options to decouple the arguments for different set of experiments.

In brief, decorated_options converts this:

def run(max_num_followers, num_segments, is_hawkes):
    # ...
    # ...

# tmp = run_multiple_followers(max_num_followers=10, num_segments=10, is_hawkes=True)
# tmp = run_multiple_followers(max_num_followers=100, num_segments=10, is_hawkes=False)
# tmp = run_multiple_followers(max_num_followers=10, num_segments=50, is_hawkes=True)
tmp = run_multiple_followers(max_num_followers=1000, num_segments=100, is_hawkes=False)

to:

from decorated_options import Options, optioned

@optioned('opts')
def run(max_num_followers, num_segments, is_hawkes):
    # ...
    # ...


opts = Options(max_num_followers=10, num_segments=10, is_hawkes=True)
# tmp = run_multiple_followers(opts=opts)
# tmp = run_multiple_followers(max_num_followers=100, is_hawkes=False, opts=opts)
# tmp = run_multiple_followers(num_segments=50, is_hawkes=False, opts=opts)
tmp = run_multiple_followers(max_num_followers=1000, num_segments=100, is_hawkes=False)
  • Benefits over **kwargs in receiving function:
    1. Early reporting of errors at call-time.
    2. No need to unpack the values.
    3. Default values do not have to be hard-coded.
    4. Allows progressive improvement, no need to change old code which uses positional arguments.
  • Benefits over **dict while calling:
    1. Easier updating/overriding of values
    2. Positional arguments also work
    3. Guaranteed immutability (throws Exceptions on attempted violations.)
  • Benefits over default values in receiving function:
    1. Options objects can save defaults for multiple settings.
    2. De-couples default values from the functions themselves.

Installation

pip install decorated_options

Development

To run the all tests run:

tox

Note, to combine the coverage data from all the tox environments run:

Windows
set PYTEST_ADDOPTS=--cov-append
tox
Other
PYTEST_ADDOPTS=--cov-append tox

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.