Conversion Utilities
Smarter.common.utils.conversion
Case conversion utility functions for the Smarter framework.
This module provides functions to convert between different naming conventions, such as camelCase, PascalCase, and snake_case, for strings, dictionary keys, and lists. These utilities assure consistent treatment to/from various case formats.
Functions
to_snake_case(obj): Converts camelCase or PascalCase strings (or class/type objects) to snake_case.
to_camel_case(data, convert_values=False): Converts snake_case strings, dict keys, or lists to camelCase.
Example
from smarter.common.utils import to_snake_case, to_snake_case, to_camel_case
print(to_snake_case("UserProfile")) # Output: user_profile
print(to_snake_case("userName")) # Output: user_name
print(to_camel_case("user_name")) # Output: userName
- smarter.common.utils.conversion.ConvertibleCaseType
A type alias representing data that can be converted between different case.
formats. This includes strings, dictionaries with string keys, lists of such elements, or any object.
- smarter.common.utils.conversion.search_replace(data, replace_str, with_str)[source]
Recursively search through a dictionary or list and replace all occurrences of a specified string with another string.
- Return type:
- Parameters:
- Returns:
A new dictionary or list with the specified replacements made.
- Return type:
Notes
This function will recursively search through nested dictionaries and lists within the input dictionary.
Only string keys and string values will be checked for replacements. Non-string types will be left unchanged.
This is particularly useful for handling legacy datauration formats where certain keys or values need to be updated for compatibility reasons, such as replacing ‘chatbot’ with ‘llmclient’ in prompt datauration dictionaries to maintain
· compatibility with older versions of the React app that expect ‘chatbot’ instead of ‘llmclient’.
- smarter.common.utils.conversion.to_camel_case(data, convert_values=False, is_recursive=True)[source]
Convert snake_case strings, dictionary keys, or lists to camelCase format.
- Return type:
- Parameters:
- Returns:
The converted data in camelCase format. The return type matches the input type (str, dict, or list).
- Return type:
Any
Notes
For dictionaries, only keys are converted by default. If
convert_valuesis True, string values are also converted.Nested dictionaries and lists are processed recursively.
If the input is not a string, dictionary, or list, the original value is returned.
Examples
>>> from smarter.common.utils import to_camel_case
# Convert a string >>> to_camel_case(“user_name”) ‘userName’
# Convert a dictionary >>> data = { … “user_name”: “alice”, … “user_profile”: { … “first_name”: “Alice”, … “last_name”: “Smith” … } … } >>> to_camel_case(data) {‘userName’: ‘alice’, ‘userProfile’: {‘firstName’: ‘Alice’, ‘lastName’: ‘Smith’}}
# Convert a list of strings >>> to_camel_case([“first_name”, “last_name”]) [‘firstName’, ‘lastName’]
# Convert values as well >>> data = {“user_name”: “first_name”} >>> to_camel_case(data, convert_values=True) {‘userName’: ‘firstName’}
- smarter.common.utils.conversion.to_snake_case(data, convert_values=False, is_recursive=True)[source]
Convert camelCase or PascalCase strings, dictionary keys, or lists to snake_case format.
- Return type:
- Parameters:
data (str | dict | list) – The input to convert. Can be a string, a dictionary (with camelCase or PascalCase keys), or a list containing strings or dictionaries.
convert_values (bool, optional) – If True, string values within dictionaries and lists are also converted to snake_case. Default is False.
- Returns:
The converted data in snake_case format. The return type matches the input type (str, dict, or list).
- Return type:
Any
Notes
For dictionaries, only keys are converted by default. If
convert_valuesis True, string values are also converted.Spaces in keys are replaced with underscores.
Multiple consecutive underscores are collapsed into a single underscore.
Nested dictionaries and lists are processed recursively.
If the input is not a string, dictionary, or list, the original value is returned.
Examples
>>> from smarter.common.utils import to_snake_case
# Convert a string >>> to_snake_case(“userName”) ‘user_name’
# Convert a dictionary >>> data = { … “userName”: “alice”, … “userProfile”: { … “firstName”: “Alice”, … “lastName”: “Smith” … } … } >>> to_snake_case(data) {‘user_name’: ‘alice’, ‘user_profile’: {‘first_name’: ‘Alice’, ‘last_name’: ‘Smith’}}
# Convert a list of strings >>> to_snake_case([“firstName”, “lastName”]) [‘first_name’, ‘last_name’]
Caution
key collisions may occur when converting from camelCase to snake_case. For example, “userName” and “user_name” would both convert to “user_name”. In such cases, the last key processed will overwrite previous keys in the resulting dictionary.