def test_time_period_add(): """ flowmachine.utils.time_period_add does what it says on the tin. """ assert time_period_add("2016-01-01", 3) == "2016-01-04" assert time_period_add("2017-12-31", 1) == "2018-01-01"
def test_time_period_add_other_units(): """ flowmachine.utils.time_period_add can also add hours and minutes """ assert time_period_add("2016-01-01", 3, unit="hours") == "2016-01-01 03:00:00" assert (time_period_add("2017-12-31 01:10:00", 50, unit="minutes") == "2017-12-31 02:00:00")
def _get_start_stops(self): """ Gets two lists, one for the start dates and one for the stop dates. """ starts = [ time_period_add(self.start, i * self.period_length, self.period_unit) for i in range(self.total_periods) ] stops = [ time_period_add(self.start, (i + 1) * self.period_length, self.period_unit) for i in range(self.total_periods) ] return starts, stops
def __init__( self, start: str, total_periods: int, period_length: int = 1, period_unit: str = "days", hours: Union[str, Tuple[int, int]] = "all", table: Union[str, List[str]] = "all", subscriber_identifier: str = "msisdn", subscriber_subset: Optional[Query] = None, ): self.start = start self.total_periods = total_periods self.period_length = period_length if period_unit not in self.allowed_units: raise ValueError("`period_unit` must be one of {}".format( self.allowed_units)) self.period_unit = period_unit self.starts, self.stops = self._get_start_stops() # For convenience also store when the whole thing ends self.stop_date = time_period_add( self.start, self.total_periods * self.period_length) # This will be a long form table of unique subscribers in each time period # i.e. a subscriber can appear more than once in this list, up to a maximum # of the total time periods. self.unique_subscribers_table = self._get_unioned_subscribers_list( hours=hours, table=table, subscriber_identifier=subscriber_identifier, subscriber_subset=subscriber_subset, ) super().__init__()
def __init__(self, start, total_periods, period_length=1, period_unit="days", **kwargs): """ """ self.start = start self.total_periods = total_periods self.period_length = period_length if period_unit not in self.allowed_units: raise ValueError("`period_unit` must be one of {}".format( self.allowed_units)) self.period_unit = period_unit self.starts, self.stops = self._get_start_stops() # For convenience also store when the whole thing ends self.stop_date = time_period_add( self.start, self.total_periods * self.period_length) # This will be a long form table of unique subscribers in each time period # i.e. a subscriber can appear more than once in this list, up to a maximum # of the total time periods. self.unique_subscribers_table = self._get_unioned_subscribers_list( **kwargs) super().__init__()