satpy.decision_tree module

Simple decision tree functionality.

class satpy.decision_tree.DecisionTree(decision_dicts, match_keys, multival_keys=None)[source]

Bases: object

Structure 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_keys provided 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_keys provided 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.

static _convert_query_val_to_hashable(query_val)[source]
_find_match(curr_level, remaining_match_keys, query_dict)[source]

Find a match.

_find_match_if_known(curr_level, remaining_match_keys, query_dict)[source]
_get_query_values(query_dict, curr_match_key)[source]
_indent = '  '
_indented_print(indent_level, msg)[source]
_indented_trace(indent_level, msg)[source]
_print_matched_info(level, decision_info, print_func)[source]
Return type:

None

_print_tree_level(level, curr_level)[source]
add_config_to_tree(*decision_dicts)[source]

Add a configuration to the tree.

any_key = None
find_match(**query_dict)[source]

Find a match.

Recursively search through the tree structure for a path that matches the provided match parameters.

print_tree()[source]

Print the decision tree in a structured human-readable format.

class satpy.decision_tree._DecisionDict(match_key, level)[source]

Bases: dict

Helper 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.

__init__(match_key, level)[source]
_log_trace(msg)[source]
traced_contains(item)[source]
Return type:

bool