-
Notifications
You must be signed in to change notification settings - Fork 421
/
interval_item.py
281 lines (221 loc) · 8.96 KB
/
interval_item.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from .property_decorators import property_is_valid_time, property_not_nullable
class IntervalItem(object):
class Frequency:
Hourly = "Hourly"
Daily = "Daily"
Weekly = "Weekly"
Monthly = "Monthly"
class Occurrence:
Minutes = "minutes"
Hours = "hours"
WeekDay = "weekDay"
MonthDay = "monthDay"
class Day:
Sunday = "Sunday"
Monday = "Monday"
Tuesday = "Tuesday"
Wednesday = "Wednesday"
Thursday = "Thursday"
Friday = "Friday"
Saturday = "Saturday"
LastDay = "LastDay"
class HourlyInterval(object):
def __init__(self, start_time, end_time, interval_value):
self.start_time = start_time
self.end_time = end_time
# interval should be a tuple, if it is not, assign as a tuple with single value
if isinstance(interval_value, tuple):
self.interval = interval_value
else:
self.interval = (interval_value,)
def __repr__(self):
return f"<{self.__class__.__name__} start={self.start_time} end={self.end_time} interval={self.interval}>"
@property
def _frequency(self):
return IntervalItem.Frequency.Hourly
@property
def start_time(self):
return self._start_time
@start_time.setter
@property_is_valid_time
@property_not_nullable
def start_time(self, value):
self._start_time = value
@property
def end_time(self):
return self._end_time
@end_time.setter
@property_is_valid_time
@property_not_nullable
def end_time(self, value):
self._end_time = value
@property
def interval(self):
return self._interval
@interval.setter
def interval(self, intervals):
VALID_INTERVALS = {0.25, 0.5, 1, 2, 4, 6, 8, 12, 24}
for interval in intervals:
# if an hourly interval is a string, then it is a weekDay interval
if isinstance(interval, str) and not interval.isnumeric() and not hasattr(IntervalItem.Day, interval):
error = "Invalid weekDay interval {}".format(interval)
raise ValueError(error)
# if an hourly interval is a number, it is an hours or minutes interval
if isinstance(interval, (int, float)) and float(interval) not in VALID_INTERVALS:
error = "Invalid interval {} not in {}".format(interval, str(VALID_INTERVALS))
raise ValueError(error)
self._interval = intervals
def _interval_type_pairs(self):
interval_type_pairs = []
for interval in self.interval:
# We use fractional hours for the two minute-based intervals.
# Need to convert to minutes from hours here
if interval in {0.25, 0.5}:
calculated_interval = int(interval * 60)
interval_type = IntervalItem.Occurrence.Minutes
interval_type_pairs.append((interval_type, str(calculated_interval)))
else:
# if the interval is a non-numeric string, it will always be a weekDay
if isinstance(interval, str) and not interval.isnumeric():
interval_type = IntervalItem.Occurrence.WeekDay
interval_type_pairs.append((interval_type, str(interval)))
# otherwise the interval is hours
else:
interval_type = IntervalItem.Occurrence.Hours
interval_type_pairs.append((interval_type, str(interval)))
return interval_type_pairs
class DailyInterval(object):
def __init__(self, start_time, *interval_values):
self.start_time = start_time
self.interval = interval_values
def __repr__(self):
return f"<{self.__class__.__name__} start={self.start_time} interval={self.interval}>"
@property
def _frequency(self):
return IntervalItem.Frequency.Daily
@property
def start_time(self):
return self._start_time
@start_time.setter
@property_is_valid_time
@property_not_nullable
def start_time(self, value):
self._start_time = value
@property
def interval(self):
return self._interval
@interval.setter
def interval(self, intervals):
VALID_INTERVALS = {0.25, 0.5, 1, 2, 4, 6, 8, 12, 24}
for interval in intervals:
# if an hourly interval is a string, then it is a weekDay interval
if isinstance(interval, str) and not interval.isnumeric() and not hasattr(IntervalItem.Day, interval):
error = "Invalid weekDay interval {}".format(interval)
raise ValueError(error)
# if an hourly interval is a number, it is an hours or minutes interval
if isinstance(interval, (int, float)) and float(interval) not in VALID_INTERVALS:
error = "Invalid interval {} not in {}".format(interval, str(VALID_INTERVALS))
raise ValueError(error)
self._interval = intervals
def _interval_type_pairs(self):
interval_type_pairs = []
for interval in self.interval:
# We use fractional hours for the two minute-based intervals.
# Need to convert to minutes from hours here
if interval in {0.25, 0.5}:
calculated_interval = int(interval * 60)
interval_type = IntervalItem.Occurrence.Minutes
interval_type_pairs.append((interval_type, str(calculated_interval)))
else:
# if the interval is a non-numeric string, it will always be a weekDay
if isinstance(interval, str) and not interval.isnumeric():
interval_type = IntervalItem.Occurrence.WeekDay
interval_type_pairs.append((interval_type, str(interval)))
# otherwise the interval is hours
else:
interval_type = IntervalItem.Occurrence.Hours
interval_type_pairs.append((interval_type, str(interval)))
return interval_type_pairs
class WeeklyInterval(object):
def __init__(self, start_time, *interval_values):
self.start_time = start_time
self.interval = interval_values
def __repr__(self):
return f"<{self.__class__.__name__} start={self.start_time} interval={self.interval}>"
@property
def _frequency(self):
return IntervalItem.Frequency.Weekly
@property
def start_time(self):
return self._start_time
@start_time.setter
@property_is_valid_time
@property_not_nullable
def start_time(self, value):
self._start_time = value
@property
def interval(self):
return self._interval
@interval.setter
def interval(self, interval_values):
if not all(hasattr(IntervalItem.Day, day) for day in interval_values):
raise ValueError("Invalid week day defined " + str(interval_values))
self._interval = interval_values
def _interval_type_pairs(self):
return [(IntervalItem.Occurrence.WeekDay, day) for day in self.interval]
class MonthlyInterval(object):
def __init__(self, start_time, interval_value):
self.start_time = start_time
# interval should be a tuple, if it is not, assign as a tuple with single value
if isinstance(interval_value, tuple):
self.interval = interval_value
else:
self.interval = (interval_value,)
def __repr__(self):
return f"<{self.__class__.__name__} start={self.start_time} interval={self.interval}>"
@property
def _frequency(self):
return IntervalItem.Frequency.Monthly
@property
def start_time(self):
return self._start_time
@start_time.setter
@property_is_valid_time
@property_not_nullable
def start_time(self, value):
self._start_time = value
@property
def interval(self):
return self._interval
@interval.setter
def interval(self, interval_values):
# Valid monthly intervals strings can contain any of the following
# day numbers (1-31) (integer or string)
# relative day within the month (First, Second, ... Last)
# week days (Sunday, Monday, ... LastDay)
VALID_INTERVALS = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"LastDay",
"First",
"Second",
"Third",
"Fourth",
"Fifth",
"Last",
]
for value in range(1, 32):
VALID_INTERVALS.append(str(value))
VALID_INTERVALS.append(value)
for interval_value in interval_values:
if interval_value not in VALID_INTERVALS:
error = f"Invalid monthly interval: {interval_value}"
raise ValueError(error)
self._interval = interval_values
def _interval_type_pairs(self):
return [(IntervalItem.Occurrence.MonthDay, self.interval)]