@@ -29,7 +29,12 @@ class HourlyInterval(object):
29
29
def __init__ (self , start_time , end_time , interval_value ):
30
30
self .start_time = start_time
31
31
self .end_time = end_time
32
- self .interval = interval_value
32
+
33
+ # interval should be a tuple, if it is not, assign as a tuple with single value
34
+ if isinstance (interval_value , tuple ):
35
+ self .interval = interval_value
36
+ else :
37
+ self .interval = (interval_value ,)
33
38
34
39
@property
35
40
def _frequency (self ):
@@ -60,25 +65,44 @@ def interval(self):
60
65
return self ._interval
61
66
62
67
@interval .setter
63
- def interval (self , interval ):
68
+ def interval (self , intervals ):
64
69
VALID_INTERVALS = {0.25 , 0.5 , 1 , 2 , 4 , 6 , 8 , 12 }
65
- if float (interval ) not in VALID_INTERVALS :
66
- error = "Invalid interval {} not in {}" .format (interval , str (VALID_INTERVALS ))
67
- raise ValueError (error )
70
+ for interval in intervals :
71
+ # if an hourly interval is a string, then it is a weekDay interval
72
+ if isinstance (interval , str ) and not interval .isnumeric () and not hasattr (IntervalItem .Day , interval ):
73
+ error = "Invalid weekDay interval {}" .format (interval )
74
+ raise ValueError (error )
75
+
76
+ # if an hourly interval is a number, it is an hours or minutes interval
77
+ if isinstance (interval , (int , float )) and float (interval ) not in VALID_INTERVALS :
78
+ error = "Invalid interval {} not in {}" .format (interval , str (VALID_INTERVALS ))
79
+ raise ValueError (error )
68
80
69
- self ._interval = interval
81
+ self ._interval = intervals
70
82
71
83
def _interval_type_pairs (self ):
72
- # We use fractional hours for the two minute-based intervals.
73
- # Need to convert to minutes from hours here
74
- if self .interval in {0.25 , 0.5 }:
75
- calculated_interval = int (self .interval * 60 )
76
- interval_type = IntervalItem .Occurrence .Minutes
77
- else :
78
- calculated_interval = self .interval
79
- interval_type = IntervalItem .Occurrence .Hours
84
+ interval_type_pairs = []
85
+ for interval in self .interval :
86
+ # We use fractional hours for the two minute-based intervals.
87
+ # Need to convert to minutes from hours here
88
+ if interval in {0.25 , 0.5 }:
89
+ calculated_interval = int (interval * 60 )
90
+ interval_type = IntervalItem .Occurrence .Minutes
91
+
92
+ interval_type_pairs .append ((interval_type , str (calculated_interval )))
93
+ else :
94
+ # if the interval is a non-numeric string, it will always be a weekDay
95
+ if isinstance (interval , str ) and not interval .isnumeric ():
96
+ interval_type = IntervalItem .Occurrence .WeekDay
97
+
98
+ interval_type_pairs .append ((interval_type , str (interval )))
99
+ # otherwise the interval is hours
100
+ else :
101
+ interval_type = IntervalItem .Occurrence .Hours
80
102
81
- return [(interval_type , str (calculated_interval ))]
103
+ interval_type_pairs .append ((interval_type , str (interval )))
104
+
105
+ return interval_type_pairs
82
106
83
107
84
108
class DailyInterval (object ):
@@ -105,8 +129,45 @@ def interval(self):
105
129
return self ._interval
106
130
107
131
@interval .setter
108
- def interval (self , interval ):
109
- self ._interval = interval
132
+ def interval (self , intervals ):
133
+ VALID_INTERVALS = {0.25 , 0.5 , 1 , 2 , 4 , 6 , 8 , 12 }
134
+
135
+ for interval in intervals :
136
+ # if an hourly interval is a string, then it is a weekDay interval
137
+ if isinstance (interval , str ) and not interval .isnumeric () and not hasattr (IntervalItem .Day , interval ):
138
+ error = "Invalid weekDay interval {}" .format (interval )
139
+ raise ValueError (error )
140
+
141
+ # if an hourly interval is a number, it is an hours or minutes interval
142
+ if isinstance (interval , (int , float )) and float (interval ) not in VALID_INTERVALS :
143
+ error = "Invalid interval {} not in {}" .format (interval , str (VALID_INTERVALS ))
144
+ raise ValueError (error )
145
+
146
+ self ._interval = intervals
147
+
148
+ def _interval_type_pairs (self ):
149
+ interval_type_pairs = []
150
+ for interval in self .interval :
151
+ # We use fractional hours for the two minute-based intervals.
152
+ # Need to convert to minutes from hours here
153
+ if interval in {0.25 , 0.5 }:
154
+ calculated_interval = int (interval * 60 )
155
+ interval_type = IntervalItem .Occurrence .Minutes
156
+
157
+ interval_type_pairs .append ((interval_type , str (calculated_interval )))
158
+ else :
159
+ # if the interval is a non-numeric string, it will always be a weekDay
160
+ if isinstance (interval , str ) and not interval .isnumeric ():
161
+ interval_type = IntervalItem .Occurrence .WeekDay
162
+
163
+ interval_type_pairs .append ((interval_type , str (interval )))
164
+ # otherwise the interval is hours
165
+ else :
166
+ interval_type = IntervalItem .Occurrence .Hours
167
+
168
+ interval_type_pairs .append ((interval_type , str (interval )))
169
+
170
+ return interval_type_pairs
110
171
111
172
112
173
class WeeklyInterval (object ):
@@ -146,7 +207,12 @@ def _interval_type_pairs(self):
146
207
class MonthlyInterval (object ):
147
208
def __init__ (self , start_time , interval_value ):
148
209
self .start_time = start_time
149
- self .interval = str (interval_value )
210
+
211
+ # interval should be a tuple, if it is not, assign as a tuple with single value
212
+ if isinstance (interval_value , tuple ):
213
+ self .interval = interval_value
214
+ else :
215
+ self .interval = (interval_value ,)
150
216
151
217
@property
152
218
def _frequency (self ):
@@ -167,24 +233,24 @@ def interval(self):
167
233
return self ._interval
168
234
169
235
@interval .setter
170
- def interval (self , interval_value ):
171
- error = "Invalid interval value for a monthly frequency: {}." .format (interval_value )
172
-
236
+ def interval (self , interval_values ):
173
237
# This is weird because the value could be a str or an int
174
238
# The only valid str is 'LastDay' so we check that first. If that's not it
175
239
# try to convert it to an int, if that fails because it's an incorrect string
176
240
# like 'badstring' we catch and re-raise. Otherwise we convert to int and check
177
241
# that it's in range 1-31
242
+ for interval_value in interval_values :
243
+ error = "Invalid interval value for a monthly frequency: {}." .format (interval_value )
178
244
179
- if interval_value != "LastDay" :
180
- try :
181
- if not (1 <= int (interval_value ) <= 31 ):
182
- raise ValueError (error )
183
- except ValueError :
184
- if interval_value != "LastDay" :
185
- raise ValueError (error )
245
+ if interval_value != "LastDay" :
246
+ try :
247
+ if not (1 <= int (interval_value ) <= 31 ):
248
+ raise ValueError (error )
249
+ except ValueError :
250
+ if interval_value != "LastDay" :
251
+ raise ValueError (error )
186
252
187
- self ._interval = str ( interval_value )
253
+ self ._interval = interval_values
188
254
189
255
def _interval_type_pairs (self ):
190
256
return [(IntervalItem .Occurrence .MonthDay , self .interval )]
0 commit comments