StringBender
Sub-class of Python str
that adds case conversion functions. They are especially useful for transforming data model field names, ex: REST (camelCase) to Python (snake_case).
Table of Contents
Installation
From the command line:
pip install stringbender
or if you're using poetry:
poetry add stringbender
Functions
StringBender provides the following static functions for converting the case of a specified string:
These helper methods call a corresponding method in the stringbender.String
class and converts the output to str
.
Usage:
from stringbender import camel, kebob, pascal, snake, String
s = "Hasta la vista baby"
print(camel(s))
print(kebob(s)
print(pascal(s))
print(snake(s))
s = String("vote*for*pedro")
print(s.camel())
print(s.replace("*", " ").camel())
s = snake("Careful man, there's a beverage here!", delimiters=[",", "'", "!"])
print(snake(s))
The String Class
stringbender.String
Methods
Optional argument definitions below
camel()
(String) :
Combines all words and he first letter of the first word is lower case, while the first letter of every subsequent word is uppercase.
kebob()
(String)
Creates a hyphen delimited lower-case string.
pascal()
(String)
Combines all words, and capitalizes the first letter of each word.
snake()
(String)
Creates an underscore delimited lower-case string.
as_str()
(str)
Returns the value as a str
. This is the same as str(String(...))
Constants
DEFAULT_DELIMITERS: List[str] = [" ", ".", "-", "_", ":", "\\"]
Optional Method Arguments
delimiters: List[str] = DEFAULT_DELIMITERS
Used to split the string into words.
split_on_first_upper: bool
Splits on the first occurence of a capital letter following a lowercase letter.
title_case: bool
For character-delimited strings (kebob, snake), This will capitalize the first letter of each word.