def parse_rel_time(time_str): '''Takes a string representing an integer in range 0, 1440.''' if not is_relative_time(time_str): raise TypeError("Expected a relativ time. A string\ representing an integer between 0 and 1440. Got: " + time_str) return int(time_str)
def _get_diffusioned_intervall(self, sequence): '''Returns a tuple of start and end time.''' sequence_id = sequence.get_id() end_tm_str = sequence.get_end() start_tm_str = sequence.get_start() if sequence_id not in self.diffusions: # if deffusion is not calculated: if is_relative_time(start_tm_str[0]): # if start is relativ to the end time # parse start and end time start_tm = (parse_rel_time(start_tm_str[0]), parse_rel_time(start_tm_str[1])) end_tm = (parse_abs_time(end_tm_str[0]), parse_rel_time(end_tm_str[1])) # calculate randomized end time rand_end = end_tm[0] if end_tm[1] != 0: rand_end += random.randint(-end_tm[1], end_tm[1]) rand_end %= UNIT_PER_DAY # calculate start time depending on end. duration = start_tm[0] if start_tm[1] != 0: # randomize start time if nessesary duration += random.randint(-start_tm[1], start_tm[1]) rand_start = (rand_end - duration) % UNIT_PER_DAY if duration <= 0: rand_start = rand_end self.diffusions[sequence_id] = (rand_start, rand_end) elif is_relative_time(end_tm_str[0]): # if end is relativ to start time # parse start and end time start_tm = (parse_abs_time(start_tm_str[0]), parse_rel_time(start_tm_str[1])) end_tm = (parse_rel_time(end_tm_str[0]), parse_rel_time(end_tm_str[1])) # calculate rand_start = start_tm[0] if start_tm[1] != 0: rand_start += random.randint(-start_tm[1], start_tm[1]) rand_start %= UNIT_PER_DAY # calculate end time depending on start. duration = end_tm[0] if end_tm[1] != 0: # randomize end time if nessesary duration += random.randint(-end_tm[1], end_tm[1]) rand_end = (rand_start + duration) % UNIT_PER_DAY if duration <= 0: rand_end = rand_start self.diffusions[sequence_id] = (rand_start, rand_end) else: # if both are absolute times. start_tm = (parse_abs_time(start_tm_str[0]), parse_rel_time(start_tm_str[1])) end_tm = (parse_abs_time(end_tm_str[0]), parse_rel_time(end_tm_str[1])) # calculate start rand_start = start_tm[0] + random.randint(-start_tm[1], start_tm[1]) # calculate end rand_end = end_tm[0] + random.randint(-end_tm[1], end_tm[1]) # calculate duration of on time befor and after randomization tm_diff = end_tm[0] - start_tm[0] rand_tm_diff = rand_end - rand_start # if order of start and end time switched, make the sequence disapear. # [morning]--[start]+++[end]---[evening] => [morning]++[end]--[start]++[evening] if tm_diff * rand_tm_diff <= 0: rand_start = rand_end self.diffusions[sequence_id] = (rand_start, rand_end) return self.diffusions[sequence_id]
def _get_diffusioned_intervall(self, sequence): '''Returns a tuple of start and end time.''' sequence_id = sequence._id end_tm_str = (sequence.end_time, sequence.end_range) start_tm_str = (sequence.start_time, sequence.start_range) if sequence_id not in self.diffusions: # if deffusion is not calculated: if is_relative_time(start_tm_str[0]): # if start is relativ to the end time # parse start and end time start_tm = (parse_rel_time(start_tm_str[0]), parse_rel_time(start_tm_str[1])) end_tm = (parse_abs_time(end_tm_str[0]), parse_rel_time(end_tm_str[1])) # calculate randomized end time rand_end = end_tm[0] if end_tm[1] != 0: rand_end += random.randint(-end_tm[1], end_tm[1]) rand_end %= UNIT_PER_DAY # calculate start time depending on end. duration = start_tm[0] if start_tm[1] != 0: # randomize start time if nessesary duration += random.randint(-start_tm[1], start_tm[1]) rand_start = (rand_end - duration) % UNIT_PER_DAY if duration <= 0: rand_start = rand_end self.diffusions[sequence_id] = (rand_start, rand_end) elif is_relative_time(end_tm_str[0]): # if end is relativ to start time # parse start and end time start_tm = (parse_abs_time(start_tm_str[0]), parse_rel_time(start_tm_str[1])) end_tm = (parse_rel_time(end_tm_str[0]), parse_rel_time(end_tm_str[1])) # calculate rand_start = start_tm[0] if start_tm[1] != 0: rand_start += random.randint(-start_tm[1], start_tm[1]) rand_start %= UNIT_PER_DAY # calculate end time depending on start. duration = end_tm[0] if end_tm[1] != 0: # randomize end time if nessesary duration += random.randint(-end_tm[1], end_tm[1]) rand_end = (rand_start + duration) % UNIT_PER_DAY if duration <= 0: rand_end = rand_start self.diffusions[sequence_id] = (rand_start, rand_end) else: # if both are absolute times. start_tm = (parse_abs_time(start_tm_str[0]), parse_rel_time(start_tm_str[1])) end_tm = (parse_abs_time(end_tm_str[0]), parse_rel_time(end_tm_str[1])) # calculate start rand_start = start_tm[0] + random.randint(-start_tm[1], start_tm[1]) # calculate end rand_end = end_tm[0] + random.randint(-end_tm[1], end_tm[1]) # calculate duration of on time befor and after randomization tm_diff = end_tm[0] - start_tm[0] rand_tm_diff = rand_end - rand_start # if order of start and end time switched, make the sequence disapear. # [morning]--[start]+++[end]---[evening] => [morning]++[end]--[start]++[evening] if tm_diff * rand_tm_diff <= 0: rand_start = rand_end self.diffusions[sequence_id] = (rand_start, rand_end) return self.diffusions[sequence_id]