Skip to main content

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

Project description

Humps logo

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

Jan 11, 2021: An open call for contributors:

Please email me at nficano@gmail.com.

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

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-3.8.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

pyhumps-3.8.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyhumps-3.8.0.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.2 requests/2.28.1 requests-toolbelt/0.10.0 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/4.2.0 keyring/23.9.3 rfc3986/2.0.0 colorama/0.4.5 CPython/3.10.8

File hashes

Hashes for pyhumps-3.8.0.tar.gz
Algorithm Hash digest
SHA256 498026258f7ee1a8e447c2e28526c0bea9407f9a59c03260aee4bd6c04d681a3
MD5 c9cf66913dbcc98513af00035fcbc8e9
BLAKE2b-256 c483fa6f8fb7accb21f39e8f2b6a18f76f6d90626bdb0a5e5448e5cc9b8ab014

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyhumps-3.8.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.2 requests/2.28.1 requests-toolbelt/0.10.0 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/4.2.0 keyring/23.9.3 rfc3986/2.0.0 colorama/0.4.5 CPython/3.10.8

File hashes

Hashes for pyhumps-3.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 060e1954d9069f428232a1adda165db0b9d8dfdce1d265d36df7fbff540acfd6
MD5 d2476e8fb8c0cbedca855e55d70891ee
BLAKE2b-256 9e11a1938340ecb32d71e47ad4914843775011e6e9da59ba1229f181fef3119e

See more details on using hashes here.

Supported by

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