GreaterThan, LLC

StringBender v0.2.0

Sub-class of Python str that adds case conversion functions. They are useful for tranforming data model field names, ex: REST (camelCase) to Python (snake_case) when producing or consuming JSON.


Installation

From the command line:

pip install stringbender

Functions

StringBender provides the following static functions for converting the case of a specified string:

These helper methods call corresponding methods in the stringbender.String class and converts the output to str.

Usage:

from stringbender import camel, kebob, pascal, snake, String


# ================================================================================
# EXAMPLES                           # OUTPUT

s = "Hasta la vista baby"
print(camel(s))                      # hastaLaVistaBaby
print(kebob(s)                       # hasta-la-vista-baby
print(pascal(s))                     # HastaLaVistaBaby
print(snake(s))                      # hasta_la_vista_baby

# ================================================================================
# Using a StringBender function with a built-in function

# Create an instance of stringbender.String:
s = String("vote*for*pedro")

# Check the default output:
print(s.camel())                     # vote*For*Pedro (hmm... this isn't right)

# Pass in a custom delimiter:
print(s.replace("*", " ").camel())   # voteForPedro (Much better!)

# ================================================================================
# Using a list of delimiters
s = snake("Careful man, there's a beverage here!", delimiters=[",", "'", "!"])
print(snake(s))                      # careful_man_there_s_a_beverage_here

stringbender.String

Methods

Optional argument definitions below

Constants

Optional Method Arguments