text
stringlengths
0
828
if default is not None:
if isinstance(default, bool):
pass
else:
default_ = default.upper()
if default_ not in ('Y', 'YES', 'N', 'NO'):
raise RuntimeError(""Invalid default value: '{}'"".format(default))
default = default_ in ('Y', 'YES')
while True:
ans = input(""{} ({}/{})? "".format(question, ""Y"" if default == True else ""y"",
""N"" if default == False else ""n"")).upper()
if ans == """" and default is not None:
ret = default
break
elif ans in (""N"", ""NO""):
ret = False
break
elif ans in (""Y"", ""YES""):
ret = True
break
return ret"
4567,"def menu(title, options, cancel_label=""Cancel"", flag_allow_empty=False, flag_cancel=True, ch='.'):
""""""Text menu.
Arguments:
title -- menu title, to appear at the top
options -- sequence of strings
cancel_label='Cancel' -- label to show at last ""zero"" option
flag_allow_empty=0 -- Whether to allow empty option
flag_cancel=True -- whether there is a ""0 - Cancel"" option
ch=""."" -- character to use to draw frame around title
Returns:
option -- an integer: None; 0-Back/Cancel/etc; 1, 2, ...
Adapted from irootlab menu.m""""""
num_options, flag_ok = len(options), 0
option = None # result
min_allowed = 0 if flag_cancel else 1 # minimum option value allowed (if option not empty)
while True:
print("""")
for line in format_box(title, ch):
print("" ""+line)
for i, s in enumerate(options):
print(("" {0:d} - {1!s}"".format(i+1, s)))
if flag_cancel: print(("" 0 - << (*{0!s}*)"".format(cancel_label)))
try:
s_option = input('? ')
except KeyboardInterrupt:
raise
except:
print("""")
n_try = 0
while True:
if n_try >= 10:
print('You are messing up!')
break
if len(s_option) == 0 and flag_allow_empty:
flag_ok = True
break
try:
option = int(s_option)
if min_allowed <= option <= num_options:
flag_ok = True
break
except ValueError:
print(""Invalid integer value!"")
print((""Invalid option, range is [{0:d}, {1:d}]!"".format(0 if flag_cancel else 1, num_options)))
n_try += 1
s_option = input(""? "")
if flag_ok:
break
return option"
4568,"def format_box(title, ch=""*""):
""""""
Encloses title in a box. Result is a list
>>> for line in format_box(""Today's TODO list""):
... print(line)
*************************
*** Today's TODO list ***
*************************
""""""
lt = len(title)
return [(ch * (lt + 8)),
(ch * 3 + "" "" + title + "" "" + ch * 3),
(ch * (lt + 8))
]"
4569,"def format_progress(i, n):
""""""Returns string containing a progress bar, a percentage, etc.""""""