项目作者: sanders41

项目描述 :
Converts a string from snake case to camel case or camel case to snake case
高级语言: Python
项目地址: git://github.com/sanders41/camel-converter.git
创建时间: 2021-04-18T12:59:16Z
项目社区:https://github.com/sanders41/camel-converter

开源协议:MIT License

下载


Camel Converter

Test 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:

    1. from camel_converter import to_snake
    2. snake = to_snake("myString")

    This will convert myString into my_string

  • To convert a dictonary’s keys from camel case to snake case:

    1. from camel_converter import dict_to_snake
    2. 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.

    1. from camel_converter.decorators import dict_to_snake
    2. @dict_to_snake
    3. def my_func() -> dict[str, str]:
    4. return {"myString": "val 1"}
    5. 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:

    1. from camel_converter import to_camel
    2. 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:

    1. from camel_converter import dict_to_camel
    2. 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.

    1. from camel_converter.decorators import dict_to_camel
    2. @dict_to_camel
    3. def my_func() -> dict[str, str]:
    4. return {"my_string": "val 1"}
    5. camel = my_func()

    my_func will return {"myString": "val 1"}. Non-string keys will be
    left unchanged.

  • To convert from snake to pascal case:

    1. from camel_converter import to_pascal
    2. 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:

    1. from camel_converter import dict_to_pascal
    2. 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.

    1. from camel_converter.decorators import dict_to_pascal
    2. @dict_to_pascal
    3. def my_func() -> dict[str, str]:
    4. return {"my_string": "val 1"}
    5. 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:

  1. pip install camel-converter[pydantic]

Then your Pydantic classes can inherit from CamelBase.

  1. from camel_converter.pydantic_base import CamelBase
  2. class MyModel(CamelBase):
  3. test_field: str
  4. my_data = MyModel(**{"testField": "my value"})
  5. 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