From 05a5383060785e27f3efa2b8002c41e1e3a2ce46 Mon Sep 17 00:00:00 2001 From: Jonathon Broughton <760691+jsdbroughton@users.noreply.github.com> Date: Thu, 20 Feb 2025 17:22:02 +0000 Subject: [PATCH] Add 'contains' predicate method (#52) - Introduced a new predicate for checking if a parameter value contains a substring. - Added the corresponding method to handle the logic and error management. --- src/predicates.py | 1 + src/rules.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/predicates.py b/src/predicates.py index 8f3108e..bd05c79 100644 --- a/src/predicates.py +++ b/src/predicates.py @@ -16,4 +16,5 @@ PREDICATE_METHOD_MAP = { "is like": PropertyRules.is_parameter_value_like.__name__, "identical to": PropertyRules.is_identical_value.__name__, "not equal": PropertyRules.is_not_equal_value.__name__, + "contains": PropertyRules.is_parameter_value_containing.__name__, } diff --git a/src/rules.py b/src/rules.py index d6ca8ef..ca21351 100644 --- a/src/rules.py +++ b/src/rules.py @@ -60,6 +60,31 @@ class Rules: class PropertyRules: """A collection of rules for processing parameters in Speckle objects.""" + @staticmethod + def is_parameter_value_contains(speckle_object: Base, parameter_name: str, substring: str) -> bool: + """Checks if parameter value contains the given substring. + + Args: + speckle_object: The Speckle object to check + parameter_name: Name of the parameter to check + substring: The substring to look for + + Returns: + bool: True if the parameter value contains the substring + """ + parameter_value = PropertyRules.get_parameter_value(speckle_object, parameter_name) + if parameter_value is None: + return False + + # Convert both to strings for comparison + try: + parameter_str = str(parameter_value).lower() + substring_str = str(substring).lower() + return substring_str in parameter_str + except (TypeError, ValueError) as e: + print(f"Error in is_parameter_value_contains: {e}") + return False + @staticmethod def normalize_path(path: str) -> str: """Remove technical path prefixes like 'properties' and 'parameters'."""