"""Utility functions for the adjustments validation logic.""" import json import math import re from abacus_common_logic.adjustments_validation.logger import logger def clean_upc(upc: str) -> str: """Clean a UPC by removing leading zeros.""" return re.sub('^0', '', upc) def is_integer(value: str) -> bool: """Check if a string represents a valid integer.""" try: if value.isdigit() and not math.isnan(int(value)): return True except (TypeError, ValueError): return False return False def is_float(value: str) -> bool: """Check if a string represents a valid float.""" try: if not math.isnan(float(value)): return True except (TypeError, ValueError): return False return False def has_intersection(setA: set, setB: set) -> bool: """Check if two sets intersect.""" return not setA.isdisjoint(setB) def load_json(value: str | None) -> dict | None: """Try to convert a JSON string into a dict.""" result = None if not value: return result try: result = json.loads(value) except Exception: logger.warning(f'Failed to parse JSON: {value}') pass return result