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 PyPI - Python Version

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

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

  • 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


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-3.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

camel_converter-3.0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file camel_converter-3.0.1.tar.gz.

File metadata

  • Download URL: camel_converter-3.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.17 Linux/5.15.0-1040-azure

File hashes

Hashes for camel_converter-3.0.1.tar.gz
Algorithm Hash digest
SHA256 086d122ffd74d103816a862836fc4e74ae812b5c821f7419ccea9f312b8dee91
MD5 2ec626aaff9a6d8a45894cbb4efdc27f
BLAKE2b-256 523b8f475d8d9c057f45ac0f3f29e574e484f7cef098a5bd3881d55ac67c7d3f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: camel_converter-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.17 Linux/5.15.0-1040-azure

File hashes

Hashes for camel_converter-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07dcaf8b6c1be88a543015fb785603913fd95443d82500851a93b996a486a7e1
MD5 70072ff4c8601ea825acad501b6a04c5
BLAKE2b-256 3b586f614db46730aab963eeb56a2e4ba6b2bf1d84d2f5cb0ab7948e0c22b20a

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