Skip to main content

The tool for advanced project configuration with python object injection.

Project description

SmartParams

SmartParams is a lightweight Python framework that simplifies configuration of research projects. Creates an abstraction that warp configurable classes and extracts from them the necessary parameters into a configuration file and then injects them initialization. SmartParams can be easily integrated into existing projects, even if they are very complex. Takes care of loading the configuration file from path given in code or via command line.

Installation

Requirements

  • Python >= 3.8


Install via PyPI   pip install SmartParams.

What is Smart class

Smart class is like as partial from functools but only for classes. Smart object called will instantiate class called with the args and kwargs. If more arguments are supplied to the call, they are merged with initial keyword arguments.

from smartparams import Smart


class SomeClass:
    def __init__(self, a: str, b: str) -> None:
        print(f"Init SomeClass({a=}, {b=})")


smart_some_class = Smart(SomeClass, a="a")
some_class_object = smart_some_class(b="b")

Key features:

  • Wraps class to be partially or fully configurable.
  • Allows setting attributes that depend on other objects.
  • Creates a configuration template file based on class dependencies in the program.
  • Loads configuration from yaml.
  • Checks unset parameters.
  • Checks the type of parameters.
  • Allows overriding configuration from command line.

Basic usage

Assume that we have Class which expects name and class Random. Random expects an integer value which, if not specified, is set randomly from 1 to 100. (The full script can be found in the examples/basic/script.py).

class Random:
    def __init__(self, value: int = 0) -> None:
        self.value = value or randint(1, 100)
        print(f"Init {self}")

    def __repr__(self) -> str:
        return f"Random({self.value=})"


class Class:
    def __init__(self, name: str, random: Random) -> None:
        self.name = name
        self.random = random
        print(f"Init {self}")

    def __repr__(self) -> str:
        return f"Class({self.name=}, {self.random=})"

Our program requires Class with positive random value and another Class with value that is its negative. First, create dataclass with smart classes, then in post_init setup Smart[Class] objects.

@dataclass
class Params:
    positive_class: Smart[Class]
    negative_class: Smart[Class]

    def __post_init__(self) -> None:
        random = self.positive_class.init('random')
        self.negative_class.set('random.value', random.value * -1)
        self.message = self.positive_class.pop('message')
        self.positive_class.map('name', str.upper)

Define main function that expects Smart[Class] object.

def main(smart: Smart[Params]) -> None:
    print("--- Configure Params")
    params = smart()

    print("\n--- Configure PositiveClass")
    params.positive_class()

    print("\n--- Configure NegativeClass")
    params.negative_class(name="negative_class")

    print("\n--- " + params.message)

Create script runner that add abstract for cli and load config file from given path.

if __name__ == '__main__':
    Smart(Params).run(
        function=main,
        path=Path('examples/basic/params.yaml'),
    )

Before running the script, create a params template file with
python -m examples.basic.script --dump,
then edit this file (examples/basic/params.yaml) and set the appropriate values.

positive_class:
  class: __main__.Class:Smart
  name: "positive_class"
  message: "Hello SmartWorld!"
  random:
    class: __main__.Random

negative_class:
  class: __main__.Class:Smart
  random:
    class: __main__.Random

Finally, run script python -m examples.basic.script, optionally modify parameters via cli
by adding positive_class.name="very_positive_class" to command.
For more options use --help argument.

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a merge request.

License

Distributed under the terms of the MIT license, "SmartParams" is free and open source software.

Issues

If you encounter any problems, please email us at mateusz.baran.sanok@gmail.com, along with a detailed description.

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

SmartParams-0.17.0.tar.gz (38.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page