def prepare_db(): from models import database, FetchedUser, User, Comment, Like from models import Status, Gossip, Album, Photo, Blog with database: database.create_tables([FetchedUser, User, Comment, Like]) database.create_tables([Status, Gossip, Album, Photo, Blog])
def create_tables(): database.connect() try: database.create_tables([Pokemon]) database.commit() finally: database.close()
def init_db(): """Tables creation and test data initiation function""" with database: log.info("Drop tables...") database.drop_tables([ TemperatureLocal, TemperatureApi1, PressureLocal, PressureApi1, HumidityLocal, HumidityApi1 ]) log.info("Done...") log.info("Create tables...") database.create_tables([ TemperatureLocal, TemperatureApi1, PressureLocal, PressureApi1, HumidityLocal, HumidityApi1 ]) log.info("Done") log.info("Adding test data") for test_data in range(40): for i in (TemperatureLocal, TemperatureApi1): i.create(date_time=generate_mock_date(), temperature=generate_mock_data(), temperature_min=generate_mock_data(), temperature_max=generate_mock_data()) for i in (PressureLocal, PressureApi1): i.create(date_time=generate_mock_date(), pressure=generate_mock_data(), pressure_min=generate_mock_data(), pressure_max=generate_mock_data()) for i in (HumidityLocal, HumidityApi1): i.create(date_time=generate_mock_date(), humidity=generate_mock_data(), humidity_min=generate_mock_data(), humidity_max=generate_mock_data())
def init(): if database.get_tables(): database.drop_tables(models) print('База данных удалена') database.create_tables(models) else: database.create_tables(models) print('База данных создана')
def create_tables(): logger = logging.getLogger('FUNC') if os.path.exists(DIRECTORY + DATA_BACKUP_FILE): logger.info('DB file already exists, updating data') updateData() return False with database: database.create_tables([Competition]) logger.info('DB file created') return True
def populate_test_data(): with database: database.drop_tables([Department, Person]) database.create_tables([Department, Person]) dpts = [('Бухгалтерия',), ('Отдел автоматизации',), ('Отдел продаж',)] with database.atomic(): Department.insert_many(dpts, fields=[Department.name]).execute() Person.insert_many(get_persons()).execute()
def fetch_user(uid, args): from models import database, User, Comment, Like fetched = False with database: database.create_tables([User, Comment, Like]) if args.fetch_status: print('prepare to fetch status') from models import Status from crawl import status as crawl_status database.create_tables([Status]) status_count = crawl_status.get_status(uid) print('fetched {status_count} status'.format( status_count=status_count)) fetched = True if args.fetch_gossip: print('prepare to fetch gossip') from models import Gossip from crawl import gossip as crawl_gossip database.create_tables([Gossip]) gossip_count = crawl_gossip.get_gossip(uid) print('fetched {gossip_count} gossips'.format( gossip_count=gossip_count)) fetched = True if args.fetch_album: print('prepare to fetch albums') from models import Album, Photo from crawl import album as crawl_album database.create_tables([Album, Photo]) album_count = crawl_album.get_albums(uid) print( 'fetched {album_count} albums'.format(album_count=album_count)) fetched = True if args.fetch_blog: print('prepare to fetch blogs') from models import Blog from crawl import blog as crawl_blog database.create_tables([Blog]) blog_count = crawl_blog.get_blogs(uid) print('fetched {blog_count} blogs'.format(blog_count=blog_count)) fetched = True return fetched
def create_tables(): database.connect() database.create_tables([Goods, Admins, Orders_Info, Orders_Content], safe=True) Goods.create(name='.BASE_CAT', amount=0) Goods.create(name='Браслеты', amount=100, parent_id=1) Goods.create(name='Значки', amount=75, parent_id=1) Goods.create(name='Кружки', amount=150, price=100, parent_id=1) Goods.create(name='Синие', amount=50, price=10, parent_id=2) Goods.create(name='Красные', amount=36, price=15, parent_id=2) Goods.create(name='Желтые', amount=14, price=20, parent_id=2) Goods.create(name='Жестяные', amount=30, price=17, parent_id=3) Goods.create(name='Деревянные', amount=45, price=13, parent_id=3) Admins.create(chat_id='1234') database.close()
def create_tables(): '''Creates the tables in the database.''' tables_created = True try: # We want to know what tables we need to create. missing_tables = [ table for table in [DatabaseInfo, Prop, Job, Recipient] if not table.table_exists() ] database.create_tables(missing_tables) except Exception as e: logging.getLogger().error(f'Error while creating tables: {str(e)}') tables_created = False return tables_created
def update_fetch_info(uid): from models import database, FetchedUser, User, Status, Gossip, Album, Photo, Blog with database: database.create_tables([FetchedUser]) user = User.get_or_none(User.uid == uid) if not user: raise KeyError("no such user") fetched = model_to_dict(user) fetched.update( status=Status.select().where(Status.uid == uid).count(), gossip=Gossip.select().where(Gossip.uid == uid).count(), album=Album.select().where(Album.uid == uid).count(), photo=Photo.select().where(Photo.uid == uid).count(), blog=Blog.select().where(Blog.uid == uid).count(), ) FetchedUser.insert(**fetched).on_conflict('replace').execute() print('update fetched info {fetched}'.format(fetched=fetched)) return True
def create_tables(): """Создание БД""" database.connect() database.create_tables([Repo, Commit])
from app import models import models.database database.create_tables([User, State, City, Place, Amenity, PlaceBook, PlaceAmenities])
with open(filepath) as f: with multiprocessing.Pool() as p: for line in f: records.append(make_comment_dict(json.loads(line))) if len(records) == BATCH_SIZE: msg = ('Inserted records {} to {}' .format(count - BATCH_SIZE + 1, count)) callback = functools.partial(print, msg) p.apply_async(insert, (records,), callback=callback) records = [] count += 1 p.close() p.join() return count if __name__ == '__main__': if len(sys.argv) != 2: exit('USAGE: python insert_from_json.py <json file path>') start = time.time() database.create_tables( [Author, Comment, CommentParent, Subreddit], safe=True ) inserted = insert_from_json(sys.argv[1]) end = time.time() elapsed = end - start print('Inserted {} records in {}s'.format(inserted, elapsed))
def save_rank(self, rank_data_list): count = 0 for rank_data in rank_data_list: _, create_result = Rank.get_or_create(**rank_data) if create_result: count += 1 self.logger.info(u'存储 rank 数据完成,共:{}'.format(count)) def save_hall(self, hall_data_list): count = 0 for hall_data in hall_data_list: _, create_result = Hall.get_or_create(**hall_data) if create_result: count += 1 self.logger.info(u'存储 hall 数据完成,共:{}'.format(count)) def save_content(self, title, description, chart): Content.get_or_create(title=title, description=description, fetch_date=self.today, chart_str=chart) self.logger.info(u'存储 content 数据完成。') if __name__ == '__main__': database.create_tables([Content, Rank, Hall], safe=True) RSS_URL = 'https://www.tiobe.com/tiobe-index/rss.xml' INDEX_URL = 'https://www.tiobe.com/tiobe-index/' t = Tiobe(RSS_URL, INDEX_URL) t.fetch()
def create_table(): database.create_tables([User, Book, Author, Shelf, BookShelf, BookAuthor, BookTranslator, UserAuthorRelation, UserRelation])
"/force_create_lobby - закрыть старую комнату и создать новую\n" "/join_lobby - присоединиться к комнате\n" "/leave_lobby - выйти из комнаты\n" "/members_lobby - список участников", quote=False) def error(bot: Bot, update: Update, error): """Log Errors caused by Updates.""" logger.warning('Update "%s" caused error "%s"', update, error) # default commands dispatcher.add_error_handler(error) dispatcher.add_handler(CommandHandler('start', start)) dispatcher.add_handler(CommandHandler('help', help)) dispatcher.add_handler(CommandHandler('create_lobby', lobby.create_lobby)) dispatcher.add_handler( CommandHandler('force_create_lobby', lobby.force_create_lobby)) dispatcher.add_handler(CommandHandler('join_lobby', lobby.join_lobby)) dispatcher.add_handler(CommandHandler('leave_lobby', lobby.leave_lobby)) dispatcher.add_handler(CommandHandler('members_lobby', lobby.members_lobby)) dispatcher.add_handler(CommandHandler('start_game', lobby.start_game)) if __name__ == '__main__': database.create_tables([ models.Lobby, models.LobbyMember, models.Player, models.Card, models.PlayerCards ]) updater.start_polling() updater.idle()
def create_tables(): with database: database.create_tables([Data, Experiment, Analysis])
def create_tables(): database.create_tables([Customer, Room, Type, Booking, Status])
from app import models import models.database database.create_tables( [User, State, City, Place, Amenity, PlaceBook, PlaceAmenities])
def initialise(self): database.connect() database.create_tables([Entry], safe=True)