A sub-class of str that provides case conversion functions
Project description
StringBender
Sub-class of Python str
that adds case conversion functions. They are especiially useful for tranforming data model field names, ex: REST (camelCase) to Python (snake_case).
Table of Contents
Installation
From the command line:
pip install stringbender
Convenience Methods
StringBender provides helper methods for converting the case of a specified string:
The return type of each method is stringbender.String
.
These helper methods call a corresponding method in the stringbender.String
class.
The stringbender.String
class derives from str
. Every base method in str has been overridden to return an instance of stringbender.String
resulting in a fluent object.
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
# ================================================================================
# Chaining functions
print(pascal(s).swapcase()) # hASTAlAvISTAbABY
print(pascal(s).swapcase().snake()) # h_ast_alv_ist_ab_aby
# ================================================================================
# 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
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.
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.
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
Built Distribution
Hashes for stringbender-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd8f42bd954cbf0b3be2c0c09588363a819daca264888d69d7570d15c665b2e8 |
|
MD5 | 14e98197e13894031e51f466794b4ce8 |
|
BLAKE2b-256 | 3e3d13550d9c258c5a8996d1444e4cc8fb8863e43fce1e0294a862744d02d2ec |