def get_person_quotes(self, id): session = Session() quotes = (session.query(Quote).filter(Quote.person_id == id).all()) source = (session.query(Source).filter(Source.person_id == id).all()) person = (session.query(Person).filter(Person.id == id).one()) Session.remove() return quotes, source, [person]
def upload(user_id, package_name, version, time, remark): try: session_obj = Session() tmp = session_obj.query(user_info).filter_by(id=user_id).first() if (tmp == None): Session.remove() return -1 tmp = session_obj.query(package_info).options( subqueryload(package_info.user)).filter_by( package_name=package_name).filter_by( user_id=user_id).first() if tmp is not None: tmp = session_obj.query(package_info).get(tmp.id) session_obj.delete(tmp) session_obj.commit() package_obj = package_info(user_id=user_id, package_name=package_name, version=version, time=time, remarks=remark) session_obj.add(package_obj) session_obj.commit() Session.remove() return 0 except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2
def get_random(self, randtype): session = Session() types = {'person': Person, 'source': Source, 'quote': Quote} id = (session.query(types[randtype]).order_by( func.random()).limit(1).one().id) Session.remove() return id
def get_keyword_quotes(self, keywords, n=25): # function to convert incompatible nltk POS tags def get_pos(treebank_tag): if treebank_tag.startswith('J'): return 'a' elif treebank_tag.startswith('V'): return 'v' elif treebank_tag.startswith('N'): return 'n' elif treebank_tag.startswith('R'): return 'r' else: return '' keywords = nltk.tokenize.word_tokenize(keywords) keywords = nltk.pos_tag(keywords) keywords = [ self.lemmatize(w[0], get_pos(w[1])) if get_pos(w[1]) != '' else w[0] for w in keywords ] keywords = [k for k in keywords if k in self.model.wv.vocab] # if no words in the vocab, just return garbage... if len(keywords) == 0: keywords = [''] # duplicate short keyword strings to get in enough training base_keywords = keywords while len(keywords) < 20: keywords = keywords + base_keywords # create new docvec using model v = self.model.infer_vector(keywords, steps=300) sims = self.index[v] sims = sorted(enumerate(sims), key=lambda item: -item[1]) sims = sims[:n] ids = [i[0] + 1 for i in sims] # cache keyword's similar quotes in the db. This means when this # keyword is needed in the future (e.g., to update PCA coords), vector # and similarity isn't recomputed quotebytes = np.array(ids).tostring() keyword = Keyword(quotes=quotebytes) session = Session() session.add(keyword) session.commit() keyword_id = keyword.id # retrieve actual quotes quotes = session.query(Quote).filter(Quote.id.in_(ids)).all() quotes_dict = {q.id: q for q in quotes} quotes = [quotes_dict[i] for i in ids] source_ids = set( [q.source_id for q in quotes if q.source_id is not None]) sources = session.query(Source).filter(Source.id.in_(source_ids)).all() person_ids = set([q.person_id for q in quotes]) people = session.query(Person).filter(Person.id.in_(person_ids)).all() Session.remove() return quotes, sources, people, keyword_id
def test_remove_item_success(self): r = self.app.delete("/rooms/living-room/items/TV") self.assertEqual(r.status_code, 201) self.assertEqual(json.loads(r.data), {}) # Check room is still there but item has been deleted Session.remove() new_session = Session() self.assertEqual(len(new_session.query(Room).all()), 1) self.assertEqual(len(new_session.query(Item).all()), 0)
def users(): try: session_obj = Session() ret = session_obj.query(user_info).all() Session.remove() return (0, ret) except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return (-1, [])
def build_docvec_table(model_filename): model = Doc2Vec.load(model_filename + '.model') docvecs = model.docvecs session = Session() for i in range(len(docvecs)): qv = QuoteVec(id=i, vec=docvecs[i].tostring()) session.add(qv) session.commit() Session.remove() model.docvecs = [] model.save(model_filename + '_deletetraining.model')
def remove_by_day(to_day): try: session_obj = Session() session_obj.query(file_list).filter( file_list.time < to_day).delete() Session.remove() return 0 except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2
def is_exist_id(user_id) -> int: try: session_obj = Session() ret = session_obj.query(user_info).filter_by(id=user_id).first() Session.remove() if (ret == None): return -1 return 0 except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2
def query_packages(package_id): try: session_obj = Session() ret = session_obj.query(package_info).options( subqueryload( package_info.user)).filter_by(id=package_id).first() Session.remove() return ret except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -1
def insert(user_id,login_type,time,msg,u_uuid,remark=''): session_obj = Session() tmp = session_obj.query(user_info).filter_by(id=user_id).first() if(tmp ==None): Session.remove() return -1 package_obj = logger(user_id = user_id,login_type= login_type,time =time,msg=msg,u_uuid=u_uuid,remarks = remark ) session_obj.add(package_obj) session_obj.commit() Session.remove() return 0
def test_add_room_success(self): r = self.app.post( "/rooms", content_type="application/json", data=json.dumps({"name": "kitchen"}) ) self.assertEqual(r.status_code, 201) self.assertEqual(json.loads(r.data), {"ref": "/rooms/kitchen"}) # Check room has been added Session.remove() new_session = Session() self.assertEqual(len(new_session.query(Room).all()), 2)
def query_name_by_id(user_id) -> tuple: try: session_obj = Session() ret = session_obj.query(user_info).filter_by(id=user_id).first() Session.remove() if not ret: return (-1, '') return (0, ret.username) except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return (-2, '')
def generate(self, current_time, timestep): try: requests = Session.execute( query.format(table=self.table, t1=current_time, t2=current_time + timestep)) customers = [Customer(request) for request in requests] except: Session.rollback() raise finally: Session.remove() return customers
def get_quote_suggestions(self, query, n=10): session = Session() query_ft = query # replace problematic characters for fulltext search for char in '()+-*~@<>"': query_ft = query.replace(char, ' ') quotes = (session.query(Quote).filter( FullTextSearch( query_ft + '*', Quote, FullTextMode.BOOLEAN)).filter( Quote.quote.like('%' + query.strip() + '%')).order_by( func.length(Quote.quote)).limit(n).all()) Session.remove() return quotes
def get_person_suggestions(self, query, n=10): session = Session() query_ft = query # replace problematic characters for fulltext search for char in '()+-*~@<>"': query_ft = query_ft.replace(char, ' ') people = (session.query(Person).filter( FullTextSearch( query_ft + '*', Person, FullTextMode.BOOLEAN)).filter( Person.name.like('%' + query.strip() + '%')).order_by( func.length(Person.name)).limit(n).all()) Session.remove() return people
def get_docvecs(self, ids): session = Session() docvecs = (session.query(QuoteVec).filter(QuoteVec.id.in_(ids)).all()) # sort ids back into given order order = np.argsort(ids) docvecs_empty = [None] * len(docvecs) for i in range(len(docvecs)): docvecs_empty[order[i]] = docvecs[i] # get docvecs from byte strings docvecs = [np.fromstring(d.vec, np.float32) for d in docvecs_empty] Session.remove() return docvecs
def remove(package_id): try: session_obj = Session() ret = session_obj.query(package_info).filter_by( id=package_id).first() if not ret: Session.remove() return -1 tmp = session_obj.query(package_info).get(ret.id) folder_path = config.ROOTDIR + tmp.user.username + config.PATCHFOLDER file_path = os.path.join(folder_path, tmp.package_name) if is_file_open(file_path) or is_package_in_task(package_id): Session.remove() return -4 if os.path.exists(file_path): try: os.remove(file_path) except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() Session.remove() return -2 session_obj.delete(tmp) session_obj.commit() Session.remove() return 0 except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -3
def test_remove_room_success(self): r = self.app.delete( "/rooms", content_type="application/json", data=json.dumps({"name": "living-room"}) ) self.assertEqual(r.status_code, 201) self.assertEqual(json.loads(r.data), {}) # Check room and related items have been deleted Session.remove() new_session = Session() self.assertEqual(len(new_session.query(Room).all()), 0) self.assertEqual(len(new_session.query(Item).all()), 0)
def group_alias(login_id, group_name): try: session_obj = Session() ret = session_obj.query(user_info).filter_by(id=login_id).first() if not ret: Session.remove() return (-1, '') ret = session_obj.query(group_alias_info).filter_by( user_id=login_id).filter_by(name=group_name).first() if ret is None: alias_obj = group_alias_info(user_id=login_id, name=group_name) session_obj.add(alias_obj) session_obj.commit() Session.remove() return (0, '') if ret.alias is None: Session.remove() return (0, '') Session.remove() return (0, ret.alias) except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return (-2, '')
def get_keyword_quotes_cached(self, id): session = Session() keyword = session.query(Keyword).filter(Keyword.id == id).one() ids = np.fromstring(keyword.quotes, dtype=np.int64) ids = [int(id) for id in ids] quotes = session.query(Quote).filter(Quote.id.in_(ids)).all() quotes_dict = {q.id: q for q in quotes} quotes = [quotes_dict[i] for i in ids] source_ids = set( [q.source_id for q in quotes if q.source_id is not None]) sources = session.query(Source).filter(Source.id.in_(source_ids)).all() person_ids = set([q.person_id for q in quotes]) people = session.query(Person).filter(Person.id.in_(person_ids)).all() Session.remove() return quotes, sources, people
def packages(user_id): try: session_obj = Session() ret = session_obj.query(package_info).options( subqueryload(package_info.user)).filter( or_(package_info.user_id == user_id, package_info.user_id == 1)).order_by( desc(package_info.user_id), desc(package_info.time)).all() Session.remove() return ret except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2
def reset_permission(user_id, permission): try: session_obj = Session() tmp = session_obj.query(user_info).filter_by(id=user_id).first() if tmp == None: Session.remove() return -1 tmp.permission = permission session_obj.commit() Session.remove() except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2 return 0
def reset_pwd(user_id) -> int: try: session_obj = Session() tmp = session_obj.query(user_info).filter_by(id=user_id).first() if tmp == None: Session.remove() return -1 tmp.pwd = "e10adc3949ba59abbe56e057f20f883e" session_obj.commit() Session.remove() except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2 return 0
def temps(user_id): try: session_obj = Session() tmp = session_obj.query(user_info).filter_by(id=user_id).first() if (tmp == None): Session.remove() return -1 ret = session_obj.query(blackbox_temps).filter_by( user_id=user_id).order_by(desc(blackbox_temps.time)).all() Session.remove() return ret except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2
def session_scope_test(): s1 = Session() s2 = Session() logger.info(s1 == s2) # True logger.info(s1) # <sqlalchemy.orm.session.Session object at 0x1046a2b50> logger.info(s2) # <sqlalchemy.orm.session.Session object at 0x1046a2b50> Session.remove() # 別セッションになる。 s3 = Session() logger.info(s1 == s3) # False logger.info(s1) # <sqlalchemy.orm.session.Session object at 0x1046a2b50> logger.info(s3) # <sqlalchemy.orm.session.Session object at 0x1046d0640>
def update_pwd(user_id, pwd, new_pwd) -> int: try: session_obj = Session() tmp = session_obj.query(user_info).filter_by(id=user_id).first() if tmp == None: return -1 if tmp.pwd != pwd: return -2 tmp.pwd = new_pwd session_obj.commit() Session.remove() except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -3 return 0
def update(package_id, remark): try: session_obj = Session() tmp = session_obj.query(package_info).filter_by( id=package_id).first() if tmp == None: Session.remove() return -1 tmp.remarks = remark session_obj.commit() Session.remove() return 0 except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2
def verification(username, pwd) -> tuple: try: session_obj = Session() tmp = session_obj.query(user_info).filter_by( username=username).filter_by(pwd=pwd).first() Session.remove() if (tmp == None): return -1, -1 if 'guest' == tmp.identity_type: user_type = 0 else: user_type = 1 return tmp.id, user_type except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2, -2
def remove(user_id) -> int: try: session_obj = Session() ret = session_obj.query(user_info).filter_by(id=user_id).first() if not ret: Session.remove() return -1 #删除用户 tmp = session_obj.query(user_info).get(ret.id) session_obj.delete(tmp) session_obj.commit() Session.remove() except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2 return 0
def file_list(user_id): try: session_obj = Session() now_t = datetime.date.today() - datetime.timedelta(days=15) file_manager.remove_by_day( datetime.datetime(now_t.year, now_t.month, now_t.day, datetime.datetime.now().hour, datetime.datetime.now().minute, datetime.datetime.now().second, 0)) ret = session_obj.query(file_list).options( subqueryload(file_list.user)).filter_by( user_id=user_id).order_by(desc(file_list.time)).all() Session.remove() return ret except Exception as e: session_obj.rollback() Logger().get_logger().error(str(e)) return -1
def generate(self, current_time, timestep): try: # List of requests within a certain timeframe requests = list( Session.execute( query.format(table=self.table, t1=current_time, t2=current_time + timestep))) # List of customers associated with each request customers = [Customer(request) for request in requests] # for r in requests: # print("Iterating R: ", r) # print("Cust: ", len(customers), requests) except: Session.rollback() raise finally: Session.remove() return customers
def remove_temps(temps_id): try: session_obj = Session() ret = session_obj.query(blackbox_temps).filter_by( id=temps_id).first() if not ret: Session.remove() return -1 tmp = session_obj.query(blackbox_temps).get(ret.id) session_obj.delete(tmp) session_obj.commit() Session.remove() return 0 except Exception as e: Logger().get_logger().error(str(e)) session_obj.rollback() return -2
def test_add_item_success(self): r = self.app.put( "/rooms/living-room/items/couch", content_type="application/json", data=json.dumps( { "weight": 65, "description": "Something to sit on", "is_fragile": False } ) ) self.assertEqual(r.status_code, 201) self.assertEqual( json.loads(r.data), {"ref": "/rooms/living-room/items/couch"} ) # Check item has been added Session.remove() new_session = Session() item = new_session.query(Item).filter_by(name="couch").one() self.assertEqual(item.description, "Something to sit on")
def test_update_item_success(self): r = self.app.put( "/rooms/living-room/items/TV", content_type="application/json", data=json.dumps( { "weight": 120, "description": "Cathode tv", "is_fragile": False } ) ) self.assertEqual(r.status_code, 201) self.assertEqual( json.loads(r.data), {"ref": "/rooms/living-room/items/TV"} ) # Check item has been updated Session.remove() new_session = Session() items = new_session.query(Item).all() self.assertEqual(len(items), 1) self.assertEqual(items[0].description, "Cathode tv")
root = None sid = request.sid errno = 0 task_id = '' valid = 0 dbstatus = 0 try: root = ET.fromstring(result) status = root.find('returnstatus').text if status != 'Success': msg = root.find('message').text logger.warning('send failed, msg[%s]' % msg) errno = SEND_FAILED else: task_id = root.find('taskID').text dbstatus = 1 valid = 1 except xml.etree.ElementTree.ParseError: logger.warning("invalid result[%s] request[%s]" % (result, request)) errno = THIRD_SERVER_ERROR except Exception, e: logger.warning('xml parse exception,ult[%s] request[%s]' % (result, request)) UserSms.update(sid, dbstatus, valid, task_id) Session.remove() return errno def heart_beat(self): return True
def clear(): Session.remove()
def tearDown(self): Session.remove()
def shutdown_session(exception=None): Session.remove()
def test_multiple_requests_do_not_share_same_session(self): s1 = Session() # Simulate a new request creating a new thread, and hence a new session Session.remove() s2 = Session() self.assertNotEqual(s1, s2)