2018-11-21 15:37:54 -05:00
|
|
|
#
|
|
|
|
# @author Sinu John
|
|
|
|
# sinuvian at gmail dot com
|
|
|
|
# www.sinujohn.wordpress.com
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# python clock.py [degree [time]]
|
|
|
|
#
|
|
|
|
# 'degree' is the degree of fuzziness - can have values of 1 or 2
|
|
|
|
# 'time' - give time in the format hour:minute (24-hour format)
|
|
|
|
# example: 9:45, 11:30, 14:25
|
|
|
|
# Default value of 'degree' is 1, and 'time' is System time.
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
2019-05-15 16:12:59 -04:00
|
|
|
|
2018-11-21 15:37:54 -05:00
|
|
|
def fuzzy(hour, minute, degree=1):
|
2020-12-09 14:13:35 -05:00
|
|
|
"""Implements the fuzzy clock.
|
2019-05-15 16:12:59 -04:00
|
|
|
returns the the string that spells out the time - hour:minute
|
|
|
|
Supports two degrees of fuzziness. Set with degree = 1 or degree = 2
|
|
|
|
When degree = 1, time is in quantum of 5 minutes.
|
2020-12-09 14:13:35 -05:00
|
|
|
When degree = 2, time is in quantum of 15 minutes."""
|
2019-05-15 16:12:59 -04:00
|
|
|
|
|
|
|
if degree <= 0 or degree > 2:
|
2020-12-09 14:13:35 -05:00
|
|
|
print("Please use a degree of 1 or 2. Using fuzziness degree=1")
|
2019-05-15 16:12:59 -04:00
|
|
|
degree = 1
|
|
|
|
|
2020-12-09 14:13:35 -05:00
|
|
|
begin = "It's "
|
|
|
|
|
|
|
|
f0 = "almost "
|
|
|
|
f1 = "exactly "
|
|
|
|
f2 = "around "
|
|
|
|
|
|
|
|
b0 = " past "
|
|
|
|
b1 = " to "
|
|
|
|
|
|
|
|
hourlist = (
|
|
|
|
"One",
|
|
|
|
"Two",
|
|
|
|
"Three",
|
|
|
|
"Four",
|
|
|
|
"Five",
|
|
|
|
"Six",
|
|
|
|
"Seven",
|
|
|
|
"Eight",
|
|
|
|
"Nine",
|
|
|
|
"Ten",
|
|
|
|
"Eleven",
|
|
|
|
"Twelve",
|
|
|
|
)
|
|
|
|
|
|
|
|
s1 = s2 = s3 = s4 = ""
|
2019-05-15 16:12:59 -04:00
|
|
|
base = 5
|
|
|
|
|
|
|
|
if degree == 1:
|
|
|
|
base = 5
|
2020-12-09 14:13:35 -05:00
|
|
|
val = ("Five", "Ten", "Quarter", "Twenty", "Twenty-Five", "Half")
|
2019-05-15 16:12:59 -04:00
|
|
|
elif degree == 2:
|
|
|
|
base = 15
|
2020-12-09 14:13:35 -05:00
|
|
|
val = ("Quarter", "Half")
|
2019-05-15 16:12:59 -04:00
|
|
|
|
|
|
|
# to find whether we have to use 'almost', 'exactly' or 'around'
|
|
|
|
dmin = minute % base
|
|
|
|
if minute > 30:
|
|
|
|
pos = int((60 - minute) / base) # position in the tuple 'val'
|
|
|
|
else:
|
|
|
|
pos = int(minute / base)
|
|
|
|
|
|
|
|
if dmin == 0:
|
|
|
|
s1 = f1
|
|
|
|
pos = pos - 1
|
2020-12-04 08:56:26 -05:00
|
|
|
elif dmin <= base / 2:
|
2019-05-15 16:12:59 -04:00
|
|
|
s1 = f2
|
|
|
|
if minute < 30:
|
2020-12-04 08:56:26 -05:00
|
|
|
pos = pos - 1
|
2019-05-15 16:12:59 -04:00
|
|
|
else:
|
|
|
|
s1 = f0
|
|
|
|
if minute > 30:
|
|
|
|
pos = pos - 1
|
|
|
|
|
|
|
|
s2 = val[pos]
|
|
|
|
|
|
|
|
if minute <= base / 2:
|
|
|
|
# Case like "It's around/exactly Ten"
|
2020-12-09 14:13:35 -05:00
|
|
|
s2 = s3 = ""
|
|
|
|
s4 = hourlist[hour - 12 - 1]
|
2019-05-15 16:12:59 -04:00
|
|
|
elif minute >= 60 - base / 2:
|
|
|
|
# Case like "It's almost Ten"
|
2020-12-09 14:13:35 -05:00
|
|
|
s2 = s3 = ""
|
|
|
|
s4 = hourlist[hour - 12]
|
2019-05-15 16:12:59 -04:00
|
|
|
else:
|
|
|
|
# Other cases with all words, like "It's around Quarter past One"
|
|
|
|
if minute > 30:
|
|
|
|
s3 = b1 # to
|
2020-12-09 14:13:35 -05:00
|
|
|
s4 = hourlist[hour - 12]
|
2019-05-15 16:12:59 -04:00
|
|
|
else:
|
|
|
|
s3 = b0 # past
|
2020-12-09 14:13:35 -05:00
|
|
|
s4 = hourlist[hour - 12 - 1]
|
2019-05-15 16:12:59 -04:00
|
|
|
|
|
|
|
return begin + s1 + s2 + s3 + s4
|
|
|
|
|
2018-11-21 15:37:54 -05:00
|
|
|
|
|
|
|
def main():
|
2019-05-15 16:12:59 -04:00
|
|
|
deg = 1
|
|
|
|
stm = time.localtime()
|
|
|
|
h = stm.tm_hour
|
|
|
|
m = stm.tm_min
|
|
|
|
|
|
|
|
if len(sys.argv) >= 2:
|
|
|
|
try:
|
|
|
|
deg = int(sys.argv[1])
|
|
|
|
except Exception:
|
2020-12-09 14:13:35 -05:00
|
|
|
print("Please use a degree of 1 or 2. Using fuzziness degree=1")
|
2019-05-15 16:12:59 -04:00
|
|
|
|
|
|
|
if len(sys.argv) >= 3:
|
2020-12-09 14:13:35 -05:00
|
|
|
tm = sys.argv[2].split(":")
|
2019-05-15 16:12:59 -04:00
|
|
|
try:
|
|
|
|
h = int(tm[0])
|
|
|
|
m = int(tm[1])
|
|
|
|
if h < 0 or h > 23 or m < 0 or m > 59:
|
|
|
|
raise Exception
|
|
|
|
except Exception:
|
2020-12-09 14:13:35 -05:00
|
|
|
print("Bad time entered. Using the system time.")
|
2019-05-15 16:12:59 -04:00
|
|
|
h = stm.tm_hour
|
|
|
|
m = stm.tm_min
|
|
|
|
print(fuzzy(h, m, deg))
|
|
|
|
return
|
2018-11-21 15:37:54 -05:00
|
|
|
|
2020-12-04 08:56:26 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|