def dbupdate(self,whereclause,**kwargs): """ Used to insert exception info into database. Params: module : module name, indicating who raises the exception, e.g. android,ios,psg,adpns,db .etc type : exception type, 0 means service level while 1 is system level. message : exception description,length limit to 256 bytes """ try: i=self._table.update().values(**kwargs).where(whereclause) if direct_engine: engine.execute(i) #gevent.spawn(engine.execute,i) else: session = scoped_session(sessionmaker(bind=engine)) session.execute(i) session.commit() session.close() except Exception,e: #dba_logger.log(40,'Exception when dbwriter:%s' % str(e)) #dba_logger.log(20,'Exception detail:%s' % str(kwargs)) exctrace('db','1','Error happened when updating db',dba_logger,'Exception when dbupdate:%s' % str(e),'Exception detail:%s' % str(kwargs)) if not direct_engine: session.rollback() session.close()
def tmp_dbwrite(tablename,**kwargs): """ Used to insert exception info into database. Params: module : module name, indicating who raises the exception, e.g. android,ios,psg,adpns,db .etc type : exception type, 0 means service level while 1 is system level. message : exception description,length limit to 256 bytes """ try: #_table=Table(tablename, metadata, autoload=True) _table = tables[tablename] i=_table.insert().values(**kwargs) if direct_engine: engine.execute(i) #gevent.spawn(engine.execute,i) #gevent.sleep(0) #gevent.joinall([gevent.spawn(engine.execute,i)]) else: session = scoped_session(sessionmaker(bind=engine)) session.execute(i) session.commit() session.close() except Exception,e: #dba_logger.log(40,'Exception when dbwriter:%s' % str(e)) #dba_logger.log(20,'Exception detail:%s' % str(kwargs)) exctrace('db','1','Error happened when writing db',dba_logger,'Exception when dbwriter:%s' % str(e),'Exception detail:%s' % str(kwargs)) if not direct_engine: session.rollback() session.close()
def db_connect(): envType = os.getenv('ENV_TYPE', 'Local') if envType == 'Local' : pass if envType == 'Prod' : engine.execute("USE ebdb") if envType == 'Develop' : engine.execute("USE ebdb")
def to_db(model, df, append=False): print(f"Uploading {model.__tablename__}") df.columns = [c.lower() for c in df.columns] df = get_new(model, df) if append else df engine.execute(model.__table__.insert(), df.drop_duplicates().to_dict('records')) session.commit() return
def get_command_output(): """ Returns as json the command details that have been processed --- tags: [commands] responses: 200: description: Commands returned OK 400: description: Commands not found """ commands = session.query(Command) db_raw = engine.execute(select([commands])).fetchall() json_result = list() for row in db_raw: json_result.append(dict(row.items())) return dumps(json_result)
def dbquery(self,whereclause): try: i=self._table.select().where(whereclause) if direct_engine: res = engine.execute(i) return res else: session = scoped_session(sessionmaker(bind=engine)) res = session.execute(i) return res session.close() except Exception,e: #dba_logger.log(40,'Exception when dbwriter:%s' % str(e)) #dba_logger.log(20,'Exception detail:%s' % str(kwargs)) exctrace('db','1','Error happened when querying db',dba_logger,'Exception when dbquery:%s' % str(e),'Exception detail:%s' % str(whereclause)) #session.rollback() if not direct_engine: session.close()
import pickle import os def get_model(model_path=None): if model_path is None: model_path = os.environ.get('MODEL_PATH') with open(model_path, 'rb') as f: model = pickle.load(f) return model if __name__ == '__main__': from db import engine pipeline = get_model() result = engine.execute('select * from tweets limit 1').fetchone() data = {} for key, value in result.items(): data[key] = value prediction = pipeline.predict_proba([data['tweet_text']]) print(prediction)
def seed_database(debug=False): """ WARNING: This will NOT create the tables for you if you dropped your entire database. You must re-create the database then run 'Base.metadata.create_all(<engine>)' to create your tables then you can run this without an issue! IMPORTANT: You need to import Base from models.models or else SQLAlchemy won't know about the models and therefore will NOT create the proper tables in your database which will cause this function to fail. Create data in the database by randomly generating values. Once this function is complete, it will write data that is likely to be used in testing to the 'seeded_objects_info.txt' file with separators and linebreaks to make it easier to read. When a user instance is created, its subsequent Profile is created as well and then they are associated with one another. After the user and profile objects are created, create some recipe objects to be associated with the profile object. Leave some profile objects without any recipes for filter testing. """ if debug: # If something happened during object creation # we'll get a UNIQUE CONSTRAINT FAILED error # when trying again so, delete all the records # in the database and try to create the objects. # so we can preserve the existing tables from models.base_model import Base from db import engine for tbl in reversed(Base.metadata.sorted_tables): engine.execute(tbl.delete()) # Create how ever many user and profile objects the user defined user_dicts = [ { 'street_address': "777 Brockton Avenue", 'city': "Abington", "state_or_province": "Massachusetts", 'country': "United States", 'zip_code': "01001", 'email': "*****@*****.**", 'password': "******", }, { 'street_address': "30 Memorial Drive", 'city': "Avon", 'state_or_province': "Massachusetts", 'country': "United States", 'zip_code': "20194", 'email': "*****@*****.**", 'password': "******" }, { 'street_address': "250 Hartford Avenue", 'city': "Toronto", 'state_or_province': "Ontario", 'country': "Canada", 'zip_code': "A1A 1A1", 'email': "*****@*****.**", 'password': "******" }, ] profile_dicts = [{ 'name': "Giuseppe", "is_chef": True, "about_me": "I love to cook and have been doing so for 15 years. My specialty is Italian food", "location": "Massachusettes, United States", 'profile_image': "https://images.unsplash.com/photo-1600565193348-f74bd3c7ccdf?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80", 'favourite_recipe': "Spicy Pork Tenderloin with Apples and Sweet Potatoes", 'favourite_cuisine': "italian,", }, { 'name': "Mario", "is_chef": False, 'about_me': "I love food, if I could eat every hour of the day I would.", "location": "Massachusettes, United States", 'profile_image': "https://images.unsplash.com/photo-1521341057461-6eb5f40b07ab?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80", 'favourite_cuisine': "thai,", }, { 'name': "Tessa", "is_chef": False, 'about_me': "I'm not a chef but wish I was, I couldn't boil noodles without burning them!", "location": "Ontario, Canada", 'profile_image': "https://images.unsplash.com/photo-1505999407077-7937810b98ae?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1188&q=80", 'favourite_cuisine': "Mexican," }] recipe_dicts = [ { 'name': 'Spicy Pork Tenderloin with Apples and Sweet Potatoes', 'description': "A spicy pork tenderloin with delicious green apples and sweet potatoes", 'available': True, 'cuisine': "french", 'price': 1000, 'ingredients': "Pork Tenderloin,Green Apples,Sweet Potatoes,Rosemary", 'required_items': "Dinner Plate, Kitchen Table, Oven", 'image_urls': "https://images.unsplash.com/photo-1598514982205-f36b96d1e8d4?ixid=MXwxMjA3fDB8MHxzZWFyY2h8M3x8cG9yayUyMHRlbmRlcmxvaW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=60https://images.unsplash.com/photo-1598514982205-f36b96d1e8d4?ixid=MXwxMjA3fDB8MHxzZWFyY2h8M3x8cG9yayUyMHRlbmRlcmxvaW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=60," }, { 'name': "Al's Burmese Chicken Curry", 'description': "Chicken", 'available': False, 'cuisine': "indian", 'price': 1000, 'ingredients': "Pork Tenderloin,Green Apples,Sweet Potatoes,Rosemary", 'required_items': "Dinner Plate,Kitchen Table,Oven", 'image_urls': "https://images.unsplash.com/photo-1501200291289-c5a76c232e5f?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=1000&q=60," }, { 'name': "Sweet Potato and Venison Shepherd's Pie", 'description': "Shepherds Pie stuffed with sweet potatoes and venison, cooked to golden perfection", 'available': True, 'cuisine': "french", 'price': 2000, 'ingredients': "Venison,Sweet potatoes,Gravy", 'required_items': "Dinner Plate,Oven", 'image_urls': "https://images.unsplash.com/photo-1600626336264-60ef2a55bd33?ixid=MXwxMjA3fDB8MHxzZWFyY2h8NHx8c2hlcGhlcmRzJTIwcGllfGVufDB8fDB8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=60," }, { 'name': "Gemelli Pasta with Roasted Pumpkin and Pancetta", "description": "Delicious pasta smothered in Pancetta with Roasted Pumpkin", 'available': False, 'cuisine': "italian", 'price': 1500, 'ingredients': "Roasted Pumpkin,Pasta,Pancetta", 'required_items': "Large Pot,Stove,Dinner Table", 'image_urls': "https://images.unsplash.com/photo-1579631542720-3a87824fff86?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=400&q=80," }, { 'name': "Beef Stroganoff with Ground Beef", 'description': "Beef stroganoff filled with ground beef, served with a delicious buttery dinner roll", 'available': True, 'cuisine': "turkish", 'price': 2500, 'ingredients': "Ground Beef,Brown Gravy,Wide Egg Noodles,", 'required_items': "Large Pot,Stove Top,Oven", 'image_urls': "https://images.unsplash.com/photo-1504669221159-56caf7b07f57?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80,", }, ] last_user = None for index in range(len(user_dicts)): print(index) new_user = User.create(**user_dicts[index]) # Create profile new_profile = Profile() new_profile = new_profile.create(**profile_dicts[index]) # Assign the new profile to the new user new_user.assign_one_to_one('profile', new_profile) # If we have another user, create messages between the # last one and the new one if last_user != None: new_conversation = Conversation.create(**{ 'user_one': last_user, 'user_two': new_user }) # Create messages for the two users last_users_message = Message.create( **{ 'sender': last_user, 'content': "Hello %s how are you?" % (new_user.profile.name) }) new_users_message = Message.create( **{ 'sender': new_user, 'content': "I'm good %s how about you?" % (last_user.profile.name) }) # Add messages to the conversation new_conversation.add_to_relationship('messages', last_users_message) new_conversation.add_to_relationship('messages', new_users_message) # Create 5 recipes for each user/profile new_recipe = Recipe.create(**recipe_dicts[index]) # Add the new recipe to the One to Many field in the Profile model new_profile.add_to_relationship('recipes', new_recipe) last_user = new_user print_info(user_dicts, profile_dicts, recipe_dicts)
def __init__(self, name, selectable): self.name = name self.selectable = selectable print("CreateView called") engine.execute(self)
def get_max_group_id(): result = engine.execute( "select max(group_id) from groups").fetchall()[0][0] if result == None: result = 0 return result
if s is not '*None*': source = Source(source=s) person.sources.append(source) for q in sources[s]: quote = Quote(quote=q) person.quotes.append(quote) if s is not '*None*': source.quotes.append(quote) session.add(person) session.commit() session.close() if __name__ == "__main__": sql = text('DROP TABLE IF EXISTS quotes;') result = engine.execute(sql) sql = text('DROP TABLE IF EXISTS sources;') result = engine.execute(sql) sql = text('DROP TABLE IF EXISTS people;') result = engine.execute(sql) Base.metadata.create_all(engine) # skip these weirdly formatted pages bad_pages = [ 'Anonymous', 'Rani Mukerji', 'Sappho', 'George Herbert', 'Noel Fielding', 'Giovanni Rucellai', 'Pope Sixtus V', 'Geoffrey Chaucer', 'Stefano Guazzo' ] try: with open('raw/_people_list_.txt', 'r') as f: people_pages = f.readlines()