def __init__(self, main_tags, option_tags): main_tags = lists.make_sure_is_iterable(main_tags) option_tags = lists.make_sure_is_iterable(option_tags) for tag in main_tags: if tag in option_tags: raise ValueError("Option tags cannot be in main tags") for tag in option_tags: if tag in main_tags: raise ValueError("Main tags cannot be in option tags") self._main_tags = main_tags self._option_tags = option_tags
def __init__(self, possible_plans): possible_plans = lists.make_sure_is_iterable(possible_plans) self._possible_items = possible_plans.copy() self._plan = [] self._pre_hook = [] self._post_hook = []
def _do_tests_pass(message, result): if message.tests is not None: tests = lists.make_sure_is_iterable(message.tests) for test in tests: if test(result) is False: return False return True
def _create_key(self, key, value=None, tests=()): if key in self: raise KeyError("Key already exists") tests = lists.make_sure_is_iterable(tests) db = self._load_database() db[key] = TestedSetterAndGetter(value, *tests) self._save_database(db)
def _combine_dicts(dict1, dict2): if dict1 is None: dict1 = {} if dict2 is None: dict2 = {} for key2 in dict2: if key2 in dict1: list1 = lists.make_sure_is_iterable(dict1[key2]) list2 = lists.make_sure_is_iterable(dict2[key2]) dict1[ key2] = VarietyPopulator._combine_lists_without_duplicates( list1, list2) else: list2 = lists.make_sure_is_iterable(dict2[key2]) dict1[ key2] = VarietyPopulator._combine_lists_without_duplicates( [], list2) return dict1
def wildcard_search_in_list(pattern, list_, wildcard_symbol='*'): list_ = lists.make_sure_is_iterable(list_) idxs = [] for idx in range(len(list_)): if is_wildcard_match( pattern, list_[idx], wildcard_symbol=wildcard_symbol ): idxs.append(idx) return [list_[i] for i in idxs]
def _get_total_active_steps(self, dates=None): if dates is None: return 0 dates = lists.make_sure_is_iterable(dates) steps = 0 for date in dates: steps += self._get_total_active_steps_one_date(date) return steps
def _create_dict( files, variations_dict=None, ): files = lists.make_sure_is_iterable(files) for f in files: with open(f) as variation_file: new_dict = json.load(variation_file) variations_dict = VarietyPopulator._combine_dicts( variations_dict, new_dict) return variations_dict
def _get_last_n_list(list_, n, exclude_values=None): exclude_values = lists.make_sure_is_iterable(exclude_values) list_ = lists.remove_repeats(list_, is_remove_from_front=False) out = [] idx = len(list_)-1 while len(out) < n and idx >= 0: value = list_[idx] if value not in exclude_values: out.append(value) idx -= 1 return out
def insert(self, plan, pre_hook=None, post_hook=None): plan = lists.make_sure_is_iterable(plan) if not self._is_valid_plan(plan): raise ValueError(f"Invalid plan: '{plan}'") if pre_hook is not None: pre_hook = lists.make_sure_is_iterable(pre_hook) else: pre_hook = [None] * len(plan) if len(plan) != len(pre_hook): raise ValueError("Pre hooks must be the same length as plans") if post_hook is not None: post_hook = lists.make_sure_is_iterable(post_hook) else: post_hook = [None] * len(plan) if len(plan) != len(post_hook): raise ValueError("Post hooks must be the same length as plans") for i in range(len(plan)): self._insert_one(plan[i], pre_hook[i], post_hook[i])
def update(self, observations): observations = lists.make_sure_is_iterable(observations) for o in observations: if o is True: pL_ = (self._pL * (1 - self._pS)) / (self._pL * (1 - self._pS) + (1 - self._pL) * self._pG) else: pL_ = (self._pL * self._pS) / (self._pL * self._pS + (1 - self._pL) * (1 - self._pG)) self._pL = pL_ return Bkt(self._pL, self._pT, self._pS, self._pG)
def test_make_sure_is_iterable(self): self.assertEqual( [None], lists.make_sure_is_iterable(None) ) self.assertEqual( [1], lists.make_sure_is_iterable(1) ) self.assertEqual( ['obj'], lists.make_sure_is_iterable('obj') ) self.assertEqual( [], lists.make_sure_is_iterable([]) ) self.assertEqual( [1, 2, 3], lists.make_sure_is_iterable([1, 2, 3]) ) self.assertEqual( (1, 2, 3), lists.make_sure_is_iterable((1, 2, 3)) )
def __init__( self, name, transitions, message=None, content=None, options=None, message_type=None, args=None, result_convert_from_str_fn=str, result_db_key=None, is_append_result=False, tests=None, is_confirm=False, error_message="Please enter a valid input", error_options=('Okay', 'Oops'), text_populator=None, transition_fn=None, ): self._name = name if message is None: message = Message( content=content, options=options, message_type=message_type, name=name, args=args, result_convert_from_str_fn=result_convert_from_str_fn, result_db_key=result_db_key, is_append_result=is_append_result, tests=tests, is_confirm=is_confirm, error_message=error_message, error_options=error_options, text_populator=text_populator, ) else: if options is not None: message.options = options if args is not None: message.args = args self._message = message transitions = lists.make_sure_is_iterable(transitions) if not self._is_transitions_valid(transitions, transition_fn): raise ValueError("Transitions should agree with message options") self._transitions = transitions if transition_fn is not None and not callable(transition_fn): raise ValueError('Transition function must be callable') self._transition_fn = transition_fn
def fit(self, obs): obs = lists.make_sure_is_iterable(obs) if all(obs): obs = [False] + obs elif not any(obs): obs = [True] + obs obs = Bkt._bool_list_to_hmmlearn_number_array(obs) model = self._get_model() model.fit(obs) pL_ = model.startprob_[0] pT_ = model.transmat_[1][0] pS_ = model.transmat_[0][1] pG_ = model.transmat_[1][0] return Bkt(pL_, pT_, pS_, pG_)
def __init__(self, name, nodes, start_node, exit_code='exit'): super().__init__(name) self._nodes_dict = {} nodes = lists.make_sure_is_iterable(nodes) for node in nodes: self._add_node(node) try: self.check_node_transitions(exit_code, nodes) except Exception as e: raise e from e if start_node not in self._nodes_dict: raise KeyError self._start_node_name = start_node self._exit_code = exit_code self._current_node_name = None self._is_active = None self.reset()
def set_input(self, values): self._response = lists.make_sure_is_iterable(values)
def _bool_list_to_hmmlearn_number_array(bools, value_if_true=1, value_if_false=0): bools = lists.make_sure_is_iterable(bools) return [[value_if_true] if b else [value_if_false] for b in bools]
def _hmmlearn_number_array_to_bool_list(nums, value_for_true=1): nums = lists.make_sure_is_iterable(nums) return [n[0] == value_for_true for n in nums]
def _list_of_names(plans): plans = lists.make_sure_is_iterable(plans) for p in plans: if not issubclass(p.__class__, BaseMessenger): raise TypeError return [p.name for p in plans]
def make_dict_with_foo_elements(keys): keys = lists.make_sure_is_iterable(keys) if len(keys) is not len(set(keys)): raise ValueError("All elements in keys must be unique") return dict(zip(keys, range(len(keys))))
def _is_valid_plan(self, plan): plan = lists.make_sure_is_iterable(plan) for p in plan: if p not in self._possible_items: return False return True
def _print_enumerated_list(options): options = lists.make_sure_is_iterable(options) for i in range(len(options)): print(f" {i}. " + options[i])
def new_plan(self, plan, pre_hooks=None, post_hook=None): plan = lists.make_sure_is_iterable(plan) if not self._is_valid_plan(plan): raise ValueError("Invalid plan") self._plan = [] self.insert(plan, pre_hooks, post_hook)