Converter

Option_merge includes functionality to convert values on access.

This functionality is assisted by the Converter and Converters classes.

Converter is used to encapsulate a single converter, and converters is used to group together multiple converters.

Usage is through the MergedOptions object, which has an instance of Converters on it.

m = MergedOptions.using({"a": 1})
converter = Converter(convert=lambda p, v: v+1, convert_path=['a'])
m.add_converter(converter)

assert m['a'] == 1
m.converters.activate()
assert m['a'] == 2
assert m['a'] == 2

Note that the converters must be activated before use and subsequent calls to converted paths will use a cached result.

Single Converter

class option_merge.converter.Converter(convert, convert_path=None)

Encapsulates a single converter.

It contains the converting logic as well as a list of paths that this conversion should be used against.

It has a method “matches” that is used against each possible path and will check for exact matches against the convert_path.

__call__(path, data)

Proxy the conversion logic in self.convert

matches(path)

Check to see if this converter should be used against this path

Group of converters

class option_merge.converter.Converters

Holds a group of converters.

Has logic to say whether the converters are activated.

Also memoizes the results of conversion.

__iter__()

Iterate through the converters

Yield nothing if not activated yet

converted(path)

Return whether this path has been converted yet

converted_val(path)

Return the converted value for this path

This function should be guarded via the use of self.converted

done(path, value)

Mark a path as been replaced by the specified value

started(path)

Mark this path as waiting

waiting(path)

Return whether we’re waiting for this path