From 1bda399ce2ed10ea36b4f1119adfe18725fde15c Mon Sep 17 00:00:00 2001 From: Len Wanger Date: Fri, 18 Aug 2017 10:38:45 -0500 Subject: [PATCH] Added CapitalizeCleaner. More cleanups for transition to cooked_input. --- cooked_input/__init__.py | 6 ++++-- cooked_input/cleaners.py | 25 +++++++++++++++++++++++++ cooked_input/test/get_database.py | 12 ++++++------ cooked_input/test/get_ints.py | 6 +++--- cooked_input/test/get_menu.py | 6 +++--- cooked_input/test/get_passwords.py | 6 +++--- cooked_input/test/get_strs.py | 22 +++++++++++++++++----- cooked_input/test/get_table_input.py | 12 ++++++------ cooked_input/test/simple_input.py | 4 ++-- docs/cleaners.rst | 6 +++++- 10 files changed, 74 insertions(+), 31 deletions(-) diff --git a/cooked_input/__init__.py b/cooked_input/__init__.py index 846a2e3..8ff8f79 100644 --- a/cooked_input/__init__.py +++ b/cooked_input/__init__.py @@ -5,10 +5,12 @@ from .io_get_input import get_input, get_table_input, process_value from .io_get_input import TABLE_ID, TABLE_VALUE, TABLE_ID_OR_VALUE -from .convertors import Convertor, IntConvertor, FloatConvertor, BooleanConvertor, ListConvertor, DateConvertor, YesNoConvertor, TableConvertor +from .convertors import Convertor, IntConvertor, FloatConvertor, BooleanConvertor +from .convertors import ListConvertor, DateConvertor, YesNoConvertor, TableConvertor from .validators import Validator, ExactLengthValidator, InLengthValidator, ExactValueValidator, InRangeValidator from .validators import NotInValidator, InChoicesValidator, RegexValidator, PasswordValidator, ListValidator from .validators import validate, in_all, in_any, not_in -from .cleaners import Cleaner, UpperCleaner, LowerCleaner, StripCleaner, ReplaceCleaner +from .cleaners import Cleaner, UpperCleaner, LowerCleaner, CapitalizeCleaner +from .cleaners import StripCleaner, ReplaceCleaner __all__ = ['io_get_input', 'convertors', 'validators', 'cleaners'] diff --git a/cooked_input/cleaners.py b/cooked_input/cleaners.py index 8245379..98d95e8 100644 --- a/cooked_input/cleaners.py +++ b/cooked_input/cleaners.py @@ -8,6 +8,7 @@ """ import sys +from string import capwords #### @@ -50,6 +51,30 @@ def __repr__(self): return 'UpperCleaner()' +class CapitalizeCleaner(Cleaner): + # capitalize the input + def __init__(self, all_words=False, **kwargs): + """ + Capitalize the value + + :param all_words: capitalize all of the words of the value if True, if False, only capitalize the first word. + :param kwargs: + """ + self.all_words = all_words + super(CapitalizeCleaner, self).__init__(**kwargs) + + def __call__(self, value): + if self.all_words: + result = value.capitalize() + else: + result = capwords(value) + + return result + + def __repr__(self): + return 'CapitalizeCleaner(all_words={})'.format(self.all_words) + + class StripCleaner(Cleaner): # strip off white space def __init__(self, lstrip=True, rstrip=True, **kwargs): diff --git a/cooked_input/test/get_database.py b/cooked_input/test/get_database.py index 6b757b8..0782412 100644 --- a/cooked_input/test/get_database.py +++ b/cooked_input/test/get_database.py @@ -1,7 +1,7 @@ import sqlite3 from collections import Counter -import io_get_input -from io_get_input import get_table_input +import cooked_input +from cooked_input import get_table_input def create_db(): # Create an in memory sqlite database of hamburger options @@ -44,7 +44,7 @@ def create_db(): buns = { bun[0]: (bun[1], bun[2]) for bun in cursor.fetchall() } table = [ (k, '{}'.format(v[0], v[1])) for k,v in buns.items() ] default_val = 'plain' - bun = get_table_input(table=table, input_value=io_get_input.TABLE_ID_OR_VALUE, return_value=io_get_input.TABLE_ID, + bun = get_table_input(table=table, input_value=cooked_input.TABLE_ID_OR_VALUE, return_value=cooked_input.TABLE_ID, show_table=True, default=default_val, prompt='Which kind of bun do you want', value_error='a valid bun id or type') price += buns[bun][1] @@ -53,7 +53,7 @@ def create_db(): patties = {patty[0]: (patty[1], patty[2]) for patty in cursor.fetchall()} table = [(k, '{}'.format(v[0], v[1])) for k, v in patties.items()] default_val = 'hamburger' - patty = get_table_input(table=table, input_value=io_get_input.TABLE_ID_OR_VALUE, return_value=io_get_input.TABLE_ID, + patty = get_table_input(table=table, input_value=cooked_input.TABLE_ID_OR_VALUE, return_value=cooked_input.TABLE_ID, default=default_val, prompt='Which kind of patty do you want', value_error='a valid patty id or type') price += patties[patty][1] @@ -65,7 +65,7 @@ def create_db(): while True: extra = get_table_input(table=table, cleaners=None, convertor=None, validators=None, - input_value=io_get_input.TABLE_ID_OR_VALUE, return_value=io_get_input.TABLE_ID, + input_value=cooked_input.TABLE_ID_OR_VALUE, return_value=cooked_input.TABLE_ID, prompt='Which kind of extra do you want (hit return when done choosing extras)', value_error='a valid extra id or type', blank_ok=True) if extra is None: @@ -90,4 +90,4 @@ def create_db(): print('\t{}x {}: ${:.2f}'.format(count, extras[option][0], extras[option][1])) print('\ntotal price: \t${:.2f}'.format(price)) - conn.close() \ No newline at end of file + conn.close() diff --git a/cooked_input/test/get_ints.py b/cooked_input/test/get_ints.py index 4340ae7..1f8d7d8 100644 --- a/cooked_input/test/get_ints.py +++ b/cooked_input/test/get_ints.py @@ -2,9 +2,9 @@ Test to test getting an integer values """ -from io_get_input import get_input -from io_get_input.convertors import IntConvertor -from io_get_input.validators import InRangeValidator, ExactValueValidator, NotInValidator, InAnyValidator +from cooked_input import get_input +from cooked_input.convertors import IntConvertor +from cooked_input.validators import InRangeValidator, ExactValueValidator, NotInValidator, InAnyValidator if __name__ == '__main__': int_convertor = IntConvertor() diff --git a/cooked_input/test/get_menu.py b/cooked_input/test/get_menu.py index 0ccf5c6..347e0d5 100644 --- a/cooked_input/test/get_menu.py +++ b/cooked_input/test/get_menu.py @@ -1,7 +1,7 @@ -from io_get_input import get_table_input -from io_get_input.convertors import IntConvertor -from io_get_input.validators import InRangeValidator +from cooked_input import get_table_input +from cooked_input.convertors import IntConvertor +from cooked_input.validators import InRangeValidator from collections import namedtuple if __name__ == '__main__': diff --git a/cooked_input/test/get_passwords.py b/cooked_input/test/get_passwords.py index e6fbe4c..78bfb55 100644 --- a/cooked_input/test/get_passwords.py +++ b/cooked_input/test/get_passwords.py @@ -1,6 +1,6 @@ -from io_get_input import get_input -from io_get_input.validators import PasswordValidator +from cooked_input import get_input +from cooked_input.validators import PasswordValidator if __name__ == '__main__': @@ -19,4 +19,4 @@ result = get_input(cleaners=None, convertor=None, validators=[disallowed_chars_password_val], prompt='type in a password (type in a; password(no; vowels, even; digits or !, *, \ %)', blank_ok=False, hidden=True) - print('password result=%r' % (result)) \ No newline at end of file + print('password result=%r' % (result)) diff --git a/cooked_input/test/get_strs.py b/cooked_input/test/get_strs.py index 589ed8b..71957b9 100644 --- a/cooked_input/test/get_strs.py +++ b/cooked_input/test/get_strs.py @@ -5,10 +5,11 @@ - show cleaners: strip, upper, lower """ -from io_get_input import get_input -from io_get_input.validators import ExactLengthValidator, InLengthValidator, InChoicesValidator, NotInValidator -from io_get_input.cleaners import StripCleaner, LowerCleaner, UpperCleaner -from io_get_input.convertors import YesNoConvertor +from cooked_input import get_input +from cooked_input.validators import ExactLengthValidator, InLengthValidator, InChoicesValidator, NotInValidator +from cooked_input.cleaners import StripCleaner, LowerCleaner, UpperCleaner +from cooked_input.cleaners import CapitalizeCleaner +from cooked_input.convertors import YesNoConvertor if __name__ == '__main__': colors = ['red', 'green', 'blue'] @@ -26,6 +27,8 @@ strip_cleaner = StripCleaner() lower_cleaner = LowerCleaner() upper_cleaner = UpperCleaner() + capitalize_cleaner = CapitalizeCleaner(all_words=False) + capitalize_all_cleaner = CapitalizeCleaner(all_words=True) # get any string print(get_input(prompt='Enter any string')) @@ -35,6 +38,11 @@ print(get_input(prompt='Enter any string (will be stripped of trailing spaces and converted to upper)', cleaners=[strip_cleaner, upper_cleaner])) + print(get_input(prompt='Enter your name (first word will be capitalized)', + cleaners=capitalize_cleaner)) + print(get_input(prompt='Enter your name (all words will be capitalized)', + cleaners=capitalize_cleaner)) + prompt_str = "What is your favorite flavor jelly bean (don't say licorice!)?" print(get_input(prompt=prompt_str, validators=not_in_choices_validator, default='cherry')) @@ -52,4 +60,8 @@ prompt_str = 'What is your favorite color (%s)' % ', '.join(colors) print(get_input(prompt=prompt_str, default='green')) - print(get_input(prompt="Yes or no?", cleaners=strip_cleaner, convertor=YesNoConvertor(), default='Y')) \ No newline at end of file + print(get_input(prompt=prompt_str, cleaners=cleaners, validators=validators, default='cherry')) + + print(get_input(prompt="Yes or no?", cleaners=strip_cleaner, convertor=YesNoConvertor(), default='Y')) + + diff --git a/cooked_input/test/get_table_input.py b/cooked_input/test/get_table_input.py index c064b86..54bd51e 100644 --- a/cooked_input/test/get_table_input.py +++ b/cooked_input/test/get_table_input.py @@ -1,24 +1,24 @@ -import io_get_input -from io_get_input import get_table_input +import cooked_input +from cooked_input import get_table_input if __name__ == '__main__': table = [(1, 'red'), (2, 'blue'), (4, 'green'), (6, 'yellow')] cur_val = 'red' - color = get_table_input(table=table, cleaners=None, convertor=None, validators=None, input_value=io_get_input.TABLE_ID, return_value=io_get_input.TABLE_ID, show_table=True, + color = get_table_input(table=table, cleaners=None, convertor=None, validators=None, input_value=cooked_input.TABLE_ID, return_value=cooked_input.TABLE_ID, show_table=True, prompt='Enter the id of the color you want', value_error='a valid color id') print('color id=%d' % (color)) - color = get_table_input(table=table, cleaners=None, convertor=None, validators=None, input_value=io_get_input.TABLE_ID, return_value=io_get_input.TABLE_VALUE, show_table=True, + color = get_table_input(table=table, cleaners=None, convertor=None, validators=None, input_value=cooked_input.TABLE_ID, return_value=cooked_input.TABLE_VALUE, show_table=True, default=cur_val, prompt='Enter the id of the color you want', value_error='a valid color id') print('color=%s' % (color)) - color = get_table_input(table=table, cleaners=None, convertor=None, validators=None, input_value=io_get_input.TABLE_VALUE, return_value=io_get_input.TABLE_VALUE, show_table=True, + color = get_table_input(table=table, cleaners=None, convertor=None, validators=None, input_value=cooked_input.TABLE_VALUE, return_value=cooked_input.TABLE_VALUE, show_table=True, default=cur_val, prompt='Enter the name of color you want', value_error='a valid color name') print('color=%s' % (color)) - color = get_table_input(table=table, cleaners=None, convertor=None, validators=None, input_value=io_get_input.TABLE_ID_OR_VALUE, return_value=io_get_input.TABLE_VALUE, show_table=True, + color = get_table_input(table=table, cleaners=None, convertor=None, validators=None, input_value=cooked_input.TABLE_ID_OR_VALUE, return_value=cooked_input.TABLE_VALUE, show_table=True, prompt='Enter the name or id of the color you want', value_error='a valid color name or id') print('color=%s' % (color)) diff --git a/cooked_input/test/simple_input.py b/cooked_input/test/simple_input.py index 498b2b3..ecf90eb 100644 --- a/cooked_input/test/simple_input.py +++ b/cooked_input/test/simple_input.py @@ -47,7 +47,7 @@ def robust_guess(): print('Ding ding... you guessed it!') -from io_get_input import get_input, IntConvertor, InRangeValidator +from cooked_input import get_input, IntConvertor, InRangeValidator def get_input_guess(): number = random.randint(1, 10) @@ -66,4 +66,4 @@ def get_input_guess(): if __name__ == '__main__': simple_guess() # robust_guess() - get_input_guess() \ No newline at end of file + get_input_guess() diff --git a/docs/cleaners.rst b/docs/cleaners.rst index 0015026..1b4f413 100644 --- a/docs/cleaners.rst +++ b/docs/cleaners.rst @@ -31,6 +31,10 @@ case looks like:: Makes input value all Upper case. +.. autoclass:: CapitalizeCleaner + + Capitlizes input value. + .. autoclass:: StripCleaner @@ -39,4 +43,4 @@ case looks like:: .. autoclass:: ReplaceCleaner - Replaces all occurances of "old" string with "new" string white space from the input value \ No newline at end of file + Replaces all occurances of "old" string with "new" string white space from the input value