Convert strings (and dictionary keys) between snake case, camel case and pascal case in Python. Inspired by Humps for Node
  • Python 96.3%
  • Makefile 3.7%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2023-06-01 11:47:41 +02:00
.github Dropped support for Python 3.6 (EOL) 2022-10-21 06:31:04 -04:00
artwork Add GitHub social art 2019-08-20 08:49:28 -04:00
docs Added kebab-case to documentation 2022-06-19 13:26:02 +02:00
humps decamelize: ignore numeric characters 2023-06-01 11:47:41 +02:00
tests allow acronyms ending in digits 2022-06-26 03:17:51 +05:30
.envrc Migrate to Poetry 2021-12-29 13:23:09 -05:00
.gitignore Removed .idea/ files. 2021-05-01 14:39:06 -05:00
.pre-commit-config.yaml removed conflict linter w/ black 2021-05-05 15:38:37 -04:00
.pylintrc Add pylint (#211) 2021-10-14 07:50:41 -07:00
.readthedocs.yaml Add readthedocs config (#217) 2021-10-14 07:41:57 -07:00
LICENSE Migrate to Poetry 2021-12-29 13:23:09 -05:00
Makefile Update workflow/Makefile to use poetry (#237) 2022-01-03 10:12:16 -08:00
MANIFEST.in added manifest file 2018-10-18 18:23:07 -04:00
Pipfile.lock Bump sphinx from 4.3.1 to 4.3.2 2021-12-20 10:13:39 +00:00
poetry.lock update dependencies 2022-10-21 06:20:50 -04:00
pyproject.toml Bump version: 3.7.3 → 3.8.0 2022-10-21 06:38:55 -04:00
README.md Update README.md 2022-10-30 08:11:42 -04:00
setup.cfg Bump version: 3.7.3 → 3.8.0 2022-10-21 06:38:55 -04:00
setup.py Bump version: 3.7.3 → 3.8.0 2022-10-21 06:38:55 -04:00

Humps logo

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

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
humps.kebabize("white_castle")  # white-castle

Converting dictionary keys

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.kebabize(array)  # [{'attr-one': 'foo'}, {'attr-one': 'bar'}]

array = [{"attr_one": "foo"}, {"attr_one": "bar"}]
humps.pascalize(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_kebabcase('from-that-thrift-shop')  # True
humps.is_camelcase("down_the_road")  # False

humps.is_snakecase("imGonnaPopSomeTags")  # False
humps.is_kebabcase('only_got_twenty_dollars_in_my_pocket')  # False


# what about abbrevations, acronyms, and initialisms? No problem!
humps.decamelize("APIResponse")  # api_response

Cookbook

Pythonic Boto3 API Wrapper

# 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)

# usage
api("s3:download_file", bucket="bucket", key="hello.png", filename="hello.png")