pyhumps Documentation

repository·master·Indexed 18 days ago

https://github.com/nficano/humps

A Python utility for converting strings and dictionary keys between snake_case, camelCase, PascalCase, and kebab-case. Version 3.8.0 provides functions such as camelize, decamelize, pascalize, and kebabize, which support recursive conversion for dictionaries and lists. It includes boolean check functions (e.g., is_snakecase) and specialized logic to handle acronyms and abbreviations during transformation.

Tokens
2.9K
Snippets
16
Records
17
Agent score
61%

What's inside pyhumps

  1. Install pyhumps from source

    master

    You can install pyhumps by cloning the repository or downloading the tarball. Once you have the source code locally, navigate to the directory and install it using pipenv install . to add it to your site-packages or embed it in your package.

    $ git clone git://github.com/nficano/humps.git
    $ cd humps
    $ pipenv install .
  2. How humps handles acronyms and abbreviations

    master
    Humps includes logic to handle acronyms and initialisms during conversion to prevent incorrect splitting. For example, when converting APIResponse to snake case, the internal _fix_abbreviations function ensures the result is api_response rather than a_p_i_response. This is achieved using the ACRONYM_RE regex to identify and title-case acronyms before the main transformation occurs.
  3. Create a Boto3 API Wrapper using humps

    master

    You can use humps to bridge the gap between Pythonic snake_case arguments and the PascalCase requirements often found in AWS/Boto3 responses or specific API structures.

    In the example below, pascalize is used to transform keyword arguments before passing them to the Boto3 client, and decamelize (or a similar transformation) can be used to clean up the response.

    # 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)
        # Note: The example uses depascalize, ensure you use the correct humps function
        return (humps.decamelize(response) if decamelize else response)
    
    # usage
    api("s3:download_file", bucket="bucket", key="hello.png", filename="hello.png")
  4. Recursively convert dictionary keys

    master

    The decamelize function (and other conversion functions) supports recursive conversion of dictionary keys. This works for single dictionaries as well as lists containing dictionaries.

    import humps
    
    # Converting keys in a list of dictionaries
    humps.decamelize([{'downTheRoad': True}])         # [{'down_the_road': True}]
  5. Convert dictionary keys in lists or dictionaries

    master

    The conversion functions (camelize, decamelize, kebabize, pascalize) can be applied to collections (like lists of dictionaries) to transform all keys within them.

    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"}]
  6. Check string casing with is_* functions

    master

    Verify if a string follows a specific casing convention using these boolean functions:

    • is_camelcase(string)
    • is_pascalcase(string)
    • is_snakecase(string)
    • is_kebabcase(string)
    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
  7. Convert strings between different casing styles

    master

    The humps library provides functions to convert strings between snake_case, camelCase, PascalCase, and kebab-case.

    Key string conversion functions include:

    • decamelize(string): Converts camelCase or PascalCase to snake_case.
    • camelize(string): Converts snake_case to camelCase.
    • kebabize(string): Converts snake_case to kebab-case.
    • pascalize(string): Converts snake_case to PascalCase.
    • dekebabize(string): Converts kebab-case to snake_case.
    import humps
    
    humps.decamelize('illWearYourGranddadsClothes')   # 'ill_wear_your_granddads_clothes'
    humps.camelize('i_look_incredible')               # 'iLookIncredible'
    humps.kebabize('i_look_incredible')               # 'i-look-incredible'
    humps.pascalize('im_in_this_big_ass_coat')        # 'ImInThisBigAssCoat'
    humps.decamelize('FROMThatThriftShop')            # 'from_that_thrift_shop'
    humps.dekebabize('FROM-That-Thrift-Shop')         # 'FROM_That_Thrift_Shop'
  8. Convert string casing with humps

    master

    Use the following functions to transform strings between different casing styles:

    • humps.camelize(string): Converts a string to camelCase.
    • humps.decamelize(string): Converts a camelCase or PascalCase string to snake_case.
    • humps.pascalize(string): Converts a string to PascalCase.
    import humps
    
    humps.camelize('jack_in_the_box')  # jackInTheBox
    humps.decamelize('rubyTuesdays')  # ruby_tuesdays
    humps.pascalize('red_robin')     # RedRobin
  9. Convert strings and dictionary keys to snake case with decamelize()

    master

    The decamelize() function converts a string, a dictionary, or a list of dictionaries to snake_case (e.g., helloWorld becomes hello_world). This is also the behavior of depascalize(). It recursively processes keys in dictionaries or elements in lists.

    from humps import decamelize
    
    # String conversion
    print(decamelize("helloWorld"))  # "hello_world"
    
    # Dictionary key conversion
    dict_data = {"userId": 1}
    print(decamelize(dict_data))     # {"user_id": 1}