Properties

Central provides properties that are of a specific type, caches its value and can change at runtime, the value provider for properties is a Config object.

Instead of writing code like this to deal with something hard to change at runtime:

from central.config.file import FileConfig

config = FileConfig('config.json')
config.load()

timeout = 10  # default value

try:
    timeout = config.get_int('timeout')
except TypeError:
    # handle format issues

myMethod(timeout)

You can write much cleaner code to take advantage of a type specific property:

from central.config.file import FileConfig

config = FileConfig('config.json')
config.load()

properties = PropertyManager(config)

timeout = properties.get_property('timeout').as_int(10)

myMethod(timeout.get())  # .get may change at runtime and will never raise an error

timeout.get() will never raise an error, in case of format issue it will return the default value which is 10 in this case and it will log a warning.

In addition, you can add callback listener to be notified when the property is changed:

@timeout.on_updated
def timeout_updated(value):
    pass

Properties cache their type specific values, that means multiple calls to timeout.get() will not fetch the value again from the Config object and decode it to the user specific type, instead it caches the last value fetched and decoded from the Config object, when the value is changed in the Config object the property updates itself.

properties = PropertyManager(config)

timeout = properties.get_property('timeout').as_int(10)

timeout.get()  # fetches and decodes the value from the Config object.
timeout.get()  # value is cached, just return it.
timeout.get()  # value is cached, just return it.

That is more performant than using directly a Config object.

config.get_int('timeout', default=10)  # fetches and decodes the value to int (it may raise a format error)
config.get_int('timeout', default=10)  # fetches and decodes the value to int (it may raise a format error)
config.get_int('timeout', default=10)  # fetches and decodes the value to int (it may raise a format error)

results matching ""

    No results matching ""