Utils - Conversion
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.to_camel_case(data, convert_values=False, is_recursive=False)[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.
- Raises:
SmarterValueError – If the input is not a string, dictionary, or list, and cannot be converted.
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)[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 function attempts to convert its string representation.
- Raises:
SmarterValueError – If the input is not a string, dictionary, or list, and cannot be converted.
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’]