Camel Converter
Camel Converter is a Python library designed to effortlessly convert strings and dictionary keys between camel case, snake case, and pascal case. It's particularly useful for handling JSON data where keys are often in camelCase, facilitating seamless integration with Python's snake_case conventions. The library also provides decorators for automatic dictionary key conversion in function returns and offers integration with Pydantic models. The current version is 5.1.0.
Warnings
- gotcha The library directly converts strings or dictionary keys. For converting complex nested structures (lists of dictionaries, deeply nested dictionaries), ensure you apply the conversion functions recursively or use tools built for such scenarios.
- gotcha When using `dict_to_snake`, `dict_to_camel`, or `dict_to_pascal`, only string keys are converted. Non-string keys (e.g., integers, tuples) in a dictionary will be left unchanged in the output.
- gotcha The `CamelBase` Pydantic integration requires Pydantic to be installed as an additional dependency. It is not part of the `camel-converter` core package dependencies.
- gotcha The library explicitly requires Python 3.10 or newer. Attempting to install or run it on older Python versions will result in compatibility errors.
Install
-
pip install camel-converter
Imports
- to_snake
from camel_converter import to_snake
- to_camel
from camel_converter import to_camel
- to_pascal
from camel_converter import to_pascal
- dict_to_snake
from camel_converter import dict_to_snake
- dict_to_camel
from camel_converter import dict_to_camel
- dict_to_pascal
from camel_converter import dict_to_pascal
- dict_to_snake (decorator)
from camel_converter.decorators import dict_to_snake
- dict_to_camel (decorator)
from camel_converter.decorators import dict_to_camel
- dict_to_pascal (decorator)
from camel_converter.decorators import dict_to_pascal
- CamelBase
from camel_converter.pydantic_base import CamelBase
Quickstart
from camel_converter import to_snake, to_camel, dict_to_snake
from camel_converter.decorators import dict_to_camel
from pydantic import BaseModel # For CamelBase example
# String conversion
snake_case_string = to_snake('myCamelCaseString')
print(f"'myCamelCaseString' to snake_case: {snake_case_string}")
camel_case_string = to_camel('my_snake_case_string')
print(f"'my_snake_case_string' to camelCase: {camel_case_string}")
# Dictionary key conversion
camel_dict = {'firstName': 'John', 'lastName': 'Doe', 'dateOfBirth': '1990-01-01'}
snake_dict = dict_to_snake(camel_dict)
print(f"CamelCase dict to snake_case: {snake_dict}")
# Decorator usage
@dict_to_camel
def get_data_from_db() -> dict:
# Simulate fetching data with snake_case keys
return {'user_id': 123, 'user_name': 'test_user'}
api_response = get_data_from_db()
print(f"Decorated function output (snake_case to camelCase): {api_response}")
# Pydantic integration (requires 'pydantic' to be installed)
try:
from camel_converter.pydantic_base import CamelBase
class User(CamelBase):
first_name: str
last_name: str
user_data_camel = {'firstName': 'Jane', 'lastName': 'Smith'}
user_obj = User(**user_data_camel)
print(f"Pydantic CamelBase model from camelCase input: {user_obj.first_name} {user_obj.last_name}")
except ImportError:
print("Pydantic not installed. Skipping CamelBase example.")