def ai_evaluate_actions(self): for m in self.available_actions: take_action = False #TODO: merge this if/else if len(m.ai_action) == 1: #AND or just 1 condition for condition in m.ai_action: for key, val in condition.items(): attr = utils.rgetattr(self, key) check = utils.eval_condition(val, attr) if not check: take_action = False break else: take_action = True else: for condition in m.ai_action: #OR for key, val in condition.items(): attr = utils.rgetattr(self, key) check = utils.eval_condition(val, attr) if not check: take_action = False else: take_action = True break if take_action: action = m() self.new_measures.append(action)
def test_get_doesnt_exist(self): class Foo(object): pass foo = Foo() with self.assertRaises(AttributeError): rgetattr(foo, "bar_var")
def test_get_on_same(self): class Foo(object): def __init__(self): self.foo_var = 11 foo = Foo() rgetattr(foo, "foo_var") got = foo.foo_var self.assertEquals(got, 11)
def get_python_call(module, attrs): json_args = request.json or {} kwargs = {k: v for k, v in json_args.items() if k != '__subattrs'} subattrs = json_args.get('__subattrs') mod = import_module(module) result = rgetattr(mod, attrs)(**kwargs) if subattrs: for item in subattrs: kwd = "".join(i for i in item.keys()) result = rgetattr(result, kwd)(item[kwd]) return jsonify(result)
def test_get_on_related(self): class Foo(object): def __init__(self): self.foo_var = 11 class Bar(object): def __init__(self): self.bar_var = Foo() bar = Bar() rgetattr(bar, "bar_var__foo_var") got = bar.bar_var.foo_var self.assertEquals(got, 11)
def update_monitors(self): """Loops through the monitor keys and appends current value of any object's attribute found.""" for key in self.mons: try: self.mons[key].append(rgetattr(self, key)) except AttributeError: pass
def post_data(self): """Posts the current values of the data to the server""" LOGGER.debug('Data streamer %r sending data.', self) for sensor_id, attr in self.id_to_attribute.items(): value = rgetattr(self.instance, attr) LOGGER.debug("Sending new value for %s: %s.", attr, value) self.client.update_sensor_value( self.recipe_instance, value, sensor_id)
def get_radii_and_norms(self): """Calculates the spectral radii and/or norms of any monitor keys where this is specified.""" for feature, func in zip(['radius', 'norm'], [get_spectral_radius, norm]): for key in self.mons: if feature in key: attr = key.split('-')[0] self.mons[key].append(func(rgetattr(self, attr)))
def bag_to_dataframe(bag_path, topic, fields, aggregation=None): data = {} timestamps = [] for field in fields: data[field] = [] if type(bag_path) is not tuple: bag_path = (bag_path, ) for single_bag_path in bag_path: single_bag = rosbag.Bag(single_bag_path) for _, message, timestamp in single_bag.read_messages(topics=topic): for field in fields: if aggregation is not None: agg_res = aggregation(utils.rgetattr(message, field)) data[field].append(agg_res) else: data[field].append(utils.rgetattr(message, field)) timestamps.append(timestamp.to_sec()) df = pd.concat([ pd.Series(data[field], index=timestamps, name=field) for field in fields ], axis=1) return df