diff --git a/PATCHES.md b/PATCHES.md new file mode 100644 index 0000000000000000000000000000000000000000..44bc8ee57fe997439c37f4d11bb7126d1b3da67b --- /dev/null +++ b/PATCHES.md @@ -0,0 +1,54 @@ +# Patching System +## Conditions + +**and** - proceeds only if all conditions within return true. + +Arguments: +- `conditions` - array of conditions to evaluate + + +--- + +**or** - proceeds if any conditions within return true. + +Arguments: +- `conditions` - array of conditions to evaluate + + +--- + +**not** - proceeds if the condition within is not true. + +Arguments: +- `condition` - condition to evaluate + + +--- + +**field** - checks a field for a value. + +Arguments: +- `field` - the name of a field in the lesson data +- `value` - the value to compare +- `operator` - the operator to use to compare - one of the following: + - `equals` + - `contains` + - `startswith` + - `endswith` + - `>` greater than + - `<` less than + - `>=` greater then/equal to + - `<=` less than/equal to + + +--- + +**big_query** - finds a certain number of matches across the entire original lesson table +- `conditions` - array of the conditions to match (uses AND matching) +- `matches` - number of matches to count as a success +- `operator` - operator to use on the matches value - one of the following + - `equals` (any data type) + - `>` greater than + - `<` less than + - `>=` greater then/equal to + - `<=` less than/equal to \ No newline at end of file diff --git a/main.py b/main.py index 3721711c12687c0c47d91e29551f17322cf48894..921402bb196db8d9d4aee3af850b38f44deb0ab4 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,7 @@ def eval_lesson_condition(input_lesson: dict, condition: dict) -> bool: """ Evaluates a condition on a lesson. """ - global logger + global logger, original_lessons_table return_value = False if condition["type"] == "and": # Evaluate and condition @@ -78,10 +78,41 @@ def eval_lesson_condition(input_lesson: dict, condition: dict) -> bool: else: # Invalid operator - logger.warn("Invalid operator: %s, returning False.", fld_operator) + logger.warning("Invalid operator: %s, returning False.", fld_operator) + + elif condition["type"] == "big_query": + # Evaluate a big query condition + # Process the condition (all conditions must be true) + q_cond = { + "type": "and", + "conditions": condition["conditions"] + } + + # Define matches + matches = 0 + + # Search through the original lessons table + for lesson in original_lessons_table: + if eval_lesson_condition(lesson, q_cond): + matches += 1 + # Process how many matches we want + match_target = condition["matches"] + if condition["operator"] == "equals": + return_value = matches == match_target + elif condition["operator"] == ">": + return_value = matches > match_target + elif condition["operator"] == "<": + return_value = matches < match_target + elif condition["operator"] == ">=": + return_value = matches >= match_target + elif condition["operator"] == "<=": + return_value = matches <= match_target# + else: + logger.warning("Invalid big query condition operator %s, returning False.", condition["operator"]) + return_value = False else: # Invalid condition type - logger.warn("Invalid condition type %s, returning False.", condition["type"]) + logger.warning("Invalid condition type %s, returning False.", condition["type"]) return return_value @@ -176,6 +207,8 @@ if __name__ == "__main__": lessons_table = input_json["table"] week_lookup_table = input_json["table2"] + original_lessons_table = lessons_table.copy() # Used for big_query + # Open patches patches = json.load(open("patches.json"))