initial checkin

This commit is contained in:
Will Roberts
2014-01-31 21:06:09 +01:00
commit 0bb68b9653
5 changed files with 164 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*.pyc
*~

74
notes.py Normal file
View File

@@ -0,0 +1,74 @@
# http://stackoverflow.com/questions/4628122/how-to-construct-a-timedelta-object-from-a-simple-string
32m
2h32m
3d2h32m
1w3d2h32m
1w 3d 2h 32m
1 w 3 d 2 h 32 m
4:13
4:13:02
4:13:02.266
2:04:13:02.266
2 days, 4:13:02 # uptime format
2 days, 4:13:02.266
5hr34m56s
5 hours, 34 minutes, 56 seconds
5 hrs, 34 mins, 56 secs
2 days, 5 hours, 34 minutes, 56 seconds
1.2 m
1.2 min
1.2 mins
1.2 minute
1.2 minutes
172 hours
172 hr
172 h
172 hrs
172 hour
1.24 days
5 d
5 day
5 days
5.6 wk
5.6 week
5.6 weeks
3.1 months
12.24 year
12.24 years
12.24 yr
12.24 y
12 years, 3 months, 2 days, 5 hours, 34 minutes, 56 seconds
12 years, 3 months, 2 days, 4:13:02
# http://stackoverflow.com/questions/538666/python-format-timedelta-to-string
# standard format using
# str(datetime.timedelta(hours=10.56))
def interpret_time_since ( s ):
'''Attempts to parse a "time since", such as 5.6 wk. If successful,
returns the time referred to.'''
m = re.match ( r'^([0-9.]+)\s*([a-z]*)$', s.strip().lower() )
if m is not None:
number, units = m.groups()
if not units:
units = ''
number, units = float(number), units.strip()
if units in [ 'm', 'min', 'mins', 'minute', 'minutes' ]:
return ( datetime.datetime.now() -
datetime.timedelta ( minutes = number ) )
elif units in [ 'h', 'hr', 'hrs', 'hour', 'hours' ]:
return ( datetime.datetime.now() -
datetime.timedelta ( hours = number ) )
elif units in [ 'd', 'day', 'days' ]:
return ( datetime.datetime.now() -
datetime.timedelta ( days = number ) )
elif units in [ 'w', 'week', 'weeks' ]:
return ( datetime.datetime.now() -
datetime.timedelta ( weeks = number ) )
elif units in [ 'y', 'year', 'years' ]:
return ( datetime.datetime.now() -
datetime.timedelta ( weeks = number * 52 ) )
assert False

3
test.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
python -m unittest discover

42
testtimeparse.py Normal file
View File

@@ -0,0 +1,42 @@
from timeparse import *
import unittest
class TestRegexes(unittest.TestCase):
def setUp(self):
pass
def test_mins(self):
self.assertEqual(re.match(MINS, '32min').groupdict(),
{'mins': '32'})
self.assertEqual(re.match(MINS, '32mins').groupdict(),
{'mins': '32'})
self.assertEqual(re.match(MINS, '32minute').groupdict(),
{'mins': '32'})
self.assertEqual(re.match(MINS, '32minutes').groupdict(),
{'mins': '32'})
self.assertEqual(re.match(MINS, '32mins').groupdict(),
{'mins': '32'})
self.assertEqual(re.match(MINS, '32min').groupdict(),
{'mins': '32'})
def test_hrs(self):
self.assertEqual(re.match(HOURS, '32h').groupdict(),
{'hours': '32'})
self.assertEqual(re.match(HOURS, '32hr').groupdict(),
{'hours': '32'})
self.assertEqual(re.match(HOURS, '32hrs').groupdict(),
{'hours': '32'})
self.assertEqual(re.match(HOURS, '32hour').groupdict(),
{'hours': '32'})
self.assertEqual(re.match(HOURS, '32hours').groupdict(),
{'hours': '32'})
self.assertEqual(re.match(HOURS, '32 hours').groupdict(),
{'hours': '32'})
self.assertEqual(re.match(HOURS, '32 h').groupdict(),
{'hours': '32'})
def test_time(self):
self.assertEqual(
re.match(TIME + r'\s*$', '16h32m64s ').groupdict(),
{'hours': '16', 'secs': '64', 'mins': '32'})

43
timeparse.py Normal file
View File

@@ -0,0 +1,43 @@
# ------------------------------------------------------------
# implementation
# ------------------------------------------------------------
import re
YEARS = r'(?P<years>\d+)\s*(?:ys?|yrs?.?|years?)'
MONTHS = r'(?P<months>\d+)\s*(?:mos?.?|mths?.?|months?)'
DAYS = r'(?P<days>\d+)\s*(?:d|dys?|days?)'
HOURS = r'(?P<hours>\d+)\s*(?:h|hrs?|hours?)'
MINS = r'(?P<mins>\d+)\s*(?:m|(mins?)|(minutes?))'
SECS = r'(?P<secs>\d+)\s*(?:s|secs?|seconds?)'
SEPARATORS = r'[,/]'
OPT = lambda x: '(?:{x})?'.format(x=x, SEPARATORS=SEPARATORS)
OPTSEP = lambda x: '(?:{x}\s*(?:{SEPARATORS}\s*)?)?'.format(x=x, SEPARATORS=SEPARATORS)
#TIME = r'(?:{HOURS}\s*(?:{SEPARATORS}\s*)?)?(?:{MINS}\s*(?:{SEPARATORS}\s*)?\s*)?(?:{SECS})?'.format(HOURS=HOURS, SEPARATORS=SEPARATORS, MINS=MINS, SECS=SECS)
TIME = '{YEARS}\s*{MONTHS}\s*{DAYS}\s*{HOURS}\s*{MINS}\s*{SECS}'.format(
YEARS=OPTSEP(YEARS),
MONTHS=OPTSEP(MONTHS),
DAYS=OPTSEP(DAYS),
HOURS=OPTSEP(HOURS),
MINS=OPTSEP(MINS),
SECS=OPT(SECS))
#TIME = r'{HOURS}\s*{MINS}\s*{SECS}'.format(HOURS=HOURS, SEPARATORS=SEPARATORS, MINS=MINS, SECS=SECS)
def t(x, y):
if re.match(x, y):
print re.match(x, y).group(0)
print re.match(x, y).groupdict()
#RE =