Skip to main content

Camel Converter

Test 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 package 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 dictionary'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.

  • Class base model to add methods

Inheriting the Converter adds to_camel and from_camel methods to your class

from camel_converrter import Converter

class SomeClass(Converter):
    some_value = "some value"

    def __init__(self) -> None:
        self.another_value = "another value"

example = SomeClass()
print(example)

This will print {"someValue": "some value", "anotherValue": "another value"}

from camel_converrter import Converter

class SomeClass(Converter):
    def __init__(self, some_value: str) -> None:
        self.some_value = "some value"

example = SomeClass.from_camel({"someValue": "some value"})
print(example.some_value)

This will print some value

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 interested in contributing to this project please see our contributing guide

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

camel_converter-5.1.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: camel_converter-5.1.1.tar.gz
  • Upload date:
  • Size: 50.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for camel_converter-5.1.1.tar.gz
Algorithm Hash digest
SHA256 fd601b343695025e9d4ec316845d1fe6011cd2ded763397032feb1ef74855b7e
MD5 9e7c88fa6bd8dab589fe2959ef670a83
BLAKE2b-256 6ffb58f39ce79ec56e480ac86e4c6a7758d2ee7503c7e9c6f91f535dcbae81da

See more details on using hashes here.

File details

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

File metadata

  • Download URL: camel_converter-5.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for camel_converter-5.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7cf028484efd7cf0200a05812307e97e83f161af256552e8dfe5833150bab21c
MD5 cf94808b6328bb4fbec8013210274bf8
BLAKE2b-256 c99ffcfe35d042eb4405d158aaba613dc712f18350c5e6ea3e2376c8a8dcbe10

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

5.1.1 This release

2 files

5.1.0

2 files

5.0.1

2 files

5.0.0

2 files

4.0.1

2 files

4.0.0

2 files

3.1.2

2 files

3.1.1

2 files

3.1.0

2 files

3.0.3

2 files

3.0.2

2 files

3.0.1

2 files

3.0.0

2 files

2.0.1

2 files

2.0.0

2 files

1.0.7

2 files

1.0.6

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.2.0.post1

2 files

0.2.0

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page