Skip to main content

Converts a string from snake case to camel case or camel case to snake case

Project description

Camel Converter

CI Status Coverage PyPI version

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 myString into my_string

  • To convert from snake case to camel case:

    from camel_converter import to_camel
    
    camel = to_camel("my_string")
    

    This will convert my_string into myString

  • 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_string into MyString

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

camel-converter-0.1.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

camel_converter-0.1.1-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file camel-converter-0.1.1.tar.gz.

File metadata

  • Download URL: camel-converter-0.1.1.tar.gz
  • Upload date:
  • Size: 3.4 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

Hashes for camel-converter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c39d7aacb241bc32a95b1f9e3c009984de8426523414e9370d1006c3b1cf68eb
MD5 53b65c70f5127a6b763bc2ca8f9eab99
BLAKE2b-256 cb8239e514a9878a0321650af9387d1ad5a87db49e03365787f043f76d0d4d4e

See more details on using hashes here.

File details

Details for the file camel_converter-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: camel_converter-0.1.1-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

Hashes for camel_converter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eeff2138f484475df2164f0542d632120f2d87624fbd6301e533b6347bdf86a3
MD5 92923df8ed3349682a4a4f7ffe324f8d
BLAKE2b-256 c97f33a18781e0c44b37fdc564441d0059b8dfc628b1c6635b7b9b81c553da3b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page