Uilities
Utility functions for Django models and the dictionaries that are applied to them.
This module provides utility functions for working with Django ORM base models and related data structures. It includes helpers for validating string values, cleaning dictionary keys, and transforming lists of dictionaries.
Main Features:
dict_key_cleaner(): Cleans a string key by removing line breaks and tabs, and replacing spaces with underscores.dict_keys_to_list(): Recursively extracts all keys from a nested dictionary into a flat list.list_of_dicts_to_list(): Converts a list of dictionaries into a list of the cleaned values of their first key.list_of_dicts_to_dict(): Converts a list of dictionaries into one dictionary of those cleaned values.assign_changed_fields()andtags_changed(): Compare an ORM instance with the values to apply to it.
These utilities are intended to simplify common data manipulation tasks in Django-based applications, especially when dealing with dynamic or nested data structures.
- smarter.lib.django.models.utils.assign_changed_fields(instance, data)[source]
Set the fields of an ORM instance whose values differ from the given ones.
A caller that applies a manifest saves the instance only when this returns a field. Every deploy applies the built-in manifests again, and saving an unchanged row moves its
updated_at, sendspost_saveto every receiver, and invalidates its caches. A value that compares unequal to the stored one only because of its type, e.g. a float and a Decimal, counts as changed, so the instance is saved, as it always was.
- smarter.lib.django.models.utils.dict_key_cleaner(key)[source]
Clean a key by removing line breaks and tabs, and replacing spaces with underscores.
This function removes newline (
\n), carriage return (\r) and tab (\t) characters from the input string, and replaces its spaces with underscores (_).Example:
dict_key_cleaner("my key\nwith\tspaces") # returns: 'my_keywithspaces'
- smarter.lib.django.models.utils.dict_keys_to_list(data, keys=None)[source]
Recursively extract all keys from a nested dictionary.
This function traverses a dictionary and all nested dictionaries, collecting every key encountered into a flat list.
- Parameters:
- Return type:
- Returns:
A list of all keys found in the dictionary and its nested dictionaries.
Example:
data = { "a": 1, "b": {"c": 2, "d": {"e": 3}} } dict_keys_to_list(data) # returns: ['a', 'b', 'c', 'd', 'e']
- smarter.lib.django.models.utils.list_of_dicts_to_dict(data)[source]
Convert a list of dictionaries into one dict, keyed by the cleaned values of the first dictionary’s first key.
This function takes the first key of the first dictionary, and, for each dictionary that has that key, maps its value, cleaned with
dict_key_cleaner(), to the original value.- Parameters:
- Return type:
- Returns:
A dictionary of cleaned values to original values, or None if the list is empty or doesn’t start with a dict.
Example:
data = [{"name": "Alice"}, {"name": "Bob"}] list_of_dicts_to_dict(data) # returns: {'Alice': 'Alice', 'Bob': 'Bob'}
- smarter.lib.django.models.utils.list_of_dicts_to_list(data)[source]
Convert a list of dictionaries into a list of the cleaned values of the first dictionary’s first key.
This function takes the first key of the first dictionary, and, for each dictionary that has that key, cleans its value with
dict_key_cleaner(). It logs a warning each time it runs.- Parameters:
- Return type:
- Returns:
A list of cleaned values, or None if the list is empty or doesn’t start with a dict.
Example:
data = [{"name": "Alice"}, {"name": "Bob"}] list_of_dicts_to_list(data) # returns: ['Alice', 'Bob']