# Camel Converter
[![CI Status](https://github.com/sanders41/camel-converter/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/sanders41/camel-converter/actions?query=workflow%3CI+branch%3Amain+event%3Apush)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/sanders41/camel-converter/main.svg)](https://results.pre-commit.ci/latest/github/sanders41/camel-converter/main)
[![Coverage](https://codecov.io/github/sanders41/camel-converter/coverage.svg?branch=main)](https://codecov.io/gh/sanders41/camel-converter)
[![PyPI version](https://badge.fury.io/py/camel-converter.svg)](https://badge.fury.io/py/camel-converter)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/camel-converter?color=5cc141)](https://github.com/sanders41/camel-converter)
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:
```py
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:
```py
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.
```py
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:
```py
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:
```py
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.
```py
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:
```py
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:
```py
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.
```py
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](https://pydantic-docs.helpmanual.io/) that provides a
base class to automatically convert between snake case and camel case. To use this Pydantic class
install camel converter with:
```sh
pip install camel-converter[pydantic]
```
Then your Pydantic classes can inherit from CamelBase.
```py
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](CONTRIBUTING.md)
Raw data
{
"_id": null,
"home_page": "https://github.com/sanders41/camel-converter",
"name": "camel-converter",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "python,pydantic",
"author": "Paul Sanders",
"author_email": "psanders1@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/ae/bc/84ed236a10d61eb5fd45bcfcc82909ec3e700a79161c7089800301ffe56b/camel_converter-3.1.1.tar.gz",
"platform": null,
"description": "# Camel Converter\n\n[![CI Status](https://github.com/sanders41/camel-converter/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/sanders41/camel-converter/actions?query=workflow%3CI+branch%3Amain+event%3Apush)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/sanders41/camel-converter/main.svg)](https://results.pre-commit.ci/latest/github/sanders41/camel-converter/main)\n[![Coverage](https://codecov.io/github/sanders41/camel-converter/coverage.svg?branch=main)](https://codecov.io/gh/sanders41/camel-converter)\n[![PyPI version](https://badge.fury.io/py/camel-converter.svg)](https://badge.fury.io/py/camel-converter)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/camel-converter?color=5cc141)](https://github.com/sanders41/camel-converter)\n\nIn JSON keys are frequently in camelCase format, while variable names in Python are typically\nsnake_case. The purpose of this pacakgae is to help convert between the two formats.\n\n## Usage\n\n- To convert from camel case to snake case:\n\n ```py\n from camel_converter import to_snake\n\n snake = to_snake(\"myString\")\n ```\n\n This will convert `myString` into `my_string`\n\n- To convert a dictonary's keys from camel case to snake case:\n\n ```py\n from camel_converter import dict_to_snake\n\n snake = dict_to_snake({\"myString\": \"val 1\"})\n ```\n\n This will convert `{\"myString\": \"val 1\"}` into `{\"my_string\": \"val 1\"}`. Non-string keys will be\n left unchanged.\n\n This is also available as a decorator for functions that return a dictionary.\n\n ```py\n from camel_converter.decorators import dict_to_snake\n\n @dict_to_snake\n def my_func() -> dict[str, str]:\n return {\"myString\": \"val 1\"}\n\n snake = my_func()\n ```\n\n `my_func` will return `{\"my_string\": \"val 1\"}`. Non-string keys will be\n left unchanged.\n\n- To convert from snake case to camel case:\n\n ```py\n from camel_converter import to_camel\n\n camel = to_camel(\"my_string\")\n ```\n\n This will convert `my_string` into `myString`\n\n- To convert from a dictionary's keys from snake case to camel case:\n\n ```py\n from camel_converter import dict_to_camel\n\n camel = to_camel({\"my_string\": \"val 1\"})\n ```\n\n This will convert `{\"my_string\": \"val 1\"}` into `{\"myString\": \"val 1\"}` Non-string keys will be\n left unchanged.\n\n This is also available as a decorator for functions that return a dictionary.\n\n ```py\n from camel_converter.decorators import dict_to_camel\n\n @dict_to_camel\n def my_func() -> dict[str, str]:\n return {\"my_string\": \"val 1\"}\n\n camel = my_func()\n ```\n\n `my_func` will return `{\"myString\": \"val 1\"}`. Non-string keys will be\n left unchanged.\n\n- To convert from snake to pascal case:\n\n ```py\n from camel_converter import to_pascal\n\n pascal = to_pascal(\"my_string\")\n ```\n\n This will convert `my_string` into `MyString`\n\n- To convert from a dictionary's keys from snake case to pascal case:\n\n ```py\n from camel_converter import dict_to_pascal\n\n pascal = to_pascal({\"my_string\": \"val 1\"})\n ```\n\n This will convert `{\"my_string\": \"val 1\"}` into `{\"MyString\": \"val 1\"}` Non-string keys will be\n left unchanged.\n\n This is also available as a decorator for functions that return a dictionary.\n\n ```py\n from camel_converter.decorators import dict_to_pascal\n\n @dict_to_pascal\n def my_func() -> dict[str, str]:\n return {\"my_string\": \"val 1\"}\n\n pascal = my_func()\n ```\n\n `my_func` will return `{\"MyString\": \"val 1\"}`. Non-string keys will be\n left unchanged.\n\n### Optional Extras\n\nAn optional extra is provided for [Pydantic](https://pydantic-docs.helpmanual.io/) that provides a\nbase class to automatically convert between snake case and camel case. To use this Pydantic class\ninstall camel converter with:\n\n```sh\npip install camel-converter[pydantic]\n```\n\nThen your Pydantic classes can inherit from CamelBase.\n\n```py\nfrom camel_converter.pydantic_base import CamelBase\n\n\nclass MyModel(CamelBase):\n test_field: str\n\n\nmy_data = MyModel(**{\"testField\": \"my value\"})\nprint(my_data.test_field)\n```\n\nwill result in `my value` being printed.\n\nWith setting up your model in this way `myField` from the source, i.e. JSON data, will map to `my_field` in your model.\n\n## Contributing\n\nIf you are interested in contributing to this project please see our [contributing guide](CONTRIBUTING.md)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Converts a string from snake case to camel case or camel case to snake case",
"version": "3.1.1",
"project_urls": {
"Documentation": "https://github.com/sanders41/camel-converter",
"Homepage": "https://github.com/sanders41/camel-converter",
"Repository": "https://github.com/sanders41/camel-converter"
},
"split_keywords": [
"python",
"pydantic"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "55fd065986b2e7d609f57ad94c3b6f883d375235a68ccf1adfa4976d31f67614",
"md5": "c585468e47b1750bf5150203794d1545",
"sha256": "f9f4ad446e46cc317d612a435b653bcb8c3572f1fb2252b0620c5e4fd3b50ebf"
},
"downloads": -1,
"filename": "camel_converter-3.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c585468e47b1750bf5150203794d1545",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 5704,
"upload_time": "2023-11-27T00:10:35",
"upload_time_iso_8601": "2023-11-27T00:10:35.635778Z",
"url": "https://files.pythonhosted.org/packages/55/fd/065986b2e7d609f57ad94c3b6f883d375235a68ccf1adfa4976d31f67614/camel_converter-3.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "aebc84ed236a10d61eb5fd45bcfcc82909ec3e700a79161c7089800301ffe56b",
"md5": "37a374a487797db820c5845f30bfb375",
"sha256": "73c1e31801d0f7baf08fe2a44e6a712e685496e490cab3cd9ce7222845502ef7"
},
"downloads": -1,
"filename": "camel_converter-3.1.1.tar.gz",
"has_sig": false,
"md5_digest": "37a374a487797db820c5845f30bfb375",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 4933,
"upload_time": "2023-11-27T00:10:36",
"upload_time_iso_8601": "2023-11-27T00:10:36.840488Z",
"url": "https://files.pythonhosted.org/packages/ae/bc/84ed236a10d61eb5fd45bcfcc82909ec3e700a79161c7089800301ffe56b/camel_converter-3.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-27 00:10:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sanders41",
"github_project": "camel-converter",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "camel-converter"
}