def create_unique(subj, type, obj): """creates relation with given subj_id, type, and obj_id, if not already present""" query = Relation.gql("WHERE subj_id = :1 AND type = :2 and obj_id = :3", subj, type, obj) if query.count() == 0: rpc = urlfetch.create_rpc() urlfetch.make_fetch_call(rpc, cmdurl + '/relation/?%s/%s/%s' % (type, obj, subj), method="PUT", headers={'sharedpass': sharedpass}) record = Relation(subj_id = subj, type = type, obj_id = obj) record.put()
def create_unique(subj, type, obj): """creates relation with given subj_id, type, and obj_id, if not already present""" query = Relation.gql("WHERE subj_id = :1 AND type = :2 and obj_id = :3", subj, type, obj) if query.count() == 0: rpc = urlfetch.create_rpc() urlfetch.make_fetch_call(rpc, cmdurl + '/relation/?%s/%s/%s' % (type, obj, subj), method="PUT", headers={'sharedpass': sharedpass}) record = Relation(subj_id=subj, type=type, obj_id=obj) record.put()
def delete(subj, type, obj): """deletes any and all entities with given subj_id, type, and obj_id""" query = Relation.gql("WHERE subj_id = :1 AND type = :2 and obj_id = :3", subj, type, obj) for record in query: rpc = urlfetch.create_rpc() urlfetch.make_fetch_call(rpc, cmdurl + '/relation/all?%s/%s/%s' % (type, obj, subj), method="DELETE", headers={'sharedpass': sharedpass}) record.delete()
def del_by_obj_type(obj, type): """deletes any and all entities with given obj_id and type""" query = Relation.gql("WHERE obj_id = :1 AND type = :2", obj, type) for record in query: rpc = urlfetch.create_rpc() urlfetch.make_fetch_call(rpc, cmdurl + "/relation/type?%s/%s/null" % (type, obj), method="DELETE", headers={'sharedpass': sharedpass}) record.delete()
def del_by_obj(obj): """deletes any and all entities with given obj_id""" query = Relation.gql("WHERE obj_id = :1", obj) for record in query: rpc = urlfetch.create_rpc() urlfetch.make_fetch_call(rpc, cmdurl + "/relation/obj?null/%s/null" % (obj), method="DELETE", headers={'sharedpass': sharedpass}) record.delete()
def find_history(request): user = request.__user__ # 读取年份、月份 year = int(request.GET['year']) month = int(request.GET['month']) # 计算每日是周几 firstDayWDay, monthDays = calendar.monthrange(year, month) everyDayData = {} for day in range(monthDays): wday = (firstDayWDay + day) % 7 everyDayData[day] = {} everyDayData[day]['wday'] = wday2weekday(wday) # 调取打卡记录 everyDayData[day]['status'] = yield from check_status( year, month, day + 1, user.name) # 调取关注的好友 friends = yield from Relation.findAll('active_user_name=?', [user.name]) everyDayData[day]['friends'] = {} for friend in friends: friend.status = yield from check_status(year, month, day + 1, friend.passive_user_name) everyDayData[day]['friends'][ '%s' % friend.passive_user_name] = friend.status # 返回 return { '__template__': 'history.html', 'username': user.name, 'year': year, 'month': month, 'monthDays': monthDays, 'everyDayData': everyDayData }
def add_page_pair_to_database(from_page, to_page, limit): with db_lock: cou = session.query(Page.id).filter(Page.url == from_page).scalar() cou1 = session.query(Page.id).filter(Page.url == to_page).scalar() if cou is None: new_page_from = Page(url=from_page, text="", rank=0) session.add(new_page_from) session.flush() id0 = new_page_from.id else: id0 = cou if cou1 is None: allowed = limit < 1 or limit > session.query(Page).count() if not allowed: return new_page_to = Page(url=to_page, text="", rank=0) session.add(new_page_to) session.flush() id1 = new_page_to.id else: id1 = cou1 new_relation = Relation(page_id=id0, destination_id=id1) # print(new_relation.page_id.id) session.add(new_relation) session.commit()
def rm_following(request): user = request.__user__ following_name = request.GET['following_name'] relations = yield from Relation.findAll('active_user_name=?', [user.name]) for relation in relations: if relation.passive_user_name == following_name: yield from relation.remove() return web.HTTPFound('/group')
def group(request): user = request.__user__ name = user.name following = yield from Relation.findAll('active_user_name=?', [name]) noFollowing = False if type(following) == type(None): noFollowing = True follower = yield from Relation.findAll('passive_user_name=?', [name]) noFollower = False if type(follower) == type(None): noFollower = True return { '__template__': 'group.html', 'username': name, 'following': following, 'follower': follower, 'noFollowing': noFollowing, 'noFollower': noFollower }
def provide(self, relation: Relation, column: Column) -> dict: relation.count = self._resolve_count(relation.name) if column.required is False: column.nullability = self._resolve_nullability( relation.name, column) raw_config = self._config(relation.name, column) if column.column_type == 'varchar': return self._parse_varchar_config(raw_config) if column.column_type in ['date', 'timestamp']: return self._parse_date_config(raw_config) return raw_config
def add_following(request): user = request.__user__ add_following = request.POST['add'] new_following = yield from User.findAll('name=?', [add_following]) result = None if len(new_following) == 1: following_user = new_following[0] relations = yield from Relation.findAll('active_user_name=?', [user.name]) if len(relations) >= 1: for relation in relations: if relation.passive_user_name == following_user.name: result = 'already following' if result != 'already following': r1 = Relation(active_user_id=user.id, active_user_name=user.name, passive_user_id=following_user.id, passive_user_name=following_user.name) yield from r1.save() else: r1 = Relation(active_user_id=user.id, active_user_name=user.name, passive_user_id=following_user.id, passive_user_name=following_user.name) yield from r1.save() else: result = 'User Not Found' # 重新载入此页 following = yield from Relation.findAll('active_user_name=?', [user.name]) noFollowing = False if type(following) == type(None): noFollowing = True follower = yield from Relation.findAll('passive_user_name=?', [user.name]) noFollower = False if type(follower) == type(None): noFollower = True return { '__template__': 'group.html', 'username': user.name, 'following': following, 'follower': follower, 'noFollowing': noFollowing, 'noFollower': noFollower, 'result': result }
def create_users(loop): yield from orm.create_pool(loop=loop, user='******', password='******', db='check_in_system') user_dict = { 'u1': { 'name': 'tonyyeti', 'passwd': 'Tonyalston911', 'admin': True}, 'u2': { 'name': 'feifei', 'passwd': '123456', 'admin': False}, 'u3': { 'name': 'maomao', 'passwd': '123456', 'admin': False} } for user in user_dict.values(): uid = next_id() sha1_passwd = '%s:%s' % (uid, user['passwd']) u = User(id=uid, name=user['name'], passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='about:blank', admin=user['admin']) yield from u.save() print(user['name'] + 'saved.') feifei = yield from User.findAll('name=?', ['feifei']) maomao = yield from User.findAll('name=?', ['maomao']) r1 = Relation(active_user_id=feifei[0].id, active_user_name=feifei[0].name, passive_user_id=maomao[0].id, passive_user_name=maomao[0].name) r2 = Relation(active_user_id=maomao[0].id, active_user_name=maomao[0].name, passive_user_id=feifei[0].id, passive_user_name=feifei[0].name) yield from r1.save() print('r1 saved.') yield from r2.save() print('r2 saved')
def index(request): if request.__user__ is None: return web.HTTPFound('/signin') user = request.__user__ name = user.name todayTime = time.localtime() year = todayTime.tm_year month = todayTime.tm_mon day = todayTime.tm_mday weekday = wday2weekday(todayTime.tm_wday) r_dict = { '__template__': 'index.html', 'username': name, 'year': year, 'month': month, 'day': day, 'weekday': weekday } friends = yield from Relation.findAll('active_user_name=?', [name]) for friend in friends: status = yield from check_status(year, month, day, friend.passive_user_name) journal = yield from find_journal_data(year, month, day, friend.passive_user_name) content = None if not type(journal) == type(None): content = journal.content friend.status = status friend.journal = content r_dict['friends'] = friends status = yield from check_status(year, month, day, name) journal = yield from find_journal_data(year, month, day, name) content = None if not type(journal) == type(None): content = journal.content r_dict['status'] = status r_dict['journal'] = content return r_dict
def find_history_journal(request): # 查询日的信息 user = request.__user__ name = user.name year = int(request.GET['year']) month = int(request.GET['month']) day = int(request.GET['day']) weekday = wday2weekday(dt.date(year, month, day).weekday()) r_dict = { '__template__': 'history_journal.html', 'username': user.name, 'year': year, 'month': month, 'day': day, 'weekday': weekday, } # 读取朋友列表、朋友的打卡记录与日志 friends = yield from Relation.findAll('active_user_name=?', [name]) for friend in friends: status = yield from check_status(year, month, day, friend.passive_user_name) journal = yield from find_journal_data(year, month, day, friend.passive_user_name) content = None if not type(journal) == type(None): content = journal.content friend.status = status friend.journal = content r_dict['friends'] = friends # 你至己的打卡信息与日志 status = yield from check_status(year, month, day, name) journal = yield from find_journal_data(year, month, day, name) content = None if not type(journal) == type(None): content = journal.content r_dict['status'] = status r_dict['journal'] = content return r_dict
def parse(self, filepath: str) -> List[Relation]: relations = [] definition_start_regex = re.compile("create table ([a-z0-9_]+)", re.IGNORECASE) definition_end_regex = re.compile("\);") stage = 0 STAGE_SEARCHING = 0 STAGE_READING = 1 attribute_lines = [] with open(filepath, "r") as file: line = file.readline() while line: if stage == STAGE_SEARCHING: match = definition_start_regex.match(line) if match is not None: r = Relation(match.group(1)) relations.append(r) stage = STAGE_READING file.readline() self.relations_index[r.name] = r else: if definition_end_regex.search(line) is not None: ddl_lines = re.split( ',\n', ''.join(attribute_lines).replace(', ', ',')) attribute_lines = [] columns_list = list( filter(lambda x: x is not None, map(lambda x: Column.parse(x), ddl_lines))) self._resolve_foreign_keys(columns_list) columns_dict = {c.name: c for c in columns_list} relations[-1].columns = columns_dict stage = STAGE_SEARCHING continue attribute_lines.append(line) line = file.readline() return relations
def relation_dal(self): return Relation() def get(self, user_id):
def relation(self): return Relation() @property
def getby_subj_type(subj, type): """returns all relation entities with given subj, type""" return Relation.gql("WHERE subj_id = :1 AND type = :2", subj, type)
def relation_dal(self): return Relation()
def getby_subj_obj_type(subj, obj, type): return Relation.gql("WHERE subj_id = :1 AND type = :2 and obj_id = :3", subj, type, obj)
def getby_subj_obj(subj, obj): """returns all relation entities with given subj_id and obj_id""" return Relation.gql("WHERE subj_id = :1 AND obj_id = :2", subj, obj)
def getby_obj_type(obj, type): """returns all relation entities with given obj, type""" return Relation.gql("WHERE obj_id = :1 AND type = :2", obj, type)
r = 0.9 # pin radius Np = 10 # number of pin rows and columns in the assembly points_on_walls = False # True # assembly bundle a = Bundle.box(ll=(0,0), ur=(pp*(Np-1), pp*(Np-1)), r=1.3*r) a.wt =1. a.color = 'blue' # channel ch = Bundle.box(ll=(0, 0), ur=(2*pp, 3*pp), r=0.8*r) ch.color = 'green' ch.wt = 0.2 rc = Relation() rc.x = 5*pp rc.y = 3*pp a.interior.append((ch, rc)) # fuel pin master model p = Rod() p.radius = r p.color = 'red' # insert fuel pins into the assembly chsh = a.shapely([0]) if points_on_walls: Imin = 0 Imax = 0 else:
# dimensions: pp = 2. # pin pitch rr = 0.9 # pin radius Np = 3 # number of pin rows and columns in the assembly # assembly bundle a = Bundle.box(ll=(0, 0), ur=(pp * (Np - 1), pp * (Np - 1)), r=1.3 * rr) a.wt = 1. a.color = 'blue' # channel ch = Bundle.box(ll=(1, 1), ur=(3.1, 3.0), r=0.1) ch.color = 'green' ch.wt = 0.2 rc = Relation() rc.x = 0 rc.y = 0 a.interior.append((ch, rc)) # insert fuel pins into the assembly Imin = -1 Imax = 1 for i in range(Imin, Np + Imax): x = pp * i for j in range(Imin, Np + Imax): if (i, j) != (-10, -10): y = pp * j r = Relation() r.x = x + uniform(-0.1 * rr, 0.1 * rr) r.y = y + uniform(-0.1 * rr, 0.1 * rr)
def relation(self): return Relation()
def run(): ali = alignmenter() checker = checker_conflict() file_handler = single_file_handler() root = r'A:\研二2\Bi-LSTM+CRF\brat\relabled' result = {} #冲突事件集合 contradictory = {} #冲突事件被预测为冲突 contradictory['p-Contradictory'] = 0 #蕴含 contradictory['p-Entailment'] = 0 #没有检测出来 contradictory['p-non'] = 0 #放入result集合 result['Contradictory'] = contradictory entailment = {} entailment['p-Contradictory'] = 0 entailment['p-Entailment'] = 0 entailment['p-non'] = 0 result['Entailment'] = entailment #标记数据中没有的事件被预测为冲突或者蕴含 non = {} non['p-Contradictory'] = 0 non['p-Entailment'] = 0 result['non'] = non for dir_name in os.listdir(root): dir = os.path.join(root,dir_name) if(not os.path.isdir(dir)): continue for index_name in os.listdir(dir): index_dir = os.path.join(dir,index_name) if(not os.path.isdir(index_dir)): continue for file in os.listdir(index_dir): '''遍历每个文件获取events''' if(file.find('.ann')==-1): continue origin_file = os.path.join(index_dir,file.replace('ann','txt')) if(not os.path.exists(origin_file)): continue event_map,event_relations = file_handler.handler(os.path.join(index_dir,file),origin_file) events = list(event_map.values()) my_event_relations = [] for i in range(len(events)): for j in range(i+1,len(events)): if (ali.alignment(events[i], events[j])): if (checker.check(events[i], events[j])): my_event_relations.append(Relation(None,'Contradictory',events[i],events[j])) else: my_event_relations.append(Relation(None,'Entailment',events[i],events[j])) cal(result,event_relations,my_event_relations) save_root = r'A:\研二2\Bi-LSTM+CRF\bert\data' with open(os.path.join(save_root, 'event_relations.txt'), 'w', encoding='utf8') as writer: writer.write('\t\tp-Contradictory\tp-Entailment\tp-Non\n') for (key,val) in result.items(): writer.write(key+'\t'+str(val['p-Contradictory'])+'\t'+str(val['p-Entailment'])+'\t') if(key!='non'): writer.write('\t'+str(val['p-non'])+'\n') else: writer.write('\n')