Git Product home page Git Product logo

animal_case's Introduction

Animal case convert recursively your dict/json keys to camelCase or snakeCase.

CircleCI

Most commonly used to build proxies when we need to create communication between apis that have different syntaxes on their endpoint.

Imagine that you have to make a get request on some endpoint and send a post request to another endpoint with some mutate data comming from the first request but each of there endpoints have differents json key sintaxies, now you have to convert all the keys recursively and the hell begins...fear no more my friend.

Run tests

python pytest -m tests/

Usage:

Converting dict keys recursively

By default parse_keys convert keys to snake_case

from animal_case import parse_keys

my_dict = {
    "firstKey": "first value",
    "secondKey": "second value",
    "thirdKey": [
        {"subThirdKey": 1},
        {"subThirdKey2": 2},
        {"subThirdKey3": [
                {"superDeep": "wow"}
            ]
        }
    ]
}

converted = parse_keys(my_dict)
# output
'''
{
    "first_key": "first value",
    "second_key": "second value",
    "third_key": [
        {"sub_third_key": 1},
        {"sub_third_key2": 2},
        {"sub_third_key3": [
                {"super_deep": "wow"}
            ]
        }
    ]
}
'''
from animal_case import parse_keys
from animal_case.types import CAMEL_CASE

my_dict = {
    "first_key": "first value",
    "second_key": "second value",
    "third_key": [
        {"sub_third_key": 1},
        {"sub_third_key2": 2},
        {"sub_third_key3": [
                {"super_deep": "wow"}
            ]
        }
    ]
}

converted = parse_keys(my_dict, types=CAMEL_CASE)
# output
'''
{
    "firstKey": "first value",
    "secondKey": "second value",
    "thirdKey": [
        {"subThirdKey": 1},
        {"subThirdKey2": 2},
        {"subThirdKey3": [
                {"superDeep": "wow"}
            ]
        }
    ]
}
'''

snake case

from animal_case import to_snake_case

converted = to_snake_case('myRandomString')
print(converted) # output: my_random_string

camel case

from animal_case import to_camel_case

converted = to_camel_case('my_random_string')
print(converted) # output: myRandomString

animal_case's People

Contributors

rafa-acioly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

animal_case's Issues

Fails if key is an int

A dictionary such as:

{1: "abc"}

will fail with this exception:

  File "/env/lib/python3.7/site-packages/animal_case/__init__.py", line 72, in parse_keys
    formatted[key] = parse_keys(value, types)
  File "/env/lib/python3.7/site-packages/animal_case/__init__.py", line 70, in parse_keys
    for key, value in _unpack(formatter(data)):
  File "/env/lib/python3.7/site-packages/animal_case/__init__.py", line 47, in keys_to_camel_case
    return {to_camel_case(key): value for key, value in _unpack(content)}
  File "/env/lib/python3.7/site-packages/animal_case/__init__.py", line 47, in <dictcomp>
    return {to_camel_case(key): value for key, value in _unpack(content)}
  File "/env/lib/python3.7/site-packages/animal_case/__init__.py", line 37, in to_camel_case
    content = value.split('_')
AttributeError: 'int' object has no attribute 'split'

It looks like the the to_camel_case function should return identity value if input value is not a string.

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.