def append(self, item): if self: s = sorted(self, key=lambda obj: obj.score) item.score = s[-1].score + 1 else: item.score = 1 InstrumentedList.append(self, item)
def record_observation( observations: InstrumentedList, observation_cls: Type[T], **values: Any ) -> T: for observation in observations: for name, value in values.items(): if value is None: continue observed_value = getattr(observation, name) if observed_value is not None: if isinstance(value, list) and value: diffable_value = \ get_diffable_observation_relationship_values(value) diffable_observed_value = \ get_diffable_observation_relationship_values(observed_value) if diffable_value != diffable_observed_value: break elif observed_value != value: break else: for name, value in values.items(): if value is None: continue setattr(observation, name, value) return observation new_observation = observation_cls(**values) observations.append(new_observation) return new_observation
def role(self, src=None, target=None): return_list = InstrumentedList([]) for triple in self.list_triples: is_instance = triple.relation == 'instance' if not is_instance and ( (src is not None and triple.source == src) or (target is not None and triple.target == target)): return_list.append(triple) return return_list
def triples(triples_list, src=None, relation=None, target=None): try: if src is None and relation is None and target is None: return triples_list else: return_list = InstrumentedList([]) for triple in triples_list: if _match_triple(src, relation, target, triple): return_list.append(triple) return return_list except Exception as exc: PrintException()
def organize_triples_list(triples_list, top): try: if isinstance(top, Triple): new_list = InstrumentedList([]) new_list.append(top) for triple in triples_list: if triple != top: if triple.relation == 'instance': new_list.insert(1, triple) else: new_list.append(triple) return new_list except Exception as exc: PrintException()
def set(self, item, index=0): if self: s = sorted(self, key=lambda obj: obj.score) if index >= len(s): item.score = s[-1].score + 1 elif index < 0: item.score = s[0].score index = 0 else: item.score = s[index].score + 1 for i, it in enumerate(s[index:]): it.score = item.score + i + 1 # if s[i+1].score more then break else: item.score = index InstrumentedList.append(self, item)
def is_name(self, string): """ :type string: str :param string: :return: """ splitted_string = string.split() possible_names = self.get_triples(relation='instance', target='name') return_list = InstrumentedList([]) for possible_name in possible_names: possible_name_string_list = self.get_triples( src=possible_name.source, relation='op') if possible_name_string_list and any( any(ss in pns.target for ss in splitted_string) for pns in possible_name_string_list): return_list.append(possible_name) return False if not return_list else return_list[0]
def span(self, top): """ :type top: Triple :param top: :return: """ # base _ways = self.role(src=top.source) new_way = InstrumentedList([]) for way in _ways: if self.is_instance(way.target): way_instance = self.get_instance(src=way.target) new_way += InstrumentedList([way ]) + self.span(top=way_instance) else: new_way.append(way) return InstrumentedList([top] + new_way)
def add(rel: InstrumentedList, table: Base, value: dict): try: item = table.query.filter_by(**value).one() except NoResultFound: item = table(**value) rel.append(item)