1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-12-22 09:31:42 -05:00
aprsd/aprsd/fuzzyclock.py
Walter A. Boring IV ecd797d91e First stab at migrating this to a pytpi repo
This patch does some refactoring of the code and the directory
structure to conform to the needs of a pypi project.
The python code now lives in the aprsd directory so it acts like a real
python package that can be installed/included/used.
The aprsd.py is now aprds/main.py

This patch also adds support for using pbr, which enables a consistent
bin install that you can then call as 'aprsd' from the command line.

To use this as a developer you should create a virtualenv
virtualenv .venv
source .venv/bin/activate
pip install -e .

now you can edit the aprds/main.py and then test it by immediately
running aprsd from the command line.

The -e option for pip allows you to install the package as an editable
package in the .venv, so you can hack on it and not need to re-install
every time you make a change.
2018-11-21 13:56:29 -08:00

115 lines
2.6 KiB
Python

#
# @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
def fuzzy(hour, minute, degree=1):
'''Implements the fuzzy clock.
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.
When degree = 2, time is in quantum of 15 minutes.'''
if degree<=0 or degree>2:
print 'Please use a degree of 1 or 2. Using fuzziness degree=1'
degree = 1
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 = ''
base = 5
if degree == 1:
base = 5
val = ('Five', 'Ten', 'Quarter', 'Twenty', 'Twenty-Five', 'Half')
elif degree == 2:
base = 15
val = ('Quarter', 'Half')
dmin = minute % base # to find whether we have to use 'almost', 'exactly' or 'around'
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
elif dmin <= base/2:
s1 = f2
if minute < 30:
pos = pos-1
else:
s1 = f0
if minute > 30:
pos = pos -1
s2 = val[pos]
if minute <= base/2: # Case like "It's around/exactly Ten"
s2 = s3 = ''
s4 = hourList[hour - 12 - 1]
elif minute >= 60-base/2: # Case like "It's almost Ten"
s2 = s3 = ''
s4 = hourList[hour - 12]
else: # Other cases with all words, like "It's around Quarter past One"
if minute>30:
s3 = b1 # to
s4 = hourList[hour - 12]
else:
s3 = b0 # past
s4 = hourList[hour - 12 - 1]
return begin + s1 + s2 + s3 + s4
def main():
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:
print 'Please use a degree of 1 or 2. Using fuzziness degree=1'
if len(sys.argv)>=3:
tm = sys.argv[2].split(':')
try:
h = int(tm[0])
m = int(tm[1])
if h<0 or h>23 or m<0 or m>59:
raise Exception
except:
print 'Bad time entered. Using the system time.'
h = stm.tm_hour
m = stm.tm_min
print fuzzy(h, m, deg)
return
main()