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 get_subgraph(self, top): """ :type top: Triple :param top: :return: """ try: _span = self.span(top=top) possible_children = InstrumentedList(set(_span)) not_instances = [] instances = [] for children in possible_children: if children.relation != 'instance': not_instances.append(children) else: instances.append(children) for not_instance in not_instances: if all(instance.source != not_instance.source and instance. source != not_instance.target and not_instance != top for instance in instances): possible_children.remove(not_instance) possible_children = organize_triples_list( triples_list=possible_children, top=top) new_subgraph = AMRModel( top=top.source, triples=copy_triples_from_list( possible_children)) if possible_children else None return new_subgraph except Exception as exc: PrintException()
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 get_coreferences(self, term=None): if term is None: return self.coreferences else: if isinstance(term, str): return InstrumentedList(coref for coref in self.coreferences if coref.has_term(term)) elif isinstance(term, list or InstrumentedList): if isinstance(term[0], str): return InstrumentedList(coref for coref in self.coreferences if any(coref.has_term(t) for t in term)) elif isinstance(term[0], list or InstrumentedList): return InstrumentedList(coref for coref in self.coreferences if any(all(coref.has_term(compound) for compound in t) for t in term))
def update_session(form, session): """ Session will be updated in database :param form: view data form :param session: object contains all earlier data """ data = form.data speakers = data["speakers"] microlocation = data["microlocation"] level = data["level"] format = data["format"] language = data["language"] del data["speakers"] del data["microlocation"] del data["level"] del data["format"] del data["language"] db.session.query(Session) \ .filter_by(id=session.id) \ .update(dict(data)) session.speakers = InstrumentedList(speakers if speakers else []) session.microlocation = microlocation session.format = format session.level = level session.language = language save_to_db(session, "Session updated") update_version(session.event_id, False, "session_ver")
def remove(self, field, **kwargs): """ Remove values from a relationship field. This replaces the value with None or an empty list. Pass in the field and unique argument to isolate object for removal. """ current_values = self.get_field(field) if isinstance(current_values, dict): return current_values elif isinstance(current_values, InstrumentedList): if kwargs: key = [i for i in kwargs][0] try: item_index = current_values.index([ i for i in current_values if getattr(i, key) == kwargs[key] ][0]) current_values.pop(item_index) except Exception as e: return { "message": "Ensure the arguments passed are valid.", "exception": str(e) } else: setattr(self, field, InstrumentedList([])) else: setattr(self, field, None) self.save()
def __activities__(self): Activity = Activate.get_activity() query_filter = self.join_self_activities() return InstrumentedList(Activity.query.filter(query_filter) \ .order_by(Activity.dateCreated) \ .order_by(Activity.id) \ .all())
def _common_names(amr1, amr2): """ :type amr1: AMRModel :type amr2: AMRModel :param amr1: :param amr2: :return: """ try: return_list = set() names1 = amr1.get_triples(relation='instance', target='name') names2 = amr2.get_triples(relation='instance', target='name') names_string_1 = zip(names1, [amr1.get_names(name.source) for name in names1]) names_string_2 = zip(names2, [amr2.get_names(name.source) for name in names2]) for name1, string1 in names_string_1: for name2, string2 in names_string_2: if string1 == string2 or string1 in string2 or string2 in string1: return_list.add( (name1, name2, min([len(string1), len(string2)]))) return InstrumentedList(return_list) except Exception as exc: PrintException()
def verdicts(self): Verdict = get_model_with_table_name('verdict') VerdictUser = get_model_with_table_name('verdict_user') verdict_users = VerdictUser.query.filter_by(userId=self.userId) verdict_ids = [verdict_user.verdict.id for verdict_user in verdict_users] verdicts = Verdict.query.filter(Verdict.id.in_(verdict_ids)).all() return InstrumentedList(verdicts)
def test_to_dict(self, memory_db): class MyModel(docs.BaseDocument): __tablename__ = 'mymodel' _nested_relationships = ['other_obj3'] id = fields.IdField(primary_key=True) other_obj = fields.StringField() other_obj2 = fields.StringField() other_obj3 = fields.StringField() memory_db() myobj1 = MyModel(id=1) myobj1.other_obj = MyModel(id=2) myobj1.other_obj2 = InstrumentedList([MyModel(id=3)]) myobj1.other_obj3 = MyModel(id=4) result = myobj1.to_dict() assert list(sorted(result.keys())) == [ '_pk', '_type', 'id', 'other_obj', 'other_obj2', 'other_obj3' ] assert result['_type'] == 'MyModel' assert result['id'] == 1 assert result['_pk'] == '1' # Not nester one-to-one assert result['other_obj'] == 2 # Not nester many-to-one assert result['other_obj2'] == [3] # Nested one-to-one assert isinstance(result['other_obj3'], dict) assert result['other_obj3']['_type'] == 'MyModel' assert result['other_obj3']['id'] == 4
def intersection(self, amr, focus=None): """ :param focus: :type amr: AMRModel :param amr: :return: """ return_list = set() if focus is None: _other_triples = amr.get_triples() for triple in _other_triples: if triple in self.list_triples: return_list.add(triple) else: focus_is_name = amr.is_name(focus) if focus_is_name and self.is_name(focus): return_list.add(focus_is_name) else: _other_triples = amr.get_triples(relation='instance', target=focus) for triple in _other_triples: if triple in self.list_triples: return_list.add(triple) return _filter_common(InstrumentedList(return_list), self.has_common_person(amr), self.has_common_names(amr))
def pop(self, item): # FIXME if self: obj_list = sorted(self, key=lambda obj: obj.score) for i, it in enumerate(obj_list): if obj_list[i] == item: return InstrumentedList.pop(self, i)
def _filter_common(triple_list, person_list, name_list): """ :param triple_list: :param person_list: :param name_list: :return: """ filtered_names = set() names = triples(triples_list=triple_list, relation='instance', target='name') for name in names: for n_tuple in name_list: if name.source == n_tuple[0] or name.source == n_tuple[1]: filtered_names.add(name) filtered_persons = set() persons = triples(triples_list=triple_list, relation='instance', target='person') for person in persons: for p_tuple in person_list: if person.source == p_tuple[0] or person.source == p_tuple[1]: filtered_persons.add(person) return InstrumentedList( set(triple_list) - (set(names) - filtered_names) - set(set(persons) - filtered_persons))
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 _delete_relation(triple_list, relation_triple): """ :type relation_triple: Triple """ return InstrumentedList( set(triple_list) - set(relation_triple) - set(_delete_nodes(triple_list, relation_triple.target)))
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 reviews(self): Review = get_model_with_table_name('review') verdict_user_ids = [ verdictUser.user.id for verdictUser in self.verdictUsers ] reviews = Review.query.filter( (Review.articleId == self.articleId) &\ (Review.userId.in_(verdict_user_ids)) ).all() return InstrumentedList(reviews)
def verdicts(self): Verdict = ApiHandler.model_from_table_name('verdict') VerdictReviewer = ApiHandler.model_from_table_name('verdict_reviewer') verdict_reviewers = VerdictReviewer.query.filter_by( reviewerId=self.reviewerId) verdict_ids = [ verdict_reviewer.verdict.id for verdict_reviewer in verdict_reviewers ] verdicts = Verdict.query.filter(Verdict.id.in_(verdict_ids)).all() return InstrumentedList(verdicts)
def __init__(self, **kwargs): """ :param kwargs: - string - triples - top - object -copy """ if kwargs: if len(kwargs) == 3 and ('string' and 'triples' and 'top') in kwargs: self.penman = kwargs['string'] self.list_triples = kwargs['graph'] self.top = kwargs['top'] elif len(kwargs) == 2 and ('triples' and 'top') in kwargs: self.top = kwargs['top'] self.list_triples = kwargs['triples'] self.penman = triple_model_list_to_penman(self.list_triples, top_id=self.top) self.list_triples = self.delete_wiki() elif len(kwargs) == 1 and 'object' in kwargs: self.penman = pm.encode(kwargs['object'], top=kwargs['object'].top) self.list_triples, self.top = penman_to_model(kwargs['object']) self.list_triples = self.delete_wiki() elif len(kwargs) == 1 and 'copy' in kwargs: _source_amr: AMRModel = kwargs['copy'] self.penman = _source_amr.get_penman(return_type='str') self.list_triples = InstrumentedList( [Triple(copy=t) for t in _source_amr.get_triples()]) self.list_triples = self.delete_wiki() self.top = _source_amr.get_top() self.penman = triple_model_list_to_penman(self.list_triples, top_id=self.top)
def reviews(self): Review = ApiHandler.model_from_table_name('review') verdict_reviewer_ids = [ verdictReviewer.reviewer.id for verdictReviewer in self.verdictReviewers ] reviews = Review.query.filter( (Review.contentId == self.contentId) &\ (Review.reviewerId.in_(verdict_reviewer_ids)) ).all() return InstrumentedList(reviews)
def get_attributes(item): fields = {} for field in [fld for fld in dir(item) if not fld.startswith('_') and fld != 'metadata' and fld != 'query' and fld != 'query_class' and 'index_' not in fld]: field_val = item.__getattribute__(field) if isinstance(field_val, InstrumentedList): sub_items = [] for it in InstrumentedList(field_val): sub_items.append(get_attributes(it)) fields[field] = sub_items else: fields[field] = item.__getattribute__(field) return fields
def __init__(self, **kwargs): if kwargs: if len(kwargs) == 2 and ('text' and 'label') in kwargs: self.content = kwargs['text'] self.label = kwargs['label'] elif len(kwargs) == 1 and 'copy' in kwargs: kwargs['copy']: Sentence self.content = kwargs['copy'].content self.label = kwargs['copy'].label self.tokenized = InstrumentedList([ create_token(dictionary={'word': token.word, 'lemma': token.lemma, 'ner': token.ner, 'pos': token.pos}) for token in kwargs['copy'].tokenized ])
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 path(self, src, target=None, only_roles=True): """ DFS based :param src: :param target: :param only_roles: :return: """ if src == target: return [] _ways = self.role(src=src) for way in _ways: if way.target == target: return InstrumentedList( [way] if only_roles else [self.get_instance(src=src)] + [way] + [self.get_instance(src=target)]) else: new_way = self.path(src=way.target, target=target, only_roles=only_roles) if new_way and new_way is not None: return InstrumentedList([way]) + new_way return []
def update_speaker(form, speaker): """ Speaker will be updated in database :param form: view data form :param speaker: object contains all earlier data """ data = form.data del data['sessions'] db.session.query(Speaker) \ .filter_by(id=speaker.id) \ .update(dict(data)) speaker.sessions = InstrumentedList( form.sessions.data if form.sessions.data else []) save_to_db(speaker, "Speaker updated") update_version(speaker.event_id, False, "speakers_ver")
def fix_payload_post(self, event_id, data): """ Fixes payload of POST request """ data['track'] = self.get_object(TrackModel, data.get('track_id'), event_id) data['microlocation'] = self.get_object(MicrolocationModel, data.get('microlocation_id'), event_id) data['session_type'] = self.get_object(SessionTypeModel, data.get('session_type_id'), event_id) data['event_id'] = event_id data['speakers'] = InstrumentedList( SpeakerModel.query.get(_) for _ in data['speaker_ids'] if self.get_object(SpeakerModel, _, event_id) is not None) data = self._delete_fields(data) return data
def remove(self, field, **kwargs): """ Remove values from a relationship field. This replaces the value with None or an empty list. Pass in the field and unique argument to isolate object for removal. """ try: current_values = getattr(self, field) except AttributeError: return { "message": "Ensure the field passed is valid.", "help": "The field should be an attribute of the object." } if isinstance(current_values, InstrumentedList): if kwargs: key = [i for i in kwargs][0] try: item_index = current_values.index([ i for i in current_values if getattr(i, key) == kwargs[key] ][0]) current_values.pop(item_index) except Exception as e: return { "message": "Ensure the arguments passed are valid.", "help": "Should be of an existent object and unique.", "exception": e } else: setattr(self, field, InstrumentedList([])) else: setattr(self, field, None) try: db.session.add(self) db.session.commit() return True except Exception as e: db.session.rollback() return { "message": "Ensure the object saved after deletion is valid", "help": "Has all fields and doesn't repeat unique values.", "exception": e }
def create_session(form, event_id, is_accepted=True): """ Session will be saved to database with proper Event id :param form: view data form :param slide_file: :param event_id: Session belongs to Event by event id """ new_session = Session(title=form.title.data, subtitle=form.subtitle.data, long_abstract=form.long_abstract.data, start_time=form.start_time.data, end_time=form.end_time.data, event_id=event_id, short_abstract=form.short_abstract.data) new_session.speakers = InstrumentedList( form.speakers.data if form.speakers.data else []) new_session.microlocation = form.microlocation.data new_session.track = form.track.data save_to_db(new_session, "Session saved") update_version(event_id, False, "session_ver")
def _mc_appender(self, obj, **kwargs): func = BaseInstrumentedList.__getattribute__(self, '_sa_appender') func(self.__list_type_class__(obj.value), **kwargs)
def _mc_append(self, value): func = BaseInstrumentedList.__getattribute__(self, 'append') func(self.__list_type_class__(value))
def __getattribute__(self, name): if name == '_sa_appender': return self._mc_appender if name == 'append': return self._mc_append return BaseInstrumentedList.__getattribute__(self, name)
def __contains__(self, obj): for rec in self: if hasattr(rec, self.__attr_to_get__): if getattr(rec, self.__attr_to_get__) == obj: return True return InstrumentedList.__contains__(self, obj)