Cleaners¶
Cleaner classes for cleaning input before conversion and validation.
Creating Cleaners¶
Cleaner classes inherit from the Cleaner
base class. They must be callable, with the __call__
dunder
method taking one parameter, the value. An example of a cleaner to change the input value to lower
case looks like:
class LowerCleaner(Cleaner):
def __init__(self, **kwargs):
# initialize any specific state for the cleaner.
pass
def __call__(self, value):
return value.lower()
Cleaners¶
CapitalizationCleaner¶
-
class
cooked_input.
CapitalizationCleaner
(style='lower')¶ param CAP_STYLE_STR style: (optional) capitalization style to use. Defaults to LOWER_CAP_STYLE
return: the cleaned (capitalized) value rtype: str Capitalize the value using the specified style
The styles are equivalent to the following:
style equivalent string function LOWER_CAP_STYLE
lower UPPER_CAP_STYLE
upper FIRST_WORD_CAP_STYLE
capitalize LAST_WORD_CAP_STYLE
– [1] ALL_WORDS_CAP_STYLE
capwords [1] There is no standard library function for capitalizing the last word in a string. This was added to properly capitalize family names, (e.g. “van Rossum”.) This parameter is dedicated to Colleen, who will be happy to capitalize on getting the last word.
ChoiceCleaner¶
-
class
cooked_input.
ChoiceCleaner
(choices, case_sensitive=True)¶ param List[str] choices: the list of choices to match param bool case_sensitive: (optional) if True (default) matching the choice is case sensitive, otherwise matching is case insensitive return: the cleaned (matched choice from the choices
list) value or the original value if no match is foundrtype: str (type is dependent on the mapped value in choices
but is generally str)Note
The cleaned output uses the same capitalization as the item matched from the choices list regardless of the
case_sensitive
parameter.ChoiceCleaner tries to replace the input value with a single element from a list of choices by finding the unique element starting with the input value. If no single element can be identified, the input value is returned (i.e. no cleaning is performed.) This is a complicated way of saying you can type in the first few letters of an input and the cleaner will return the choice that starts with those letters if it can determined which one it is.
For example:
ChoiceCleaner(choices=['blue', 'brown', 'green'], case_sensitive=True)
will with the following input values would return the following values:
value returns note ‘g’ ‘green’ ‘br’ ‘brown’ ‘blu’ ‘blue’ ‘b’ ‘b’ original value returned as can’t tell between ‘brown’ and ‘blue’ ‘BR’ ‘BR’ original value returned as case of the input does not match [2] [2] Would return “brown” if case_sensitive
is False
RegexCleaner¶
-
class
cooked_input.
RegexCleaner
(pattern, repl, count=0, flags=0)¶ Parameters: - pattern (Pattern[str]) – regular expression to search for
- repl (str) – string to substitute for occurrences of
pattern
- count (int) – (optional) the maximum number of substitutions to perform on the input value. Default is to replace all occurrences
- flags – (optional) flags. Default is no flags. See below for details
Returns: the cleaned (pattern replaced with repl) value
Return type: str
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in the input value by the replacement repl. If the pattern is not found in the input value, the value is returned unchanged.
Count
occurrences, from left to right, are replaced. If count is 0, or not specified, all occurrences are replaced.For more information on regular expressions and the meaning of count and flags. See the re.sub function in the re module in the Python standard library.
RemoveCleaner¶
-
class
cooked_input.
RemoveCleaner
(patterns, count=0)¶ Parameters: - patterns (List[str]) – a list of strings to remove
- count (int) – (optional) the maximum number of substitutions to perform on the input value. Default is to remove all occurrences
Returns: the cleaned (
patterns
removed) valueReturn type: str
Removes all occurrences of any of the strings in the
patterns
list from the input value.
ReplaceCleaner¶
-
class
cooked_input.
ReplaceCleaner
(old, new, count=0)¶ Parameters: - old (str) – string to replace
- new (str) – string to substitute for occurrences of
old
- count (int) – (optional) the maximum number of substitutions to perform on the input value. Default is to replace all occurrences
Returns: the cleaned (
old
replaced withnew
) valueReturn type: str
Replaces occurrences of
old
string withnew
string from the input value. If count is specified the firstcount
occurrences, from left to right, are replaced. If count is 0, or not specified, all occurrences are replaced.
StripCleaner¶
-
class
cooked_input.
StripCleaner
(lstrip=True, rstrip=True)¶ Parameters: - lstrip (bool) – (optional) strips white space from the left side of the value if True (default)
- rstrip (bool) – (optional) strips white space from the right side of the value if True (default)
Returns: the cleaned (stripped) value
Return type: str
Strips white space from the input value. Strips from the left side if lstrip=True, and from the right side if rstrip=True. Both are True by default (i.e. strips from both left and right).