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 pre-commit.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.3.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

camel_converter-0.1.3-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: camel-converter-0.1.3.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.4 Linux/5.4.0-1046-azure

File hashes

Hashes for camel-converter-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e29b2640b5d8d648e1a698ef622152e8c921ce748b00ac9fb9c0f982ee75e489
MD5 143e9533089737c80908328ba0bc0777
BLAKE2b-256 424f2553abee0a1d66237a08ce0e7639e59ae56614dafb80c11a12c1ad9ec077

See more details on using hashes here.

File details

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

File metadata

  • Download URL: camel_converter-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.4 Linux/5.4.0-1046-azure

File hashes

Hashes for camel_converter-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 026fc5128c3f2a9fd30838bae8f34b9a6eaa4f74292104a1459ddde0644470d4
MD5 caa590adb6d444fbd8514c5c86d63a7c
BLAKE2b-256 ecc05a7252e29d162ae527c03f9bee7b7b468b7c8afe09416b3ee1bb57b8ec39

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