🐫 Convert strings (and dictionary keys) between snake case, camel case and pascal case in Python. Inspired by Humps for Node
Project description
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice, and every other copyright notice found in this
software, and all the attributions in every file, and this permission notice
shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Description: .. raw:: html
<p align="center">
.. container::
::
<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#cachebust" /></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>
.. raw:: html
</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):
.. code:: bash
$ pipenv install pyhumps
Usage
-----
Converting strings
~~~~~~~~~~~~~~~~~~
.. code:: python
import humps
humps.camelize('jack_in_the_box') # jackInTheBox
humps.decamelize('rubyTuesdays') # ruby_tuesdays
humps.pascalize('red_robin') # RedRobin
Converting dictionary keys
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: 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
~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: 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
.. raw:: html
<hr>
How personally I use humps
~~~~~~~~~~~~~~~~~~~~~~~~~~
Pythonic Boto3 API Wrapper
^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code:: 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code:: 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.
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft
Classifier: Operating System :: POSIX
Classifier: Operating System :: Unix
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python
Classifier: Topic :: Utilities
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice, and every other copyright notice found in this
software, and all the attributions in every file, and this permission notice
shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Description: .. raw:: html
<p align="center">
.. container::
::
<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#cachebust" /></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>
.. raw:: html
</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):
.. code:: bash
$ pipenv install pyhumps
Usage
-----
Converting strings
~~~~~~~~~~~~~~~~~~
.. code:: python
import humps
humps.camelize('jack_in_the_box') # jackInTheBox
humps.decamelize('rubyTuesdays') # ruby_tuesdays
humps.pascalize('red_robin') # RedRobin
Converting dictionary keys
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: 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
~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: 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
.. raw:: html
<hr>
How personally I use humps
~~~~~~~~~~~~~~~~~~~~~~~~~~
Pythonic Boto3 API Wrapper
^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code:: 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code:: 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.
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft
Classifier: Operating System :: POSIX
Classifier: Operating System :: Unix
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python
Classifier: Topic :: Utilities
Project details
Release history Release notifications | RSS feed
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.7.tar.gz
(4.8 kB
view hashes)