Skip to content

Commit

Permalink
Added CapitalizeCleaner. More cleanups for transition to cooked_input.
Browse files Browse the repository at this point in the history
  • Loading branch information
lwanger committed Aug 18, 2017
1 parent 86633cd commit 1bda399
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 31 deletions.
6 changes: 4 additions & 2 deletions cooked_input/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
25 changes: 25 additions & 0 deletions cooked_input/cleaners.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import sys
from string import capwords


####
Expand Down Expand Up @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions cooked_input/test/get_database.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]

Expand All @@ -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]

Expand All @@ -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:
Expand All @@ -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()
conn.close()
6 changes: 3 additions & 3 deletions cooked_input/test/get_ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions cooked_input/test/get_menu.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand Down
6 changes: 3 additions & 3 deletions cooked_input/test/get_passwords.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand All @@ -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))
print('password result=%r' % (result))
22 changes: 17 additions & 5 deletions cooked_input/test/get_strs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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'))
Expand All @@ -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'))

Expand All @@ -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'))
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'))


12 changes: 6 additions & 6 deletions cooked_input/test/get_table_input.py
Original file line number Diff line number Diff line change
@@ -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))
4 changes: 2 additions & 2 deletions cooked_input/test/simple_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -66,4 +66,4 @@ def get_input_guess():
if __name__ == '__main__':
simple_guess()
# robust_guess()
get_input_guess()
get_input_guess()
6 changes: 5 additions & 1 deletion docs/cleaners.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ case looks like::

Makes input value all Upper case.

.. autoclass:: CapitalizeCleaner

Capitlizes input value.


.. autoclass:: StripCleaner

Expand All @@ -39,4 +43,4 @@ case looks like::

.. autoclass:: ReplaceCleaner

Replaces all occurances of "old" string with "new" string white space from the input value
Replaces all occurances of "old" string with "new" string white space from the input value

0 comments on commit 1bda399

Please sign in to comment.