satpy.decision_tree module
Simple decision tree functionality.
- class satpy.decision_tree.DecisionTree(decision_dicts, match_keys, multival_keys=None)[source]
Bases:
objectStructure to search for nearest match from a set of parameters.
This class is used to find the best configuration section by matching a set of attributes. The provided dictionary contains a mapping of “section name” to “decision” dictionaries. Each decision dictionary contains the attributes that will be used for matching plus any additional keys that could be useful when matched. This class will search these decisions and return the one with the most matching parameters to the attributes passed to the
find_match()method.Note that decision sections are provided as a dict instead of a list so that they can be overwritten or updated by doing the equivalent of a
current_dicts.update(new_dicts).Examples
Decision sections are provided as a dictionary of dictionaries. The returned match will be the first result found by searching provided match_keys in order.
decisions = { 'first_section': { 'a': 1, 'b': 2, 'useful_key': 'useful_value', }, 'second_section': { 'a': 5, 'useful_key': 'other_useful_value1', }, 'third_section': { 'b': 4, 'useful_key': 'other_useful_value2', }, } tree = DecisionTree(decisions, ('a', 'b')) tree.find_match(a=5, b=2) # second_section dict tree.find_match(a=1, b=2) # first_section dict tree.find_match(a=5, b=4) # second_section dict tree.find_match(a=3, b=2) # no match
Init the decision tree.
- Parameters:
decision_dicts (dict) – Dictionary of dictionaries. Each sub-dictionary contains key/value pairs that can be matched from the find_match method. Sub-dictionaries can include additional keys outside the
match_keysprovided to act as the “result” of a query. The keys of the root dict are arbitrary.match_keys (list) – Keys of the provided dictionary to use for matching.
multival_keys (list) – Keys of match_keys that can be provided as multiple values. A multi-value key can be specified as a single value (typically a string) or a set. If a set, it will be sorted and converted to a tuple and then used for matching. When querying the tree, these keys will be searched for exact multi-value results (the sorted tuple) and if not found then each of the values will be searched individually in alphabetical order.
- __init__(decision_dicts, match_keys, multival_keys=None)[source]
Init the decision tree.
- Parameters:
decision_dicts (dict) – Dictionary of dictionaries. Each sub-dictionary contains key/value pairs that can be matched from the find_match method. Sub-dictionaries can include additional keys outside the
match_keysprovided to act as the “result” of a query. The keys of the root dict are arbitrary.match_keys (list) – Keys of the provided dictionary to use for matching.
multival_keys (list) – Keys of match_keys that can be provided as multiple values. A multi-value key can be specified as a single value (typically a string) or a set. If a set, it will be sorted and converted to a tuple and then used for matching. When querying the tree, these keys will be searched for exact multi-value results (the sorted tuple) and if not found then each of the values will be searched individually in alphabetical order.
- _build_tree(conf)[source]
Build the tree.
Create a tree structure of dicts where each level represents the possible matches for a specific
match_key. When finding matches we will iterate through the tree matching each key that we know about. The last dict in the “tree” will contain the configure section whose match values led down that path in the tree.See
DecisionTree.find_match()for more information.
- _indent = ' '
- any_key = None
- class satpy.decision_tree._DecisionDict(match_key, level)[source]
Bases:
dictHelper class for debugging decision tree choices.
At the time of writing this class does not do anything extra to the behavior of what choices are made. It is only a record keeper to make log messages and debugging operations easier. A simple dict should be useable in its place.