Skip to main content

Decorator for fixing naming conventions to keys of keyword arguments - adds trailing underscores to keys using bad naming such as reserved keywords or Python built-ins

Project description

cure

Python package pypi Made with Python Type hinted - mypy validated MIT License Code coverage

Library for adding trailing underscores to passed down keyword arguments from third party libraries. Adds the preferred trailing underscore to the key in the kwarg if the key would conflict with the Python reserved keywords or Python built-ins. Methods can be decorated with the @cure decorator.

Can also be used to convert input keyword arguments to snake case. All in all a decorator to put on your functions which take input from third party libraries which are dependant on user input. In my experience this may happen when working with web frameworks that may apply query values as kwargs or when interfacing with GraphQL libraries that will send user input arguments as kwargs.

As described in PEP 8 -- Style Guide for Python Code (https://www.python.org/dev/peps/pep-0008/):

The following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention):

single_trailing_underscore_: used by convention to avoid conflicts with Python keyword.

Installation with pip

Like you would install any other Python package, use pip, poetry, pipenv or your weapon of choice.

$ pip install cure

Usage and examples

Use the @cure.decorator

import cure

@cure.decorator(cure.KEYWORD_TRAILING_UNDERSCORES)
def my_function(id_=None, username=None, type_=None):
    pass

This function could then be called with the keyword arguments id, username and/or type. Since id and type are either reserved Python keywords or are Python built-ins and shouldn't be used as names, the decorator will add a trailing underscore to the kwargs before passing them into our function my_function.

Convert kwargs to snake case as well

import cure

@cure.decorator(cure.KEYWORD_TRAILING_UNDERSCORES | cure.KEYWORD_SNAKE_CASE_RECURSIVE)
def graphql_resolver(obj, info, resource_id, type_=None):
    pass

This function uses both the options of adding trailing underscores to reserved keywords and built-ins as well as the option of converting input kwargs to snake case. Let's say we're dealing with a GraphQL framework which would otherwise call our function like this, depending on the user input my_function(None, info, resourceId=kwargs["resourceId"], type=kwargs["type"]) or even more likely like my_function(None, info, **kwargs) where kwargs would be a dict holding the keys resourceId and type. Since we want our code to be Pythonic and adhere to proper naming conventions the @cure.decorator can help out with removing the hurdle of converting the kwargs ourself or by jumping through hoops otherwise required.

Available options

  • cure.KEYWORD_TRAILING_UNDERSCORES: Adds trailing underscores to keys in keyword arguments that are using a name that is either a reserved keyword or a Python built-in.
  • cure.KEYWORD_SNAKE_CASE: Converts keys in keyword arguments to snake case.
  • cure.KEYWORD_SNAKE_CASE_RECURSIVE: Converts keys in keyword arguments to snake case. If the keyword argument's value is a dict or a list of dicts it will also convert keys within these to snake case.
  • cure.KEYWORD_CAMEL_CASE: Converts keys in keyword arguments to camel case. This is not recommended, but may be used as a reversal of values converted by the snake case decorator.
  • cure.KEYWORD_CAMEL_CASE_RECURSIVE: Recursive conversion to camel case.

Other functions

The following functions are also available from the module.

cure.is_keyword(kw)
import cure

cure.is_keyword("id")
# True
cure.is_keyword("type")
# True
cure.is_keyword("api")
# False
cure.trail_name(kw)
import cure

cure.trail_name("id")
# "id_"
cure.trail_name("type")
# "type_"
cure.trail_name("api")
# "api"
cure.snake_case_name(kw) and cure.snake_case_dict(input_dict, recursive)
import cure

cure.snake_case_name("apiSecret")
# "api_secret"
cure.snake_case_dict({"user": {"userId": 4711, "userLevel": "ADMIN"}}, recursive=True)
# {'user': {'user_id': 4711, 'user_level': 'ADMIN'}}
cure.camel_case_name(kw) and cure.camel_case_dict(input_dict, recursive)
import cure

cure.camel_case_name("api_secret")
# "apiSecret"
cure.camel_case_dict({"user": {"user_id": 4711, "user_level": "ADMIN"}}, recursive=True)
# {'user': {'userId': 4711, 'userLevel': 'ADMIN'}}

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

cure-0.5.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

cure-0.5.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file cure-0.5.1.tar.gz.

File metadata

  • Download URL: cure-0.5.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for cure-0.5.1.tar.gz
Algorithm Hash digest
SHA256 1ae475a3b47d14392253d822cf4cb0e092d807da4b7e5edec96c02183fa58de7
MD5 48bd007e3d34c2a5d3b02d459aae4313
BLAKE2b-256 9e4489e11c4e058f86ee987bac7d798a617260d08d8dfae63db965190b5cbf16

See more details on using hashes here.

File details

Details for the file cure-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: cure-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for cure-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 601830ffd9fe93d672da3e100eb3cfb95f4d8a5425ae0fff8fbd3864420fab44
MD5 44924cc23a27d59313ec7c1ed34a3170
BLAKE2b-256 01068c54649b2ec286208904702d26815b72663c1510b7598d7b81274aede39f

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 Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page