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="https://travis-ci.org/nficano/humps"><img src="https://travis-ci.org/nficano/humps.svg?branch=master" /></a>
<a href="https://coveralls.io/github/nficano/humps?branch=master"><img src="https://coveralls.io/repos/github/nficano/humps/badge.svg?branch=master" /></a>
<a href="https://pypi.org/project/pyhumps/"><img src="https://img.shields.io/pypi/v/pyhumps.svg" alt="pypi"></a>
<a href="https://pypi.python.org/pypi/pyhumps/"><img src="https://img.shields.io/pypi/pyversions/pyhumps.svg" /></a>
</div>
</p>


Convert strings (and dictionary keys) between snake case, camel case and pascal case in Python. Inspired by [Humps](https://github.com/domchristie/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):

```bash
$ pipenv install pyhumps
```

## Usage

### Converting strings

```python
import humps

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

### Converting dictionary keys

```python
import humps

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

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

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

### Checking character casing
```python
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
```

<hr>

### How personally I use humps

#### Pythonic Boto3 API Wrapper

```python
# aws.py
import humps
import boto3

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

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

#### Flask-RESTful Adaptive Responses

```python
# I will post a code snippet for this soon. It's a decorator that checks if
# the request 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.

Source Distribution

pyhumps-1.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyhumps-1.0.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file pyhumps-1.0.0.tar.gz.

File metadata

  • Download URL: pyhumps-1.0.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for pyhumps-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ba1472609bb6e953479d40932e5d01f8dc305185f4f99178e6fb93df133b84ee
MD5 bad9d46e0a26f632a0a86e77066128d2
BLAKE2b-256 e7d3953f2e61bd9b2aefe697c95cdbd9119dbaa408f240d95721469f52fce04c

See more details on using hashes here.

File details

Details for the file pyhumps-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyhumps-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for pyhumps-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d979f5524dbb55992f1c9b60fc7e205065ff70de35e8730e748b990864ef09b
MD5 bdd762156f1bab81bd737aa2f1f0608c
BLAKE2b-256 ac098deabe35d83ebdf5a8edbf7cc4ee7d73f38a5a51a5d4cec6ea6dd4a101a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page