Skip to main content

🐫 Underscore-to-camelCase converter (and vice versa) for strings and dict keys in Python.

Project description

<p align="center">
<img src="./artwork/humps.png" alt="Humps logo" width="245" height="118">
<div align="center">
<a href=""><img src="" /></a>
<a href=""><img src="" /></a>
<a href=""><img src="" alt="pypi"></a>
<a href=""><img src="" /></a>

Convert strings (and dictionary keys) between snake case, camel case and pascal case in Python. Inspired by [Humps]( for Node.

## Why

When creating an API, the authors will often use the character casing convention that is idiomatic to their backend language, thus forcing consumers developing in a different language (with different style guidelines) to tolerate inconsistent casing styles, hardcode mappings between the two, or lug around some case conversion utility functions.

While none of these are inherently wrong, it would still be nice to have a dependable solution just a few keystrokes away.

## Installation

To install humps, simply use pipenv (or pip, of course):

$ pipenv install pyhumps

## Usage

### Converting strings

import humps

humps.camelize('jack_in_the_box') # jackInTheBox
humps.decamelize('rubyTuesdays') # ruby_tuesdays
humps.pascalize('red_robin') # RedRobin

### Converting dictionary keys

import humps

array = [{'attrOne': 'foo'}, {'attrOne': 'bar'}]
humps.decamelize_keys(array) # [{'attr_one': 'foo'}, {'attr_one': 'bar'}]

array = [{'attr_one': 'foo'}, {'attr_one': 'bar'}]
humps.camelize_keys(array) # [{'attrOne': 'foo'}, {'attrOne': 'bar'}]

array = [{'attr_one': 'foo'}, {'attr_one': 'bar'}]
humps.pascalize_keys(array) # [{'AttrOne': 'foo'}, {'AttrOne': 'bar'}]

### Checking character casing
import humps

humps.is_camelcase('illWearYourGranddadsClothes') # True
humps.is_pascalcase('ILookIncredible') # True
humps.is_snakecase('im_in_this_big_ass_coat') # True
humps.is_camelcase('from_that_thrift_shop') # False
humps.is_snakecase('downTheRoad') # False

### How personally I use humps

#### Pythonic Boto3 API Wrapper

import humps
import boto3

def api(service, decamelize=True, *args, **kwargs):
service, func = service.split(':')
client = boto3.client(service)
kwargs = humps.pascalize_keys(kwargs)
response = getattr(client, func)(*args, **kwargs)
return (depascalize_keys(response) if decamelize else response)

api('s3:download_file', bucket='bucket', key='hello.png', filename='hello.png')

#### Flask-RESTful Adaptive responses

# I will post a code snippet for this soon. It's a decorator that checks if
# the request was arguments were passed as camelcase or snake_case, it then
# rewrites the response to match the consumer's preferred casing style.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pyhumps, version 0.4.0
Filename, size File type Python version Upload date Hashes
Filename, size pyhumps-0.4.0-py3-none-any.whl (5.7 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size pyhumps-0.4.0.tar.gz (4.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page