diff --git a/README.md b/README.md index 24a7c98..35387b4 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Requirements and Installation For basic functionality: -* Python 2.7 or 3 +* Python 2.6 or 3 To install, just run: diff --git a/choice/basicterm.py b/choice/basicterm.py index 902a40e..772d4e1 100644 --- a/choice/basicterm.py +++ b/choice/basicterm.py @@ -1,9 +1,16 @@ from __future__ import division, absolute_import, print_function, unicode_literals import sys -if sys.version_info.major < 3: + +try: + if sys.version_info.major < 3: + input = raw_input +except AttributeError: + # Python 2.6 has a version tuple input = raw_input +from os import linesep + from choice.util import idNameList class BasicTermMenu(object): @@ -27,16 +34,16 @@ def pick_choice(self): start = curr_page * self.PAGE_SIZE end = start + self.PAGE_SIZE for i, c in enumerate(self.choices[start:end]): - print(" {}: {}".format(i, c[1])) + print(" {0}: {1}".format(i, c[1])) if self.global_actions is not None: print() for ga in self.global_actions: if ga[0] == ga[1]: - print(" {}".format(ga[0])) + print(" {0}".format(ga[0])) else: - print(" {}: {}".format(ga[0], ga[1])) + print(" {0}: {1}".format(ga[0], ga[1])) - print("\nEnter number or name; return for next page") + print(linesep + "Enter number or name; return for next page") resp = input('? ').strip() print() if len(resp) == 0: @@ -53,8 +60,8 @@ def pick_choice(self): def pick_action(self): print("Select an action:") for i, ac in enumerate(self.actions): - print(" {}: {}".format(i, ac[1])) - print(" {}: back".format(self.BACK_CHAR)) + print(" {0}: {1}".format(i, ac[1])) + print(" {0}: back".format(self.BACK_CHAR)) resp = input('? ') print() return str(resp) @@ -70,7 +77,7 @@ def ask(self): break else: # Global action? - for ga in self.global_actions: + for ga in self.global_actions or []: if resp == ga[0]: return None, ga[0] @@ -81,7 +88,7 @@ def ask(self): break if choice is None: - print("{} is not a valid choice".format(resp)) + print("{0} is not a valid choice".format(resp)) assert choice is not None if self.actions is None or len(self.actions) == 1: @@ -106,7 +113,7 @@ def ask(self): action = ac break if action is None: - print("{} is not a valid choice".format(resp)) + print("{0} is not a valid choice".format(resp)) assert action is not None return choice[0], action[0] @@ -118,7 +125,7 @@ def __init__(self, prompt, parser): self.parser = parser def ask(self): - resp = input('{}:\n? '.format(self.prompt)) + resp = input(self.prompt + ':' + linesep + '? ') try: return self.parser(resp) except ValueError: @@ -133,12 +140,12 @@ def __init__(self, prompt, default): def ask(self): if self.default is None: - prompt = '{}\n(y/n)? '.format(self.prompt) + options = '(y/n)' elif self.default == True: - prompt = '{}\n(Y/n)? '.format(self.prompt) + options = '(Y/n)' elif self.default == False: - prompt = '{}\n(y/N)? '.format(self.prompt) - resp = input(prompt).lower().strip() + options = '(y/N)' + resp = input(self.prompt + linesep + options + '? ').lower().strip() if len(resp) == 0 and self.default is not None: return self.default diff --git a/setup.py b/setup.py index ecb4284..cd24c8a 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup setup(name='choice', - version='0.1', + version='0.2c1', author='Frank Huang', author_email='me@nongraphical.com', url='https://github.com/fyhuang/choice', diff --git a/test/test.py b/test/test.py index fd454e8..ad4c22f 100644 --- a/test/test.py +++ b/test/test.py @@ -1,20 +1,23 @@ import choice +def c(): + print("Delete confirmed") + # Get a yes or no response (default is no) confirm = choice.Binary('Are you sure you want to delete?', False).ask() if confirm: - deleteIt() + c() # Input an arbitrary value, check for correctness howmany = choice.Input('How many pies?', int).ask() -print("You ordered {} pies".format(howmany)) +print("You ordered {0} pies".format(howmany)) # Choose from a set of options entree = choice.Menu(['steak', 'potatoes', 'eggplant']).ask() -print("You choice {}".format(entree)) +print("You choice {0}".format(entree)) -posts = ['post {}'.format(num) for num in range(15)] +posts = ['post {0}'.format(num) for num in range(15)] resp = choice.Menu(posts, ['edit', 'delete', 'publish'], ['newpost', 'exit']).ask() print(resp)