Skip to content

Commit

Permalink
Lots of cleanup of code and examples. Fixed several bugs throughout. …
Browse files Browse the repository at this point in the history
…Cleaned up redundant code, unused imports, added examples and docs to installed package. Tested all examples.
  • Loading branch information
lwanger committed Aug 21, 2017
1 parent 3892075 commit 4e3a60d
Show file tree
Hide file tree
Showing 24 changed files with 121 additions and 160 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 lwanger
Copyright (c) 2017 len wanger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright © 2017 Len Wanger and Impossible Objects
Copyright © 2017 Len Wanger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
Expand Down
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
include README.rst
include LICENSE.txt
include LICENSE
recursive-include docs *.rst conf.py Makefile
recursive-include cooked_input/examples *.py
5 changes: 0 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ cooked input.

github archive: https://github.com/lwanger/cooked_input


for the latest documentation, see: https://readthedocs.org/projects/cooked-input/

----

This is the readme for the project.

see TODO.md for list of TODO items
7 changes: 6 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
**TODO:**

* general:
* Ship example and test dirs.
* Need to work on error messages. Not handled well now.
* Add tables (build-a-burger) to tutorial
* Review examples, tutorial interface. Clean up and make: easier, cleaner, more consistent.
Expand Down Expand Up @@ -47,4 +48,8 @@
* add dollar convertor that has minimum of 0.00 and strips off $ sign and commas. Returns float

* validators:
* add: date range, date day of week
* add: date range, date day of week
* allow forcing to validate all validators instead of stopping on first failure
* return list of all validation failures
* provide list of hints for what is required
* password validator should create hint of what's required for password
51 changes: 0 additions & 51 deletions config.py

This file was deleted.

4 changes: 2 additions & 2 deletions cooked_input/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

__version__ = '0.1.4'
__version__ = '0.1.6'

from .get_input import get_input, get_table_input, process_value, make_pretty_table
from .get_input import TABLE_ID, TABLE_VALUE, TABLE_ID_OR_VALUE
Expand All @@ -9,7 +9,7 @@
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 .validators import in_all, in_any, not_in, validate
from .cleaners import Cleaner, UpperCleaner, LowerCleaner, CapitalizeCleaner
from .cleaners import StripCleaner, ReplaceCleaner

Expand Down
9 changes: 3 additions & 6 deletions cooked_input/cleaners.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
"""
get_input module to get values from the command line.
This file contains cleaner classes for io_get_input
This file contains cleaner classes for cooked_input
Author: Len Wanger
Copyright: Len Wanger, 2017
"""

import sys
from string import capwords


Expand Down Expand Up @@ -73,9 +70,9 @@ def __init__(self, all_words=False, **kwargs):

def __call__(self, value):
if self.all_words:
result = value.capitalize()
else:
result = capwords(value)
else:
result = value.capitalize()

return result

Expand Down
9 changes: 1 addition & 8 deletions cooked_input/convertors.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
"""
get_input module to get values from the command line.
convertor classes
This file contains convertors classes for io_get_input
This file contains convertors classes for cooked_input
Author: Len Wanger
Copyright: Len Wanger, 2017
"""

import sys
import re
import dateparser
import csv
from io import StringIO
Expand Down
6 changes: 6 additions & 0 deletions cooked_input/examples/get_database.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
cooked input example showing how to use with entries from database tables.
Len Wanger, 2017
"""

import sqlite3
from collections import Counter
import cooked_input
Expand Down
4 changes: 3 additions & 1 deletion cooked_input/examples/get_ints.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""
Test to test getting an integer values
cooked input examples of getting an integer values
Len Wanger, 2017
"""

from cooked_input import get_input
Expand Down
11 changes: 6 additions & 5 deletions cooked_input/examples/get_lists.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Test to test getting lists
cooked input examples of getting an list values
Len Wanger, 2017
"""

from cooked_input import get_input
Expand All @@ -20,10 +21,10 @@
upper_cleaner = UpperCleaner()

# get any string
print(get_input(prompt='Enter a list', convertor=ListConvertor()))
print(get_input(prompt='Enter a list (blank OK)', convertor=ListConvertor(), blank_ok=True))
print(get_input(prompt='Enter a list (blank not OK)', convertor=ListConvertor(), blank_ok=False))
print(get_input(prompt='Enter a list (stripped)', convertor=ListConvertor(), cleaners=strip_cleaner))
print(get_input(prompt='Enter a list (separated by commas)', convertor=ListConvertor()))
print(get_input(prompt='Enter a list (separated by commas, blank OK)', convertor=ListConvertor(), blank_ok=True))
print(get_input(prompt='Enter a list (separated by commas, blank not OK)', convertor=ListConvertor(), blank_ok=False))
print(get_input(prompt='Enter a list (separated by commas, stripped)', convertor=ListConvertor(), cleaners=strip_cleaner))

# specified delimiter
print(get_input(prompt='Enter a list (separated by commas)', convertor=ListConvertor(delimiter=',')))
Expand Down
6 changes: 6 additions & 0 deletions cooked_input/examples/get_menu.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
cooked input examples of getting entry from menus
Len Wanger, 2017
"""


from cooked_input import get_table_input
from cooked_input.convertors import IntConvertor
Expand Down
5 changes: 5 additions & 0 deletions cooked_input/examples/get_passwords.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
cooked input examples of getting password values
Len Wanger, 2017
"""

from cooked_input import get_input
from cooked_input.validators import PasswordValidator
Expand Down
36 changes: 15 additions & 21 deletions cooked_input/examples/get_strs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
Test to test getting a string values
cooked input examples of getting an string values
- show cleaners: strip, upper, lower
Len Wanger, 2017
"""

from cooked_input import get_input
Expand All @@ -25,43 +24,38 @@
not_in_choices_validator = NotInValidator(validators=[bad_flavor_validator])

strip_cleaner = StripCleaner()
rstrip_cleaner = StripCleaner(lstrip=False, rstrip=True)
lower_cleaner = LowerCleaner()
upper_cleaner = UpperCleaner()
capitalize_cleaner = CapitalizeCleaner(all_words=False)
capitalize_all_cleaner = CapitalizeCleaner(all_words=True)
strip_and_lower_cleaners = [strip_cleaner, lower_cleaner]


# get any string
print(get_input(prompt='Enter any string'))
print(get_input(prompt='Enter any string', blank_ok=True))
print(get_input(prompt='Enter any string (will be stripped of leading and trailing spaces and converted to lower)',
cleaners=[strip_cleaner, lower_cleaner]))
cleaners=strip_and_lower_cleaners))
print(get_input(prompt='Enter any string (will be stripped of trailing spaces and converted to upper)',
cleaners=[strip_cleaner, upper_cleaner]))
cleaners=[rstrip_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))
# capitalization cleaning (CapitalizeCleaner)
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_all_cleaner))

prompt_str = "What is your favorite flavor jelly bean (don't say licorice!)?"
# picking from choices (InchoicesValidator)
prompt_str = "What is your favorite flavor jelly bean (pick any flavor, don't say licorice!)?"
print(get_input(prompt=prompt_str, validators=not_in_choices_validator, default='cherry'))

prompt_str = "Which of these is your favorite flavor jelly bean (%s, but not licorice!)?" % ', '.join(good_flavors)
cleaners = [strip_cleaner, lower_cleaner]
prompt_str = "Which of these is your favorite flavor jelly bean (choose from: %s, but not licorice!)?" % ', '.join(good_flavors)
validators = [good_flavor_validator, not_in_choices_validator]
print(get_input(prompt=prompt_str, cleaners=cleaners, validators=validators, default='cherry'))
print(get_input(prompt=prompt_str, cleaners=strip_and_lower_cleaners, validators=validators, default='cherry'))

# get different length strings
print(get_input(prompt='Enter a three letter string', validators=[length_3_validator]))
print(get_input(prompt='Enter a string at least 5 letters long', validators=[length_5_plus_validator]))
print(get_input(prompt='Enter a 2 to 4 letter string', validators=[length_2_to_4_validator]))

# Use InChoicesValidator
prompt_str = 'What is your favorite color (%s)' % ', '.join(colors)
print(get_input(prompt=prompt_str, default='green'))

print(get_input(prompt=prompt_str, cleaners=cleaners, validators=validators, default='cherry'))

# Use YesNoConvertor
print(get_input(prompt="Yes or no?", cleaners=strip_cleaner, convertor=YesNoConvertor(), default='Y'))


5 changes: 5 additions & 0 deletions cooked_input/examples/get_table_input.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
cooked input examples of getting inputs from tables
Len Wanger, 2017
"""

import cooked_input
from cooked_input import get_table_input
Expand Down
8 changes: 5 additions & 3 deletions cooked_input/examples/get_user_info.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
No error msgs for password
cooked input examples of getting inputs from tables
Have Password validator create hints for proper input. Expand that idea?
Have error messages for all fields
Len Wanger, 2017
"""


Expand Down Expand Up @@ -99,6 +98,9 @@ def __call__(self, value):
role_validtor = ListValidator(elem_validators=InChoicesValidator(roles_list))
role_prompt = 'Roles ({}, separated by commas)'.format(sorted(roles_list))

# TODO -- remove me!!!
roles = get_input(prompt=role_prompt, cleaners=default_cleaners, convertor=ListConvertor(), validators=role_validtor, blank_ok=False)

# Simulate logging the user in:
try:
user_name = get_input(prompt='Username', cleaners=default_cleaners, validators=CheckUserValidator(), retries=3)
Expand Down
2 changes: 2 additions & 0 deletions cooked_input/examples/simple_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
simple_guess: the naive version. Not robust.
robust_guess: trying to beef up the naive version, lots of code for a simple input of an integer!
cooked_input_guess: implemented with cooked_input
Len Wanger, 2017
"""

import sys
Expand Down
4 changes: 2 additions & 2 deletions cooked_input/examples/validate_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

def on_button():
value = entry1.get()
processed_value = process_value(value, cleaners=StripCleaner(), convertor=IntConvertor(), validators=InRangeValidator(min_val=1, max_val=10))
valid, processed_value = process_value(value, cleaners=StripCleaner(), convertor=IntConvertor(), validators=InRangeValidator(min_val=1, max_val=10))

if processed_value:
if valid:
messagebox.showinfo("Integer is...", "Integer is good: {}".format(processed_value))
top.quit()
else:
Expand Down
22 changes: 3 additions & 19 deletions cooked_input/get_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"""
get_input module to get values from the command line.
document usage.
point to examples
talk about convertors, cleaners, and validators
see: https://github.com/lwanger/cooked_input for more information.
Author: Len Wanger
Copyright: Len Wanger, 2017
Expand All @@ -16,11 +14,8 @@
import getpass
import prettytable

# relative import needed to run, but sphinx need non-relative.
from .validators import InChoicesValidator, in_all
from .validators import InChoicesValidator, in_all, validate
from .convertors import TableConvertor
# from validators import InChoicesValidator
# from convertors import TableConvertor


TABLE_ID = 0
Expand Down Expand Up @@ -304,15 +299,4 @@ def get_table_input(table=None, cleaners=None, convertor=None, validators=None,
if return_value == TABLE_VALUE:
return t[TABLE_VALUE]
else:
return t[TABLE_ID]


def validate(value, validators=None):
"""
Run validators on a value.
:param value: the value to validate.
:param validators: list of validators to run on the value.
:return: True if the input passed validation, else False
"""
return compose(value, validators)
return t[TABLE_ID]
Loading

0 comments on commit 4e3a60d

Please sign in to comment.