-
Notifications
You must be signed in to change notification settings - Fork 10
/
Console.py
131 lines (115 loc) · 4.97 KB
/
Console.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import sys
class _RangeError(Exception): pass
def get_string(message, name="string", default=None,
minimum_length=0, maximum_length=80,
force_lower=False):
message += ": " if default is None else " [{0}]: ".format(default)
while True:
try:
line = input(message)
if not line:
if default is not None:
return default
if minimum_length == 0:
return ""
else:
raise ValueError("{0} may not be empty".format(
name))
if not (minimum_length <= len(line) <= maximum_length):
raise ValueError("{0} must have at least {1} and "
"at most {2} characters".format(
name, minimum_length, maximum_length))
return line if not force_lower else line.lower()
except ValueError as err:
print("ERROR", err)
def get_integer(message, name="integer", default=None, minimum=None,
maximum=None, allow_zero=True):
message += ": " if default is None else " [{0}]: ".format(default)
while True:
try:
line = input(message)
if not line and default is not None:
return default
x = int(line)
if x == 0:
if allow_zero:
return x
else:
raise _RangeError("{0} may not be 0".format(name))
if ((minimum is not None and minimum > x) or
(maximum is not None and maximum < x)):
raise _RangeError("{0} must be between {1} and {2} "
"inclusive{3}".format(name, minimum, maximum,
(" (or 0)" if allow_zero else "")))
return x
except _RangeError as err:
print("ERROR", err)
except ValueError as err:
print("ERROR {0} must be an integer".format(name))
def get_float(message, name="float", default=None, minimum=None,
maximum=None, allow_zero=True):
message += ": " if default is None else " [{0}]: ".format(default)
while True:
try:
line = input(message)
if not line and default is not None:
return default
x = float(line)
if abs(x) < sys.float_info.epsilon:
if allow_zero:
return x
else:
raise _RangeError("{0} may not be 0.0".format(
name))
if ((minimum is not None and minimum > x) or
(maximum is not None and maximum < x)):
raise _RangeError("{0} must be between {1} and {2} "
"inclusive{3}".format(name, minimum, maximum,
(" (or 0.0)" if allow_zero else "")))
return x
except _RangeError as err:
print("ERROR", err)
except ValueError as err:
print("ERROR {0} must be a float".format(name))
def get_bool(message, default=None):
yes = frozenset({"1", "y", "yes", "t", "true", "ok"})
message += " (y/yes/n/no)"
message += ": " if default is None else " [{0}]: ".format(default)
line = input(message)
if not line and default is not None:
return default in yes
return line.lower() in yes
def get_date(message, default=None, format="%y-%m-%d"):
# message should include the format in human-readable form, e.g.
# for %y-%m-%d, "YY-MM-DD".
message += ": " if default is None else " [{0}]: ".format(default)
while True:
try:
line = input(message)
if not line and default is not None:
return default
return datetime.datetime.strptime(line, format)
except ValueError as err:
print("ERROR", err)
def get_menu_choice(message, valid, default=None, force_lower=False):
message += ": " if default is None else " [{0}]: ".format(default)
while True:
line = input(message)
if not line and default is not None:
return default
if line not in valid:
print("ERROR only {0} are valid choices".format(
", ".join(["'{0}'".format(x)
for x in sorted(valid)])))
else:
return line if not force_lower else line.lower()