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
myString
intomy_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_func
will 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_string
intomyString
-
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_func
will 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_string
intoMyString
-
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_func
will 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 interesting 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
File details
Details for the file camel_converter-3.0.0.tar.gz
.
File metadata
- Download URL: camel_converter-3.0.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1023-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f200e1d1067245f39ae2df6c547dc3de8619060012679702f6471187280e6eb |
|
MD5 | 4eb0a2a97d4cd65f32a676a8e5459cca |
|
BLAKE2b-256 | 677492806bf240c3e2ce6cdddce5b7661f33d1bcc45f8d38dc3a1b2633e04134 |
File details
Details for the file camel_converter-3.0.0-py3-none-any.whl
.
File metadata
- Download URL: camel_converter-3.0.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1023-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b01725c8ccf918752436a8aab595fa153c5123c147225434bf1f40041acb3c7 |
|
MD5 | 6d6f9062896b44e54e491ff0cdf676c8 |
|
BLAKE2b-256 | 10557e40c775a08118a9440274e801d8fa6d85c25e24c0b913c347c7b4939563 |