Skip to content

Commit ca095d2

Browse files
committed
move parse_times to its own class
1 parent 93879d0 commit ca095d2

2 files changed

Lines changed: 89 additions & 27 deletions

File tree

models/times.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from models.time import Time, TimeType
2+
import logging, os
3+
4+
logger = logging.getLogger(__name__)
5+
6+
class Times():
7+
start_time = None
8+
end_time = None
9+
10+
@classmethod
11+
def from_shortcut_string(cls, times_string, assume_type=None):
12+
"""
13+
create a time object from a string
14+
"""
15+
logger.debug("creating times object from shortcut: " + times_string)
16+
if times_string is None:
17+
raise TypeError("Cannot create Times Object from value None")
18+
19+
day = times_string.lower()
20+
21+
# set up some shortcut ranges
22+
allday = cls(Time(0, 0, TimeType.AM), Time(11, 59, TimeType.PM))
23+
workhours = cls(Time(9, 0, TimeType.AM), Time(5, 0, TimeType.PM))
24+
25+
if "24" in day:
26+
return allday
27+
elif "business" in day:
28+
return workhours
29+
elif "work" in day:
30+
return workhours
31+
elif "all day" in day:
32+
return allday
33+
34+
raise ValueError("string '" + times_string + "' does not match a known pattern")
35+
36+
@classmethod
37+
def from_parse_results(cls, result, assume_type=None):
38+
""" Takes values from the pyparsing results and converts them to the appropriate internal objects """
39+
# assumes that all three (hours, minutes, am_pm) are the same length
40+
res_dct = result.asDict()
41+
42+
start = res_dct.get("starttime")[0]
43+
starttime = Time.from_parse_results(start)
44+
45+
end = res_dct.get("endtime")[0]
46+
endtime = Time.from_parse_results(end)
47+
48+
if starttime.is_unknown() and assume_type is not None:
49+
starttime.set_type(assume_type)
50+
51+
if endtime.is_unknown() and assume_type is not None:
52+
endtime.set_type(assume_type)
53+
54+
55+
if starttime.is_am() and endtime.is_am() and starttime.get_hours() > endtime.get_hours():
56+
endtime.set_type(TimeType.PM)
57+
58+
return cls(starttime, endtime)
59+
60+
def __init__(self, start_time, end_time):
61+
if start_time is None or end_time is None:
62+
raise TypeError("Cannot create Times Object from value None")
63+
64+
logger.debug("creating times from " + str(start_time) + " and " + str(end_time))
65+
66+
self.start_time = start_time
67+
self.end_time = end_time
68+
69+
def get_start_time(self):
70+
return self.start_time
71+
72+
def get_end_time(self):
73+
return self.end_time
74+
75+
def __str__(self):
76+
return self.start_time + to + self.end_time
77+
78+
79+
def __eq__(self, other):
80+
if not isinstance(other, Times):
81+
# don't attempt to compare against unrelated types
82+
raise NotImplementedError()
83+
return self.start_time == other.start_time and self.end_time == other.end_time
84+
85+

parse_opening_hours.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from models.day import Day, DaysEnum
1515
from models.days import Days
1616
from models.time import Time, TimeType
17+
from models.times import Times
1718
import unicodedata
1819
import os
1920
import logging
@@ -48,45 +49,21 @@ def json(self, assume_type=None):
4849
# TODO: move parse days and parse times out of the json() function
4950
days = Days.from_parse_results(self.openinghours)
5051

51-
start_time, end_time = parse_times(self.openinghours, assume_type=assume_type or self.assume_type)
52+
times = Times.from_parse_results(self.openinghours, assume_type=assume_type or self.assume_type)
5253

5354

5455
for day in days:
5556
opening_hours_json.append(
5657
create_entry(
5758
str(day),
58-
start_time,
59-
end_time
59+
str(times.get_start_time().get_as_military_time()),
60+
str(times.get_end_time().get_as_military_time())
6061
)
6162
)
6263

6364
return opening_hours_json
6465

65-
def parse_times(result, assume_type=None):
66-
""" Takes values from the pyparsing results and converts them to the appropriate internal objects """
67-
# assumes that all three (hours, minutes, am_pm) are the same length
68-
res_dct = result.asDict()
6966

70-
start = res_dct.get("starttime")[0]
71-
starttime = Time.from_parse_results(start)
72-
73-
end = res_dct.get("endtime")[0]
74-
endtime = Time.from_parse_results(end)
75-
76-
if starttime.is_unknown() and assume_type is not None:
77-
starttime.set_type(assume_type)
78-
79-
if endtime.is_unknown() and assume_type is not None:
80-
endtime.set_type(assume_type)
81-
82-
83-
if starttime.is_am() and endtime.is_am() and starttime.get_hours() > endtime.get_hours():
84-
endtime.set_type(TimeType.PM)
85-
86-
return (
87-
str(starttime.get_as_military_time()),
88-
str(endtime.get_as_military_time())
89-
)
9067

9168
def create_entry(day, opening, closing, notes=None):
9269
"""Creates a new JSON object representing a single time slot for a single day"""

0 commit comments

Comments
 (0)