pyhumps
pyhumps is a Python library that converts strings and dictionary keys between various casing styles, including snake case, camel case, Pascal case, and kebab case. It is inspired by the Humps library for Node.js. The library is actively maintained, with its current version being 3.8.0, and receives regular updates.
Warnings
- gotcha Installing the wrong package: There is an older, unrelated PyPI package also named `humps`. If you install `humps` instead of `pyhumps`, you will get a different, unmaintained library. This will likely result in `AttributeError` when attempting to use functions like `camelize` or `decamelize` from the `pyhumps` API, as they won't exist in the incorrectly installed `humps` module.
- gotcha Shadowing the `humps` module: If you create a Python file named `humps.py` in your project or name a variable `humps`, it can shadow the installed `humps` module. This will cause Python to import your local file/variable instead of the library, leading to unexpected errors or `AttributeError`s.
Install
-
pip install pyhumps
Imports
- humps
import humps
Quickstart
import humps
snake_case_string = "hello_world_example"
camel_case_string = humps.camelize(snake_case_string)
pascal_case_string = humps.pascalize(snake_case_string)
kebab_case_string = humps.kebabize(snake_case_string)
data = [
{"first_name": "John", "last_name": "Doe"},
{"first_name": "Jane", "last_name": "Smith"}
]
camelized_data = humps.camelize(data)
print(f"Original snake_case: {snake_case_string}")
print(f"Camel case: {camel_case_string}")
print(f"Pascal case: {pascal_case_string}")
print(f"Kebab case: {kebab_case_string}")
print(f"Camelized dictionary keys: {camelized_data}")