From 0bb68b9653388a761108e503cace38c7d9a5a3af Mon Sep 17 00:00:00 2001 From: Will Roberts Date: Fri, 31 Jan 2014 21:06:09 +0100 Subject: [PATCH] initial checkin --- .gitignore | 2 ++ notes.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ test.sh | 3 ++ testtimeparse.py | 42 +++++++++++++++++++++++++++ timeparse.py | 43 ++++++++++++++++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 .gitignore create mode 100644 notes.py create mode 100755 test.sh create mode 100644 testtimeparse.py create mode 100644 timeparse.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3d74a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +*~ diff --git a/notes.py b/notes.py new file mode 100644 index 0000000..368a216 --- /dev/null +++ b/notes.py @@ -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 diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..5936b79 --- /dev/null +++ b/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +python -m unittest discover diff --git a/testtimeparse.py b/testtimeparse.py new file mode 100644 index 0000000..7863808 --- /dev/null +++ b/testtimeparse.py @@ -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'}) diff --git a/timeparse.py b/timeparse.py new file mode 100644 index 0000000..1921982 --- /dev/null +++ b/timeparse.py @@ -0,0 +1,43 @@ + +# ------------------------------------------------------------ +# implementation +# ------------------------------------------------------------ + +import re + +YEARS = r'(?P\d+)\s*(?:ys?|yrs?.?|years?)' + +MONTHS = r'(?P\d+)\s*(?:mos?.?|mths?.?|months?)' + +DAYS = r'(?P\d+)\s*(?:d|dys?|days?)' + +HOURS = r'(?P\d+)\s*(?:h|hrs?|hours?)' + +MINS = r'(?P\d+)\s*(?:m|(mins?)|(minutes?))' + +SECS = r'(?P\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 = + +