Giter Club home page Giter Club logo

camel-converter's Introduction

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

camel-converter's People

Contributors

sanders41 avatar dependabot[bot] avatar pre-commit-ci[bot] avatar viktoriussuwandi avatar yyyaaaccc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.