Uilities

smarter.lib.django.models.utils

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(key): Cleans a string key by replacing whitespace characters with underscores.

  • dict_keys_to_list(data, keys=None): Recursively extracts all keys from a nested dictionary into a flat list.

  • list_of_dicts_to_list(data): Converts a list of dictionaries into a list of cleaned keys from the first key in each dict.

  • list_of_dicts_to_dict(data): Converts a list of dictionaries into a single dictionary with cleaned keys mapped to their values.

These utilities are intended to simplify common data manipulation tasks in Django-based applications, especially when dealing with dynamic or nested data structures.

copyright:

2024 Smarter, Inc. All rights reserved.

license:

MIT

smarter.lib.django.models.utils.dict_key_cleaner(key)[source]

Clean a key by replacing spaces and whitespace characters with underscores.

This function removes newline (\n), carriage return (\r), tab (\t), and space characters from the input string and replaces them with underscores (_).

Parameters:

key (str) – The string key to clean.

Returns:

The cleaned key with whitespace replaced by underscores.

Return type:

str

Example:

dict_key_cleaner("my key\nwith\tspaces")
# returns: 'my_key_with_spaces'
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) – The dictionary to extract keys from.

  • keys (list, optional) – (Optional) An existing list to append keys to (used for recursion).

Returns:

A list of all keys found in the dictionary and its nested dictionaries.

Return type:

list[str]

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 a single dict with keys extracted from the first key in the first dict.

This function iterates over a list of dictionaries, extracts the value of the first key in each dictionary, cleans it using dict_key_cleaner(), and uses the cleaned key as the key in the resulting dictionary, mapping to the original value.

Parameters:

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

Returns:

A dictionary with cleaned keys mapped to their corresponding values, or None if input is invalid.

Return type:

Optional[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 cleaned keys extracted from the first key in each dict.

This function iterates over a list of dictionaries, extracts the value of the first key in each dictionary, cleans it using dict_key_cleaner(), and returns a list of these cleaned keys.

Parameters:

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

Returns:

A list of cleaned keys, or None if input is invalid.

Return type:

Optional[list[str]]

Example:

data = [{"name": "Alice"}, {"name": "Bob"}]
list_of_dicts_to_list(data)
# returns: ['Alice', 'Bob']