from sentry.utils.canonical import CanonicalKeyDict # Create a dictionary with mixed case and inconsistent keys my_dict = {"firstName": "Alice", "last_name": "Smith", "EMAIL": "[email protected]"} # Use CanonicalKeyDict to normalize the keys normalized_dict = CanonicalKeyDict(my_dict) # The keys will now be lower case and underscore-separated print(normalized_dict) # {'email': '[email protected]', 'first_name': 'Alice', 'last_name': 'Smith'}
from sentry.utils.canonical import CanonicalKeyDict # Define two dictionaries with different keys for the same information dict1 = {"First Name": "Alice", "Last Name": "Smith"} dict2 = {"First": "Alice", "Surname": "Smith"} # Use CanonicalKeyDict to normalize the keys in each dictionary normalized_dict1 = CanonicalKeyDict(dict1) normalized_dict2 = CanonicalKeyDict(dict2) # Compare the normalized dictionaries, ignoring key order if normalized_dict1 == normalized_dict2: print("The dictionaries are equivalent") else: print("The dictionaries are different")
from sentry.utils.canonical import CanonicalKeyDict # Define a dictionary with nested dictionaries my_dict = {"person": {"name": "Alice", "age": 30, "location": {"city": "Boston", "state": "MA"}}} # Use CanonicalKeyDict to normalize the keys at all levels normalized_dict = CanonicalKeyDict(my_dict, deep=True) # The keys at all levels will be lower case and underscore-separated print(normalized_dict) # {'person': {'age': 30, 'location': {'city': 'Boston', 'state': 'MA'}, 'name': 'Alice'}}The "sentry.utils.canonical" module is part of the Sentry SDK, a package for error logging and crash handling in Python applications.