Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Requirements and Installation

For basic functionality:

* Python 2.7 or 3
* Python 2.6 or 3

To install, just run:

Expand Down
37 changes: 22 additions & 15 deletions choice/basicterm.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -70,7 +77,7 @@ def ask(self):
break
else:
# Global action?
for ga in self.global_actions:
for ga in self.global_actions or []:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I think you could put this at the top of the class:

class BasicTermMenu(object):
  def __init__(...):
    self.global_actions = idNameList(global_actions) or []

Or alternatively, edit idNameList() to return [] when the input is None. This would prevent other crashes, e.g. here:

https://github.com/fyhuang/choice/pull/3/files#diff-ea6df2eb5ce078088b03f5d6e6365c16L33

if resp == ga[0]:
return None, ga[0]

Expand All @@ -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:
Expand All @@ -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]
Expand All @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
11 changes: 7 additions & 4 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -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()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bastbnl, any reason to put this in a separate c() function rather than just inline? Seems to me that just writing print("Delete confirmed") here would work too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason. I noticed confirming the question stops the test script as the function is undefined. A separate function is not required at all and printing a message would make sense


# 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)
Expand Down