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 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 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 snake to upper camel case:
from camel_converter import to_upper_camel upper_camel = to_upper_camel("my_string")
This will convert
my_stringintoMyString
If you are using Pydantic, a common usage of Pydantic where this package is useful is in FastAPI you can use this package in your models to automatically do the conversion.
from pydantic import BaseModel
from camel_converter import to_camel
class MyModel(BaseModel):
class Config:
alias_generator = to_camel
allow_population_by_field_name = True
my_field: str
With setting up your model in this way myField from the source, i.e. JSON data, will map to my_field in your model.
You can also setup a model to inherit the config settings from so the class Config does not have to be manually set on every model:
from pydantic import BaseModel
from camel_converter import to_camel
class MyBaseModel(BaseModel):
class Config:
alias_generator = to_camel
allow_population_by_field_name = True
class MyModel(MyBaseModel):
my_field: str
class AnotherModel(MyBaseModel):
another_field: int
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
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-0.1.0.tar.gz.
File metadata
- Download URL: camel-converter-0.1.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.4 Linux/5.4.0-1043-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d15647c95362d1cf97ebc30f6d11257e54c4fbdfe67ca4a34b32f615540de7f1
|
|
| MD5 |
d2bd2ccac3b3524b75a36094a2c0555a
|
|
| BLAKE2b-256 |
8a9226171669bd2c0e56a5c4b72c596d6e2a8203d43307904750ec30cb3cce6c
|
File details
Details for the file camel_converter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: camel_converter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.4 Linux/5.4.0-1043-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8720fce8a49da24eecab26122c9ea8781757999d45028b11b68d32176d74788b
|
|
| MD5 |
89051266718a476aff9affbceb322f31
|
|
| BLAKE2b-256 |
598be3fcc1388c35e34f714a35569efbb05a03397d09f234f22831f34ba3ca13
|