def get_transition(self, user_input): if len(self._transitions) == 1: return self._transitions[0] elif self._transition_fn is None: return self._transitions[self.message.last_options.index( user_input)] else: idx = self._transition_fn(user_input) if not math_tools.is_int(idx): raise IOError("Transition function must return an index") return self._transitions[idx]
def input_multiple_choice(message): if type(message) is not Message: raise ValueError("Must input message class") response = None while response not in range(len(message.options)): response_str = input(">>> ") if math_tools.is_int(response_str): response = int(response_str) return message.options[response]
def get_session_date_range(self, session_num, output_format=None): if not math_tools.is_int(session_num) and session_num < 0: raise ValueError("'session_num' must be a non-negative int") first_day = self.get_first_day_of_session(session_num) last_day = self.get_last_day_of_session(session_num) return dates.get_date_range( start_date=first_day, end_date=last_day + datetime.timedelta(days=1), increment_days=1, output_format=output_format, )
def get_replacement(self, key, index=None, is_wrap_index=True): choices = self.values(key) if index is None: return random.choice(choices) else: if not is_wrap_index and index > self.get_num_variations(key): raise IndexError if math_tools.is_int(index): index = int(index) else: raise ValueError("Index must be an int") i = index % self.get_num_variations(key) return choices[i]
def get_date_range(start_date, end_date, increment_days=1, output_format=None): if not math_tools.is_int(increment_days): raise ValueError("'increment_days' must be an int") if increment_days < 0: raise ValueError("'increment_days' must be nonnegative") if start_date > end_date: raise IOError("'start_date' must be less than 'end_date'") date = start_date date_range = [] while date < end_date: date_range.append(date) date += datetime.timedelta(days=increment_days) if output_format is None: return date_range elif type(output_format) is str: return [ datetime.datetime.strftime(date, output_format) for date in date_range ]
def _build(self): self.create_key_if_not_exists( StateDb.Keys.USER_NAME, tests=[ lambda x: type(x) is str, lambda x: len(x) > 1, ], ) self.create_key_if_not_exists( StateDb.Keys.FIRST_MEETING, tests=lambda x: type(x) is datetime.datetime) self.create_key_if_not_exists(StateDb.Keys.IS_DONE_AM_CHECKIN_TODAY, tests=lambda x: type(x) is bool) self.create_key_if_not_exists(StateDb.Keys.IS_DONE_PM_CHECKIN_TODAY, tests=lambda x: type(x) is bool) self.create_key_if_not_exists(StateDb.Keys.IS_MISSED_PM_YESTERDAY, tests=lambda x: type(x) is bool) self.create_key_if_not_exists(StateDb.Keys.IS_REDO_SCHEDULE, tests=lambda x: type(x) is bool) self.create_key_if_not_exists(StateDb.Keys.AM_CHECKIN_TIME, tests=lambda x: type(x) is datetime.time) self.create_key_if_not_exists(StateDb.Keys.PM_CHECKIN_TIME, tests=lambda x: type(x) is datetime.time) self.create_key_if_not_exists(StateDb.Keys.WALK_TIME, tests=lambda x: type(x) is datetime.time) self.create_key_if_not_exists(StateDb.Keys.WALK_PLACES, ['park', 'neighborhood', 'inside']) self.create_key_if_not_exists(StateDb.Keys.HOW_BUSY, [ 'Plan with my calendar', 'Walk earlier or later in the day', 'Walk during meetings', ]) self.create_key_if_not_exists(StateDb.Keys.FAIL_WHY, ) self.create_key_if_not_exists(StateDb.Keys.HOW_MOTIVATE, [ 'Think of the health benefits', 'Walk with a friend', 'Listen to music while walking', ]) self.create_key_if_not_exists(StateDb.Keys.HOW_REMEMBER, [ 'Walk before or after lunch', 'Set an alarm', 'Put it on my calendar or todo list', ]) self.create_key_if_not_exists( StateDb.Keys.BKT_pL, 0.0, tests=lambda x: 0 <= x < 1, ) self.create_key_if_not_exists( StateDb.Keys.BKT_pT, 0.02, tests=lambda x: 0 <= x <= 1, ) self.create_key_if_not_exists( StateDb.Keys.BKT_pS, 0.3, tests=lambda x: 0 <= x <= 1, ) self.create_key_if_not_exists( StateDb.Keys.BKT_pG, 0.5, tests=lambda x: 0 <= x <= 1, ) self.create_key_if_not_exists( StateDb.Keys.STEPS_GOAL, tests=lambda x: math_tools.is_int(x), ) self.create_key_if_not_exists(StateDb.Keys.IS_MET_GOAL, ) self.create_key_if_not_exists( StateDb.Keys.SUGGESTED_STEPS_TODAY, tests=lambda x: math_tools.is_int(x), ) self.create_key_if_not_exists( StateDb.Keys.MIN_SUGGESTED_STEPS_TODAY, tests=lambda x: math_tools.is_int(x), ) self.create_key_if_not_exists( StateDb.Keys.MAX_SUGGESTED_STEPS_TODAY, tests=lambda x: math_tools.is_int(x), ) self.create_key_if_not_exists( StateDb.Keys.STEPS_TODAY, tests=lambda x: math_tools.is_int(x), ) self.create_key_if_not_exists( StateDb.Keys.STEPS_THIS_WEEK, tests=lambda x: math_tools.is_int(x), ) self.create_key_if_not_exists( StateDb.Keys.STEPS_LAST_WEEK, tests=lambda x: math_tools.is_int(x), ) self.create_key_if_not_exists( StateDb.Keys.LAST_FITBIT_SYNC, tests=lambda x: type(x) is datetime.datetime) self.create_key_if_not_exists(StateDb.Keys.LAST_AM_CHECKIN_DATE, tests=lambda x: type(x) is datetime.date) self.create_key_if_not_exists(StateDb.Keys.LAST_DAY_UPDATE_DATE, tests=lambda x: type(x) is datetime.date) self.create_key_if_not_exists(StateDb.Keys.DAY_OFF, ) self.create_key_if_not_exists(StateDb.Keys.PSYCH_QUESTION_INDEX, 0, tests=[ lambda x: math_tools.is_int(x), ]) self.create_key_if_not_exists(StateDb.Keys.PSYCH_QUESTION_ANSWERS, )
def _build(self): check_non_negative_int = [ lambda x: math_tools.is_int(x), lambda x: x >= 0, ] self.create_key_if_not_exists( ParameterDb.Keys.FINAL_STEPS_GOAL, 10000, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.MIN_WEEKLY_STEPS_GOAL, 2000, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.WEEKS_WITH_ROBOT, 6, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.MAX_NUM_QUESTIONS, 3, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.NUM_OPTIONS_TO_DISPLAY, 3, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.STEPS_PER_MINUTE_FOR_ACTIVE, 60, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.ACTIVE_MINS_TO_REGISTER_ACTIVITY, 10, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.CONSECUTIVE_MINS_INACTIVE_BEFORE_BREAKING_ACTIVITY_STREAK, 2, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.INACTIVE_INTERACTION_TIMEOUT_SECONDS, 360, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.MINS_BEFORE_WARNING_ABOUT_FITBIT_NOT_SYNCING, 15, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.FITBIT_PULL_RATE_MINS, 2, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.MINS_BEFORE_ALLOW_CHECKIN, 60, tests=check_non_negative_int ) self.create_key_if_not_exists( ParameterDb.Keys.MINS_AFTER_ALLOW_CHECKIN, 60, tests=check_non_negative_int )
def test_is_int(self): for i in range(-100, 100): self.assertTrue(math_tools.is_int(i)) for v in ['a', 'abc', None, 9.1, math.pi, math.nan]: self.assertFalse(math_tools.is_int(v))