caput.config#

Configure class attributes using values from a dictionary.

This module to defines strictly typed attributes of a class that can be loaded from an input dictionary. This is particularly useful for loading a class from a YAML document.

Examples#

In this example we set up a class to store information about a person.

>>> class Person(Reader):
...     name = Property(default="Bill", proptype=str)
...     age = Property(default=26, proptype=float, key="ageinyears")

We then extend it to store information about a person with a pet. The configuration will be successfully inherited.

>>> class PersonWithPet(Person):
...     petname = Property(default="Molly", proptype=str)

Let’s create a couple of objects from these classes.

>>> person1 = Person()
>>> person2 = PersonWithPet()

And a dictionary of replacement parameters.

>>> testdict = {"name": "Richard", "ageinyears": 40, "petname": "Sooty"}

First let’s check what the default parameters are:

>>> print(person1.name, person1.age)
Bill 26.0
>>> print(person2.name, person2.age, person2.petname)
Bill 26.0 Molly

Now let’s load the configuration from a dictionary:

>>> person1.read_config(testdict)
>>> person2.read_config(testdict)

Then we’ll print the output to see the updated configuration:

>>> print(person1.name, person1.age)
Richard 40.0
>>> print(person2.name, person2.age, person2.petname)
Richard 40.0 Sooty

Exceptions#

CaputConfigError

There was an error in the configuration.

Classes#

Property

Custom property descriptor that can load values from a given dict.

Reader

A class that allows the values of Properties to be assigned from a dictionary.

SafeLineLoader

YAML loader that tracks line numbers.

Functions#

enum(→ Property)

Property type that accepts only a set of possible values.

float_in_range(→ Property[float])

Property type that tests if its input is within the given range.

list_type(→ Property)

Property type that validates lists against required properties.

logging_config(→ Property[str | dict])

Property type that validates the caput logging config.

utc_time(→ Property[caput.astro.time.TimeLike])

Property type for representing UTC as UNIX time.