Converts a string from snake case to camel case or camel case to snake case
Project description
Camel Converter
In JSON keys are frequently in camelCase format, while variable names in Python are typically snake_case. The purpose of this pacakgae is to help convert between the two formats.
Usage
-
To convert from camel case to snake case:
from camel_converter import to_snake snake = to_snake("myString")
This will convert
myStringintomy_string -
To convert a dictonary's keys from camel case to snake case:
from camel_converter import dict_to_snake snake = dict_to_snake({"myString": "val 1"})
This will convert
{"myString": "val 1"}into{"my_string": "val 1"}. Non-string keys will be left unchanged.This is also available as a decorator for functions that return a dictionary.
from camel_converter.decorators import dict_to_snake @dict_to_snake def my_func() -> dict[str, str]: return {"myString": "val 1"} snake = my_func()
my_funcwill return{"my_string": "val 1"}. Non-string keys will be left unchanged. -
To convert from snake case to camel case:
from camel_converter import to_camel camel = to_camel("my_string")
This will convert
my_stringintomyString -
To convert from a dictionary's keys from snake case to camel case:
from camel_converter import dict_to_camel camel = to_camel({"my_string": "val 1"})
This will convert
{"my_string": "val 1"}into{"myString": "val 1"}Non-string keys will be left unchanged.This is also available as a decorator for functions that return a dictionary.
from camel_converter.decorators import dict_to_camel @dict_to_camel def my_func() -> dict[str, str]: return {"my_string": "val 1"} camel = my_func()
my_funcwill return{"myString": "val 1"}. Non-string keys will be left unchanged. -
To convert from snake to pascal case:
from camel_converter import to_pascal pascal = to_pascal("my_string")
This will convert
my_stringintoMyString -
To convert from a dictionary's keys from snake case to pascal case:
from camel_converter import dict_to_pascal pascal = to_pascal({"my_string": "val 1"})
This will convert
{"my_string": "val 1"}into{"MyString": "val 1"}Non-string keys will be left unchanged.This is also available as a decorator for functions that return a dictionary.
from camel_converter.decorators import dict_to_pascal @dict_to_pascal def my_func() -> dict[str, str]: return {"my_string": "val 1"} pascal = my_func()
my_funcwill return{"MyString": "val 1"}. Non-string keys will be left unchanged.
Optional Extras
An optional extra is provided for Pydantic that provides a base class to automatically convert between snake case and camel case. To use this Pydantic class install camel converter with:
pip install camel-converter[pydantic]
Then your Pydantic classes can inherit from CamelBase.
from camel_converter.pydantic_base import CamelBase
class MyModel(CamelBase):
test_field: str
my_data = MyModel(**{"testField": "my value"})
print(my_data.test_field)
will result in my value being printed.
With setting up your model in this way myField from the source, i.e. JSON data, will map to my_field in your model.
Contributing
If you are interested in contributing to this project please see our contributing guide
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file camel_converter-5.0.0.tar.gz.
File metadata
- Download URL: camel_converter-5.0.0.tar.gz
- Upload date:
- Size: 58.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.24
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97d0cf15a75b40abab288c526c71143b468376dd61690fd1cb0a49d9e471f111
|
|
| MD5 |
e55792218dce1ddbb33e29455a94d64f
|
|
| BLAKE2b-256 |
a5ddf8df35e1d2b6360a27c1613937bde2f68db41b8a67cd856f105d2031a3ed
|
File details
Details for the file camel_converter-5.0.0-py3-none-any.whl.
File metadata
- Download URL: camel_converter-5.0.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.24
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a64900bcba1ca047c504ee88cefa905a5bcac15cec06f6d6f9d1ebad46d4e80
|
|
| MD5 |
ed8cbc49073a034be4e16732fe0f3305
|
|
| BLAKE2b-256 |
5167283543fc3ac6d6d8e9cd710e331ebf2323a284f66be304ac786c3b7bdeb4
|