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:

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, sends post_save to 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.

Parameters:
  • instance (Model) – The ORM instance.

  • data (dict[str, Any]) – The values, by field name, e.g. from a broker’s manifest_to_django_orm().

Return type:

list[str]

Returns:

The names of the fields that were set, because their values changed.

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 (_).

Parameters:

key (str) – The string key to clean.

Return type:

str

Returns:

The cleaned key.

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:
  • data (dict[str, Any]) – The dictionary to extract keys from.

  • keys (list[str] | None) – An existing list to append keys to, used for recursion.

Return type:

list[str]

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:

data (list[dict[str, Any]]) – A list of dictionaries.

Return type:

dict[str, Any] | None

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:

data (list[dict[str, Any]]) – A list of dictionaries.

Return type:

list[str] | None

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']
smarter.lib.django.models.utils.tags_changed(instance, tags)[source]

Return whether the tags of an ORM instance differ from the given ones.

Parameters:
  • instance (Model) – The ORM instance, which has a tags TaggableManager.

  • tags (Iterable[str] | None) – The tags, e.g. a manifest’s metadata.tags.

Return type:

bool

Returns:

True if the instance is not saved yet, or its tags differ.