-
Notifications
You must be signed in to change notification settings - Fork 0
/
saytime.py
executable file
·151 lines (131 loc) · 4.56 KB
/
saytime.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/python3
# saytime.py by Bill Weinman [http://bw.org/]
# created for Python 3 Essential Training on lynda.com
# Copyright 2010 The BearHeart Gorup, LLC
import sys
import time
__version__ = "1.1.0"
class numwords():
"""
return a number as words,
e.g., 42 becomes "forty-two"
"""
_words = {
'ones': (
'oh', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'
), 'tens': (
'', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
), 'teens': (
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
), 'quarters': (
'o\'clock', 'quarter', 'half'
), 'range': {
'hundred': 'hundred'
}, 'misc': {
'minus': 'minus'
}
}
_oor = 'OOR' # Out Of Range
def __init__(self, n):
self.__number = n;
def numwords(self, num = None):
"Return the number as words"
n = self.__number if num is None else num
s = ''
if n < 0: # negative numbers
s += self._words['misc']['minus'] + ' '
n = abs(n)
if n < 10: # single-digit numbers
s += self._words['ones'][n]
elif n < 20: # teens
s += self._words['teens'][n - 10]
elif n < 100: # tens
m = n % 10
t = n // 10
s += self._words['tens'][t]
if m: s += '-' + numwords(m).numwords() # recurse for remainder
elif n < 1000: # hundreds
m = n % 100
t = n // 100
s += self._words['ones'][t] + ' ' + self._words['range']['hundred']
if m: s += ' ' + numwords(m).numwords() # recurse for remainder
else:
s += self._oor
return s
def number(self):
"Return the number as a number"
return str(self.__number);
class saytime(numwords):
"""
return the time (from two parameters) as words,
e.g., fourteen til noon, quarter past one, etc.
"""
_specials = {
'noon': 'noon',
'midnight': 'midnight',
'til': 'til',
'past': 'past'
}
def __init__(self, h, m):
self._hour = abs(int(h))
self._min = abs(int(m))
def words(self):
h = self._hour
m = self._min
if h > 23: return self._oor # OOR errors
if m > 59: return self._oor
sign = self._specials['past']
if self._min > 30:
sign = self._specials['til']
h += 1
m = 60 - m
if h > 23: h -= 24
elif h > 12: h -= 12
# hword is the hours word)
if h is 0: hword = self._specials['midnight']
elif h is 12: hword = self._specials['noon']
else: hword = self.numwords(h)
if m is 0:
if h in (0, 12): return hword # for noon and midnight
else: return "{} {}".format(self.numwords(h), self._words['quarters'][m])
if m % 15 is 0:
return "{} {} {}".format(self._words['quarters'][m // 15], sign, hword)
return "{} {} {}".format(self.numwords(m), sign, hword)
def digits(self):
"return the traditionl time, e.g., 13:42"
return "{:02}:{:02}".format(self._hour, self._min)
class saytime_t(saytime): # wrapper for saytime to use time object
"""
return the time (from a time object) as words
e.g., fourteen til noon
"""
def __init__(self, t):
self._hour = t.tm_hour
self._min = t.tm_min
def main():
if len(sys.argv) > 1:
if sys.argv[1] == 'test':
test()
else:
try: print(saytime(*(sys.argv[1].split(':'))).words())
except TypeError: print("Invalid time ({})".format(sys.argv[1]))
else:
print(saytime_t(time.localtime()).words())
def test():
print("\nnumbers test:")
list = (
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 19, 20, 30,
50, 51, 52, 55, 59, 99, 100, 101, 112, 900, 999, 1000
)
for l in list:
print(l, numwords(l).numwords())
print("\ntime test:")
list = (
(0, 0), (0, 1), (11, 0), (12, 0), (13, 0), (12, 29), (12, 30),
(12, 31), (12, 15), (12, 30), (12, 45), (11, 59), (23, 15),
(23, 59), (12, 59), (13, 59), (1, 60), (24, 0)
)
for l in list:
print(saytime(*l).digits(), saytime(*l).words())
print("\nlocal time is " + saytime_t(time.localtime()).words())
if __name__ == "__main__": main()