def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """ This function will add a new customer to the customers.db database """ logger.info('In add_customer().') database = SqliteDatabase('customers.db') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') with database.transaction(): new_customer = Customer.create(customer_id=customer_id, name=name, lastname=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save() logger.info('Add new customer to Customer database successfully.') except Exception as e: logger.info(f'Error creating {name} customer record.') logger.info(e) finally: logger.info('database closes.') database.close()
def initialize_database(path: Optional[str] = None): path = path if path is not None else join(expanduser("~"), '.hyperion.db') database = SqliteDatabase(path) database_proxy.initialize(database) database.connect() database.create_tables([Neighbourhood, Bike, Location, Link, Postcode], safe=True)
def CreateAllTables(self): database = SqliteDatabase('database.db', pragmas={'foreign_keys': 1}) database.connect() database.create_tables([ Player, LeaderboardRecord, Level, Terrain, Background, Monster, TerrainInfo, BackgroundInfo, MonsterInfo ])
class Database(object): def __init__(self, database=None): self.database = database if self.database is None: self.load_database() self.Model = self.get_model_class() def load_database(self): self.database = SqliteDatabase(settings.SQLITE_FILEPATH) def get_model_class(self): class BaseModel(Model): class Meta: database = self.database return BaseModel def connect_db(self): self.database.connect() def close_db(self): if not self.database.is_closed(): self.database.close()
class PWDatabase: __proxy = None @staticmethod def DBProxy(): if not PWDatabase.__proxy: PWDatabase.__proxy = Proxy() return PWDatabase.__proxy _db = None def __init__(self, path): try: self._db = SqliteDatabase(path, check_same_thread=False) PWDatabase.DBProxy().initialize(self._db) self.startup() except Exception as e: logger.error("database file does not exist, or incorrect permissions") def close(self): self._db.close() self._db = None def startup(self): self._db.connect() @property def DB(self): return self._db
def PEEWEE(): from peewee import SqliteDatabase, Model, CharField, DateField, BooleanField, ForeignKeyField db = SqliteDatabase('people.db') class Person(Model): name = CharField() birthday = DateField() gender = BooleanField() class Meta: database = db class Pet(Model): owner = ForeignKeyField(Person, related_name='pets') name = CharField() type = CharField() class Meta: database = db db.connect() # db.create_tables([Person, Pet]) from datetime import date me = Person(name='max', birthday=date(1998, 7, 8), gender=True) modified_1 = me.save() my_pet = Pet(owner=me, name='a', type='b') modified_2 = my_pet.save() print(modified_1, modified_2) # 1 1 print(me, my_pet) me = Person.select().where(Person.name == 'max').get() my_pet = Person.select().where(Pet.owner == me).get() print(me, my_pet)
def create_database(): with open("/home/wojjak/PycharmProjects/untitled/data.db", "a") as f: f.write("") db = SqliteDatabase("data.db") db.connect() db.create_tables([Client]) db.create_tables([User])
def main(): db = SqliteDatabase('daily.sqlite') class Daily_2021(Model): month = IntegerField() day = IntegerField() text = CharField(max_length=1000) verset = CharField() class Meta: database = db db.connect() db.create_tables([Daily_2021]) # result = get_text() for month in range(1, 13): for day in range(1, get_month_days(month) + 1): result = get_text(month, day) print(f"{month} {day} {result[0]} {result[1]}") Daily_2021.create(month=month, day=day, text=result[0], verset=result[1]) print()
def test_writes_db_to_archive(self): with tempfile.NamedTemporaryFile() as zffobj: zf = zipfile.ZipFile(zffobj, "w") with tempfile.NamedTemporaryFile() as dbfobj: db = SqliteDatabase(dbfobj.name) db.connect() with Using(db, [Item]): Item.create_table() item = Item(id="test", title="test", description="test", available=False, slug="srug", kind=NodeType.video, path="/test/test") item.save() db.close() save_db(db, zf) zf.close() # reopen the db from the zip, see if our object was saved with tempfile.NamedTemporaryFile() as f: # we should only have one file in the zipfile, the db. Assume # that the first file is the db. zf = zipfile.ZipFile(zffobj.name) dbfobj = zf.open(zf.infolist()[0]) f.write(dbfobj.read()) f.seek(0) db = SqliteDatabase(f.name) with Using(db, [Item]): Item.get(title="test")
def _create_database(self): db = SqliteDatabase(self._filepath) for model in self._tables: model.bind(db, bind_refs=False, bind_backrefs=False) db.connect() db.create_tables(self._tables) return db
class DatabaseConnector: """Base class for establishing connection with database, creating and dropping tables""" def __init__(self, database_name='./task-manager.db'): self.database_name = database_name self.database = SqliteDatabase(database_name) self.connected = False database_proxy.initialize(self.database) self.create_tables() def create_tables(self): if not self.connected: self.database.connect() self.connected = True Category.create_table(True) Notification.create_table(True) Task.create_table(True) TaskPlan.create_table(True) UsersReadTasks.create_table(True) UsersWriteTasks.create_table(True) def drop_tables(self): self.database.drop_tables([ Task, UsersReadTasks, UsersWriteTasks, Category, Notification, TaskPlan ]) self.database.close() self.connected = False
def loader(): database = SqliteDatabase(dbname) names = 0 donations = 1 try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') for entry in raw_input: with database.transaction(): new_donor = Donor.create(name=entry[names]) # new_donor.save() new_donation = Donation.create(gift_id=uuid.uuid4(), value=entry[donations], donated_by=entry[names], gift_num=1) # new_donation.save() logging.info('Database add successful') # except Exception as e: # logging.info(e) finally: logging.info('database closes') database.close()
def delete_artwork_in_db(self, name_of_artwork): database = SqliteDatabase(self.db) database.connect() delete = Artwork.delete().where( Artwork.name_of_artwork == name_of_artwork).execute() database.close() return delete
class Database: def __init__(self): self.movies = SqliteDatabase('movies.db') def setup(self): self.movies.connect() self.movies.create_table(Movie) def add(self, movie): row = Movie.create( serial=movie.serial, title=movie.metadata.title, image=movie.metadata.image, year=movie.metadata.year, plot=movie.metadata.plot, runtime=movie.metadata.runtime, rating=movie.metadata.rating, format=movie.metadata.format, resolution=movie.metadata.resolution, filename=movie.metadata.filename, magnet_link=movie.metadata.magnet_link, downloaded=False ) row.save() def is_duplicate(self, serial): db_movie = Movie.select().where(Movie.serial == serial) return db_movie is None
def __init__(self, database: SqliteDatabase, view: MainView, presenter: MainPresenter, builder: MainBuilder, udev_interactor: UdevInteractor, *args: Any, **kwargs: Any) -> None: LOG.debug("init Application") GLib.set_application_name(_(APP_NAME)) super().__init__(*args, application_id=APP_ID, flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, **kwargs) database.connect() database.create_tables( [SpeedProfile, SpeedStep, CurrentSpeedProfile, Setting]) if SpeedProfile.select().count() == 0: load_db_default_data() self.add_main_option_entries(self._get_main_option_entries()) self._view = view self._presenter = presenter self._presenter.application_quit = self.quit self._window: Optional[Gtk.ApplicationWindow] = None self._builder: Gtk.Builder = builder self._udev_interactor = udev_interactor self._start_hidden: bool = False
def create_db(): import os if not ('HEROKU' in os.environ): db = SqliteDatabase('sender.sqlite') db.connect(True) db.drop_tables([Account, Subscription, Messenger, AccessToken]) db.create_tables([Account, Subscription, Messenger, AccessToken]) vk = Messenger.create(name='VK', cost=200) vk.save() telegram = Messenger.create(name='Telegram', cost=200) telegram.save() else: init_db() db_proxy.connect(True) print('CONNECTED') db_proxy.create_tables([AdminPage, TargetGroup, UserPage, SenderPage], safe=True) print('before AdminPage') yuri = AdminPage(vkid=142872618) yuri.save() print('before db.close()') db_proxy.close() return 'DB is created!'
def create_db(): import os if not ('DYNO' in os.environ): db = SqliteDatabase('../sender.sqlite') db.connect(True) db.drop_tables([AdminPage, TargetGroup, UserPage, SenderPage]) db.create_tables([AdminPage, TargetGroup, UserPage, SenderPage]) yuri = AdminPage(vkid=142872618) yuri.save() else: init_db() db_proxy.connect(True) print('CONNECTED') # TODO сделать так, чтобы дубликаты не добавлялись db_proxy.create_tables([AdminPage, TargetGroup, UserPage, SenderPage], safe=True) print('before AdminPage') yuri = AdminPage(vkid=142872618) yuri.save() print('before db.close()') db_proxy.close() return 'DB is created!'
def wrapper(*args, **kwargs): language = kwargs.get("language", "en") path = kwargs.pop("database_path", None) if not path: path = CONTENT_DATABASE_PATH.format(channel=kwargs.get( "channel", CHANNEL), language=language) db = SqliteDatabase(path, pragmas=settings.CONTENT_DB_SQLITE_PRAGMAS) kwargs["db"] = db db.connect() # This should contain all models in the database to make them available to the wrapped function with Using(db, [Item, AssessmentItem]): try: output = function(*args, **kwargs) except DoesNotExist: output = None except OperationalError as e: logging.error( "Content DB error: Perhaps content database file found? " "Exception: {e}".format(e=str(e))) raise db.close() return output
class NonDeterminismCatcher: __db = None __logging = None def __init__(self, logger) -> None: self.__db = SqliteDatabase('non_determinism_quic.db') self.__db.connect() self.__db.drop_tables( [LearningRunModel, NonDeterministicResponseModel]) self.__db.create_tables( [LearningRunModel, NonDeterministicResponseModel]) self.__logging = logger logger = logging.getLogger('peewee') logger.addHandler(logging.StreamHandler()) logger.setLevel(logging.DEBUG) def add_run(self, q, res): # Check if there is already a record with this query try: if not res: res = "---" previous_run = LearningRunModel.get(LearningRunModel.run == q) self.__logging.info("Received query {}".format(q)) if not previous_run.result == res: self.__logging.info("Not deterministic with {}".format( previous_run.result)) NonDeterministicResponseModel(run=q, result=res).save() except LearningRunModel.DoesNotExist: self.__logging.info("New run inserted.") LearningRunModel(run=q, result=res).save()
def wrapper(*args, **kwargs): language = kwargs.get("language", "en") path = kwargs.pop("database_path", None) if not path: path = CONTENT_DATABASE_PATH.format( channel=kwargs.get("channel", CHANNEL), language=language ) db = SqliteDatabase(path, pragmas=settings.CONTENT_DB_SQLITE_PRAGMAS) kwargs["db"] = db db.connect() # This should contain all models in the database to make them available to the wrapped function with Using(db, [Item, AssessmentItem]): try: output = function(*args, **kwargs) except DoesNotExist: output = None except OperationalError: logging.error("No content database file found") raise db.close() return output
class SubstitutableDatabase(object): def __init__(self, filepath=":memory:", tables=[]): self._tables = tables self._create_database(filepath) def _create_database(self, filepath): self._db = SqliteDatabase( abspath(filepath) if filepath != ":memory:" else filepath, pragmas={"foreign_keys": 1}, ) for model in self._tables: model.bind(self._db, bind_refs=False, bind_backrefs=False) self._db.connect() self._db.create_tables(self._tables, safe=True) def _change_path(self, filepath): self.close() self._create_database(filepath) def _vacuum(self): print("Vacuuming database ") self.execute_sql("VACUUM;") def __getattr__(self, attr): return getattr(self._db, attr)
class PWDatabase(object): __proxy = None @staticmethod def DBProxy(): if not PWDatabase.__proxy: PWDatabase.__proxy = Proxy() return PWDatabase.__proxy _db = None def __init__(self, path): try: self._db = SqliteDatabase(path, check_same_thread=False) PWDatabase.DBProxy().initialize(self._db) self.startup() except Exception as e: logger.error( "database file does not exist, or incorrect permissions") def close(self): self._db.close() self._db = None def startup(self): self._db.connect() @property def DB(self): return self._db
def db_change_artwork_availability(self, name_of_artwork, availability): database = SqliteDatabase(self.db) database.connect() updated_artwork = Artwork.update(available=availability).where( Artwork.name_of_artwork == name_of_artwork).execute() database.close() return updated_artwork
def empty(self, *args, **options): """ Creates an empty content database for the Khan channel. This ensures that an empty content database exists in the default distribution and for tests. Especially useful for creating an *EMPTY TEMPLATE* retrievecontentpack empty en --template """ lang = args[1] if not options.get('template', False): content_db_path = topic_settings.CONTENT_DATABASE_PATH.format( channel=topic_settings.CHANNEL, language=lang, ) else: content_db_path = topic_settings.CONTENT_DATABASE_TEMPLATE_PATH.format( channel=topic_settings.CHANNEL, language=lang, ) if os.path.exists(content_db_path): if options.get("force", False): os.unlink(content_db_path) else: raise CommandError( "Content database already exists: {}".format( content_db_path)) db = SqliteDatabase(content_db_path) db.connect() db.create_table(Item, safe=True) db.create_table(AssessmentItem, safe=True) db.close() self.complete( _("Saved empty content database in {}.").format(content_db_path))
def search_db_for_artwork_by_artist(self, name): database = SqliteDatabase(self.db) database.connect() # Locate all artwork with the corresponding name artwork_list = Artwork.select().where(Artwork.artist == name) # Display all artwork database.close() return artwork_list
def add_new_artist_to_db(self, name, email): database = SqliteDatabase(self.db) database.connect() # Add the new artist to the db new_artist = Artist(name=name, email=email) new_artist.save() database.close() return new_artist
def report(): database = SqliteDatabase('mailroom.db') database.connect() print('Last Name____Title__Total Donations__Number of Donations') donor_table = database.execute_sql('select * from donor;') for donor in donor_table: print(donor) database.close()
def create_database(db): if not os.path.isfile(db): database = SqliteDatabase(db) database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') database.create_tables([Customer]) database.close()
def OpenSqliteDatabase(database_file): """ """ database = SqliteDatabase(database_file) # Configure our proxy to use the db we specified in config. DATABASE_PROXY.initialize(database) database.connect() return database
class ValveDriverTest(unittest.TestCase): @classmethod def setUpClass(cls): fakesleep.monkey_patch() SetTestMode() @classmethod def tearDownClass(cls): fakesleep.monkey_restore() def setUp(self): self.test_db = SqliteDatabase(':memory:') self.test_db.bind(MODELS) self.test_db.connect() self.test_db.create_tables(MODELS) def tearDown(self): self.test_db.drop_tables(MODELS) self.test_db.close() def test_valve_driver(self): valve_output_1 = Output.create(number=2) valve_1 = Valve.create(number=1, name='valve 1', delay=30, output=valve_output_1) SetUpTestInjections(output_controller=mock.Mock(OutputController)) driver_1 = ValveDriver(valve_1) self.assertEqual(valve_1.id, driver_1.id) self.assertEqual(0, driver_1.percentage) self.assertEqual(0, driver_1._desired_percentage) self.assertFalse(driver_1.is_open) self.assertFalse(driver_1.in_transition) driver_1.set(50) self.assertEqual(50, driver_1._desired_percentage) driver_1.close() self.assertEqual(0, driver_1._desired_percentage) driver_1.open() self.assertEqual(100, driver_1._desired_percentage) self.assertTrue(driver_1.will_open) driver_1.steer_output() driver_1._output_controller.set_output_status.assert_called_once() self.assertFalse(driver_1.will_open) self.assertEqual(100, driver_1.percentage) self.assertFalse(driver_1.is_open) self.assertTrue(driver_1.in_transition) time.sleep(20) self.assertFalse(driver_1.is_open) self.assertTrue(driver_1.in_transition) time.sleep(15) self.assertTrue(driver_1.is_open) self.assertFalse(driver_1.in_transition)
def init(dbname="blender-models.db"): db = SqliteDatabase(path.join(MORSEWEB_ROOT, dbname)) db.connect() if not BlenderModel.table_exists(): db.create_table(BlenderModel) for pathname in RESOURCES: populate(pathname)
def database(): """ Setup the test database. """ test_db = SqliteDatabase(':memory:') test_db.bind([Message, HasTapped], bind_refs=False, bind_backrefs=False) test_db.connect() test_db.create_tables([Message, HasTapped]) HasTapped.create(has_tapped=0)
def _jobs(sample_sheet, data_file): "Verify job configuration by actually running them on sample data, through their submitter." # create an in-mem database from peewee import SqliteDatabase db = SqliteDatabase(":memory:", pragmas={'foreign_keys': 1}, autoconnect=False) db.bind(models.REGISTRY, bind_refs=True, bind_backrefs=True) db.connect() db.create_tables(models.REGISTRY) from porerefiner.config import Config Config['nanopore']['path'] = data_file.parent # create a run ru = Run.create(name=data_file.parent.name, ended=datetime.datetime.now(), status='DONE', path=data_file.parent) # load the sample sheet # save the sample to the run with open(sample_sheet, 'rb') as sheet: ss = SampleSheet.new_sheet_from_message(sheet=( samplesheets.load_from_csv, samplesheets.load_from_excel)['xslx' in sample_sheet.name](sheet), run=ru) with open(data_file, 'rb') as data: hash = hashlib.md5() hash.update(data.read()) md5 = hash.hexdigest() # create a file and add it to the run fi = models.File.create( path=data_file, run=ru, checksum=md5, ) async def fileJob(job): j = fi.spawn(job) await jobs.submit_job(j) while j.status not in ('DONE', 'FAILED'): await jobs.poll_active_job(j) async def runJob(job): j = ru.spawn(job) await jobs.submit_job(j) while j.status not in ('DONE', 'FAILED'): await jobs.poll_active_job(j) async def task(): await gather(*[fileJob(job) for job in jobs.JOBS.FILES] + [runJob(job) for job in jobs.JOBS.RUNS]) run(task())
class TestManage(unittest.TestCase): def setUp(self): self.db = SqliteDatabase('peewee.db') self.db.connect() def tearDown(self): self.db.close() def testCreate(self): self.db.create_tables(ALL_MODELS, safe=True)
def init_models(app): if app.config['TESTING']: database = SqliteDatabase(':memory:') else: database = SqliteDatabase('local.db') database_proxy.initialize(database) database.connect() database.create_tables([ TodoItem, ], safe=True)
def bundle_language_pack(dest, nodes, frontend_catalog, backend_catalog, metadata, assessment_items, assessment_files, subtitles, html_exercise_path): # make sure dest's parent directories exist pathlib.Path(dest).parent.mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(dest, "w") as zf, tempfile.NamedTemporaryFile() as dbf: db = SqliteDatabase(dbf.name) db.connect() nodes = convert_dicts_to_models(nodes) nodes = mark_exercises_as_available(nodes) nodes = list(save_models(nodes, db)) # we have to make sure to force # the evaluation of each # save_models call, in order to # avoid nesting them. nodes = list(populate_parent_foreign_keys(nodes)) list(save_models(nodes, db)) nodes = recurse_availability_up_tree(nodes, db) list(save_models(nodes, db)) assessment_items = convert_dicts_to_assessment_items(assessment_items) list(save_assessment_items(assessment_items, db)) db.close() dbf.flush() save_catalog(frontend_catalog, zf, "frontend.mo") save_catalog(backend_catalog, zf, "backend.mo") # save_subtitles(subtitle_path, zf) try: # sometimes we have no html exercises save_html_exercises(html_exercise_path, zf) except FileNotFoundError: logging.warning("No html exercises found; skipping.") save_db(db, zf) save_metadata(zf, metadata) for file_path in assessment_files: save_assessment_file(file_path, zf) write_assessment_version(metadata, zf) for subtitle_path in subtitles: save_subtitle(subtitle_path, zf) return dest
def init(): path = os.path.expanduser('~/.config/aesop/database.db') global database database = SqliteDatabase(path) database_proxy.initialize(database) database.connect() for model in BaseModel.__subclasses__(): try: database.create_table(model) except Exception: pass else: if model == Config: Config.create_default()
def test_database_generation_without_args(runner, tmp_context): result = runner.invoke(pokediadb, ["generate", "-v"]) assert result.exit_code == 0 # Check existence of the database and the csv and sprites folders db_file = tmp_context.join("pokediadb.sql") assert db_file.check(file=1) assert tmp_context.join("csv").check(dir=1) assert tmp_context.join("sprites").check(dir=1) # Check type tables db = SqliteDatabase(db_file.strpath) db.connect() assert len(Type.select()) == 18 assert len(TypeEfficacy.select()) == 324 assert len(TypeTranslation.select()) == 36
def crapTheData(config): db = SqliteDatabase(config['database_name']+'.db') db.connect() for name,url in config['urls'].iteritems(): print name, 'craping '+url maxnum = generateTable(url, config['params'][name], name, db) print 'we need to crap',maxnum,' rows data' # db.create_tables([Table],safe=True) datalist = [] for i in range(maxnum): params = config['params'][name] params['page'] = i res = requests.get(url, params ,headers=headers) print i, len(datalist) data = res.json().get('tngou') if data: datalist.append(data[0]) storeData(config['database_name']+name+'.pic', datalist)
def empty(self, *args, **options): """ Creates an empty content database for the Khan channel. This ensures that an empty content database exists in the default distribution and for tests. Especially useful for creating an *EMPTY TEMPLATE* retrievecontentpack empty en --template """ lang = args[1] if not options.get('template', False): content_db_path = topic_settings.CONTENT_DATABASE_PATH.format( channel=topic_settings.CHANNEL, language=lang, ) else: content_db_path = topic_settings.CONTENT_DATABASE_TEMPLATE_PATH.format( channel=topic_settings.CHANNEL, language=lang, ) if os.path.exists(content_db_path): if options.get("force", False): os.unlink(content_db_path) else: raise CommandError( "Content database already exists: {}".format( content_db_path ) ) db = SqliteDatabase( content_db_path ) db.connect() db.create_table(Item, safe=True) db.create_table(AssessmentItem, safe=True) db.close() self.complete( _("Saved empty content database in {}.").format( content_db_path ) )
def bundle_language_pack(dest, nodes, frontend_catalog, backend_catalog, metadata, assessment_items, assessment_files, subtitles): with zipfile.ZipFile(dest, "w") as zf, tempfile.NamedTemporaryFile() as dbf: db = SqliteDatabase(dbf.name) db.connect() nodes = convert_dicts_to_models(nodes) nodes = mark_exercises_as_available(nodes) nodes = list(save_models(nodes, db)) # we have to make sure to force # the evaluation of each # save_models call, in order to # avoid nesting them. nodes = list(populate_parent_foreign_keys(nodes)) list(save_models(nodes, db)) nodes = recurse_availability_up_tree(nodes, db) list(save_models(nodes, db)) assessment_items = convert_dicts_to_assessment_items(assessment_items) list(save_assessment_items(assessment_items, db)) db.close() dbf.flush() save_catalog(frontend_catalog, zf, "frontend.mo") save_catalog(backend_catalog, zf, "backend.mo") # save_subtitles(subtitle_path, zf) save_db(db, zf) save_metadata(zf, metadata) for file_path in assessment_files: save_assessment_file(file_path, zf) for subtitle_path in subtitles: save_subtitle(subtitle_path, zf) return dest
def test_deferred_database(self): deferred_db = SqliteDatabase(None) self.assertTrue(deferred_db.deferred) class DeferredModel(Model): class Meta: database = deferred_db self.assertRaises(Exception, deferred_db.connect) sq = DeferredModel.select() self.assertRaises(Exception, sq.execute) deferred_db.init(':memory:') self.assertFalse(deferred_db.deferred) # connecting works conn = deferred_db.connect() DeferredModel.create_table() sq = DeferredModel.select() self.assertEqual(list(sq), []) deferred_db.init(None) self.assertTrue(deferred_db.deferred)
from tornado.options import options from peewee import create_model_tables,Model,SqliteDatabase from tornado.options import options __all__ = ['BaseLogModel','BaseMainModel'] DBLog = SqliteDatabase(options.db_main) DBLog.connect() DBMain = SqliteDatabase(options.db_log) DBMain.connect() class BaseLogModel(Model): class Meta: database = DBLog class BaseMainModel(Model): class Meta: database = DBMain
import os import msgpack from peewee import SqliteDatabase, Model from peewee import BooleanField, DateTimeField, IntegerField, TextField from recordtype import recordtype from config import config from features import all_features as real_features from features import _support_features import utils all_features = dict(real_features.items() + _support_features.items()) erepo_db = SqliteDatabase('erepo.db', threadlocals=True) erepo_db.connect() class _Serializable(object): """Mixin to support serialization of a custom class. By default, recordtype._asdict is used.""" __slots__ = () @classmethod def _pack(cls, obj): return obj._asdict() @classmethod def _unpack(cls, data):
ask_volume_1=self.ask_volume_1, gateway_name=self.gateway_name, ) if self.bid_price_2: tick.bid_price_2 = self.bid_price_2 tick.bid_price_3 = self.bid_price_3 tick.bid_price_4 = self.bid_price_4 tick.bid_price_5 = self.bid_price_5 tick.ask_price_2 = self.ask_price_2 tick.ask_price_3 = self.ask_price_3 tick.ask_price_4 = self.ask_price_4 tick.ask_price_5 = self.ask_price_5 tick.bid_volume_2 = self.bid_volume_2 tick.bid_volume_3 = self.bid_volume_3 tick.bid_volume_4 = self.bid_volume_4 tick.bid_volume_5 = self.bid_volume_5 tick.ask_volume_2 = self.ask_volume_2 tick.ask_volume_3 = self.ask_volume_3 tick.ask_volume_4 = self.ask_volume_4 tick.ask_volume_5 = self.ask_volume_5 return tick DB.connect() DB.create_tables([DbBarData, DbTickData])
class TestBase(unittest.TestCase): def populate_database(self): self.db = SqliteDatabase('peewee.db') self.db.connect() self.db.create_tables(model.ALL_MODELS, safe=True) for m in model.ALL_MODELS: m.delete().execute() self.db.close() # Config release = model.Config.create(app_api_key=APP_API_KEY, messaging_api_key=MESSAGING_API_KEY) admin_user = model.User.create_user(name='Administrator', description='Administrator', email='*****@*****.**', username=TEST_USER, password=TEST_PASSWORD) # Groups admin = model.Group.create(name=TEST_USER, owner=admin_user) user = model.Group.create(name='user', owner=admin_user) guest = model.Group.create(name='guest', owner=admin_user) admin.add_user(admin_user) # Users model.Device.create(user=admin, name='d2', resource='work', type='computer', dev_id='a') chloe = model.User.create_user(name='Chloe', username='******', password=TEST_PASSWORD) d = chloe.create_device(name='d2', resource='work', type='computer', dev_id='a') chloe.create_device(name='d0', resource='home', type='phone', dev_id='b') chloe.create_device(name='d3', resource='home', type='laptop', dev_id='c') chloe.create_device(name='d1', resource='work', type='phone', dev_id='d') model.UserToGroup.create(user=chloe, group=guest) sunshine = model.User.create_user(name='Sunshine', username='******', password=TEST_PASSWORD) sunshine.create_device(name='d5', resource='work', type='phone', dev_id='e') model.UserToGroup.create(user=sunshine, group=user) model.UserToGroup.create(user=sunshine, group=guest) p = model.Publication.create(user=sunshine, topic='Life and Times of Sunshine', description='', publish_group=guest, subscribe_group=guest) model.Message.create(user=sunshine, to_publication=p, subject='First post!') model.Message.create(user=sunshine, to_publication=p, subject='Eating breakfast') model.Message.create(user=sunshine, to_publication=p, subject='Time for a nap') guinness = model.User.create_user(name='Guinness', username='******', password=TEST_PASSWORD) guinness.create_device(name='d7', resource='work', type='phone', dev_id='g') model.UserToGroup.create(user=guinness, group=guest) felix = model.User.create_user(name='Felix', username='******', password=TEST_PASSWORD) felix.create_device(name='d6', resource='work', type='phone', dev_id='f') model.UserToGroup.create(user=felix, group=guest) model.Subscription.create(user=felix, publication=p) model.Message.create(user=felix, to_publication=p, subject='boring...') model.Message.create(user=felix, to_user=sunshine, subject='hi sunshine') model.Message.create(user=felix, to_device=d, subject='hi chloe') model.Message.create(user=felix, to_user=chloe, subject='hi chloe again') ducky = model.User.create_user(name='Ducky', username='******', password=TEST_PASSWORD) ducky.create_device(name='d8', resource='work', type='phone', dev_id='h') model.UserToGroup.create(user=ducky, group=admin) model.UserToGroup.create(user=ducky, group=user) model.UserToGroup.create(user=ducky, group=guest) def setUp(self): self.app = app.test_client() self.populate_database() def request(self, method, url, auth=None, json_data=None, **kwargs): headers = kwargs.get('headers', {}) if auth: # Add the auth header if credentials are specified. headers['Authorization'] = 'Basic %s' % b64encode(bytes(auth[0] + ':' + auth[1], 'utf-8')).decode('ascii') if json: headers['Content-Type'] = 'application/json' kwargs['data'] = json.dumps(obj=json_data) #print(kwargs['data']) kwargs['headers'] = headers #print(kwargs['headers']) return self.app.open(url, method=method, **kwargs)
from flask import Flask from peewee import SqliteDatabase AppInstance = Flask(__name__) DataBase = SqliteDatabase( 'BigHouse.sqlitedb', threadlocals=True ) DataBase.connect() TableList = [] from Services import *
verbose_name='Imagem original', help_text='Imagem original') normalized_image = CharField(unique=True, verbose_name='Imagem normalizada', help_text='Imagem normalizada') thumbnail_image = CharField(unique=True, verbose_name='Imagem reduzida', help_text='Imagem reduzida') def delete_all(self, site_img_path): if self.original_image: original_file = os.path.join(site_img_path, self.original_image) if os.path.exists(original_file): os.remove(original_file) if self.normalized_image: normalized_file = os.path.join(site_img_path, self.normalized_image) if os.path.exists(normalized_file): os.remove(normalized_file) if self.thumbnail_image: thumbnail_file = os.path.join(site_img_path, self.thumbnail_image) if os.path.exists(thumbnail_file): os.remove(thumbnail_file) super(Picture, self).delete_instance() def save(self, force_insert=False, only=None): super(Picture, self).save(force_insert, only) # Connect to db and create tables db.connect() db.create_tables([User, Site, Portfolio, Picture], safe=True)
class TestModel(unittest.TestCase): def setUp(self): self.db = SqliteDatabase('peewee.db') self.db.connect() self.db.create_tables([Device, Group, User, UserToGroup, Publication], safe=True) Device.delete().execute() Group.delete().execute() User.delete().execute() UserToGroup.delete().execute() Publication.delete().execute() self.user0 = User.create_user(name='user0name', username='******', password='******') self.user0.create_device(name='device0name', resource='device0resource', type='device0type', dev_id='device0id', reg_id='device0regid') self.user1 = User.create_user(name='user1name', username='******', password='******') self.user1.create_device(name='device1name', resource='device1resource', type='device1type', dev_id='device1id') self.group0 = Group.create(name='group0name', description='group0description', owner=self.user0) self.group0.add_user(user=self.user0) self.group0.add_user(user=self.user1) self.group1 = Group.create(name='group1name', description='group1description', owner=self.user0) self.group1.add_user(user=self.user0) self.group2 = Group.create(name='group2name', description='group2description', owner=self.user1) self.group2.add_user(user=self.user1) self.pub0 = Publication.create(user=self.user0, topic='pub0topic', description='pub0description', publish_group=self.group1, subscribe_group=self.group0) def tearDown(self): self.db.close() self.db = None def test_group_get(self): os = Group.select() self.assertEqual(len(os), 3) o = Group.select().where(Group.name == 'group0name').get() self.assertEqual(o, self.group0) o = Group.select().where(Group.id == self.group0.id).get() self.assertEqual(o, self.group0) self.assertTrue(o.owner_id) def test_group_memberships(self): self.assertTrue(self.group0.is_member(self.user0)) self.assertTrue(self.group0.is_member(self.user1)) self.assertTrue(self.group1.is_member(self.user0)) self.assertFalse(self.group1.is_member(self.user1)) self.assertFalse(self.group2.is_member(self.user0)) self.assertTrue(self.group2.is_member(self.user1)) def test_get_reg_ids_by_user_id(self): user_id = self.user0.id devices = Device.select(Device, User).join(User).where(User.id == user_id) self.assertEqual(len(devices), 1) reg_ids = [d.reg_id for d in devices] self.assertEqual(reg_ids, ['device0regid'])
class Pomito(object): """Controls the application lifetime. Responsibilities: - Read and initialize the configuration - Choose the run mode - Handover execution to UI plugin """ def __init__(self, config=None, database=None, message_dispatcher=None): """Create a Pomito object. Arguments: config Configuration Path to the configuration file database peewee.SqliteDatabase database to use for tasks etc. message_dispatcher MessageDispatcher message dispatcher instance """ from pomito import pomodoro self._config = config self._database = database self._message_dispatcher = message_dispatcher self._threads = {} self._hooks = [] if self._message_dispatcher is None: self._message_dispatcher = MessageDispatcher() if self._config is None: self._config_file = os.path.join(CONFIG_DIR, "config.ini") self._config = Configuration(self._config_file) self._config.load() # Pomodoro service instance. Order of initializations are important self.pomodoro_service = pomodoro.Pomodoro(self) # Default plugins pomito.plugins.initialize(self.pomodoro_service) self.ui_plugin = pomito.plugins.get_plugin(self._config.ui_plugin) self.task_plugin = pomito.plugins.get_plugin(self._config.task_plugin) # Add the plugins to threads list self._threads['task_plugin'] = threading.Thread(target=self.task_plugin) # Default hooks from pomito.hooks import activity self._hooks.append(activity.ActivityHook(self.pomodoro_service)) return def initialize(self): """Initialize configuration, database and starts worker threads.""" os.makedirs(DATA_DIR, exist_ok=True) database_path = os.path.join(DATA_DIR, "pomito.db") if self._database is None: self._database = SqliteDatabase(None) self._database.init(database_path) self._database.connect() # Initialize the plugins self.ui_plugin.initialize() self.task_plugin.initialize() # Initialize the hooks for hook in self._hooks: hook.initialize() return def run(self): """Start the application.""" if not self._validate_state(): logger.critical("Pomito.Run: Invalid state. Exiting.") return self.initialize() self._message_dispatcher.start() self.ui_plugin.run() self.exit() def exit(self): """Clean up and save any configuration data. Prepare for exiting the application.""" if self._message_dispatcher.is_alive(): self._message_dispatcher.stop() self._message_dispatcher.join() for hook in self._hooks: hook.close() if self._database is not None: self._database.close() def get_db(self): """Get the database object. Returns: database peewee.SqliteDatabase object """ return self._database def get_configuration(self): return self._config def queue_signal(self, message): self._message_dispatcher.queue_message(message) def _validate_state(self): """Validates configuration, plugins.""" import pomito.plugins _retval = True if not issubclass(type(self.ui_plugin), pomito.plugins.ui.UIPlugin): logger.error("Invalid UIPlugin object = {0}".format(self.ui_plugin)) _retval = False if not issubclass(type(self.task_plugin), pomito.plugins.task.TaskPlugin): logger.error("Invalid TaskPlugin object = {0}".format(self.task_plugin)) _retval = False return _retval
from __future__ import division from __future__ import print_function import os from path import path from peewee import Model from peewee import SqliteDatabase from peewee import BlobField from peewee import BooleanField from peewee import CharField from peewee import DateField from peewee import ForeignKeyField from peewee import IntegerField # Initialisation de la base de données bd = SqliteDatabase('cache/sql/archeo-lex.sqlite') bd.connect() ## Définition des classes représentant les articles, textes et leur versionnement # Classe représentant une livraison d’une base class Livraison(Model): class Meta: database = bd date = DateTimeField(primary_key=True) # date de la livraison type = CharField(max_length=9) # 'fondation' ou 'miseajour' (fondation = dump complet, miseajour = dump incrémental) base = CharField(max_length=4) # 'LEGI', etc.
class Database(object): """ Simple database layer that provides channel specific peewee models """ def __init__(self, settings): self.settings = settings self.db = None self.debug = False def run_migrations(self): """ Run any migrations not previously executed :return: """ db = self._get_db() class DBState(Model): migration = CharField(unique=True) class Meta: database = db if not DBState.table_exists(): DBState.create_table() migration_modules = self._find_migrations() if self.debug: print("Migration modules: " + ", ".join(migration_modules)) for module_name in migration_modules: module = importlib.import_module(module_name) if self.debug: print("Processing migration module " + module_name) for key in module.__dict__: if key == "Migration": if self.debug: print("Skipping " + key) continue if DBState.filter(migration=key).exists(): if self.debug: print("Migration " + key + " already run") continue item = module.__dict__[key] if inspect.isclass(item) and issubclass(item, Migration): if self.debug: print("Running migration " + key) instance = item() instance.up(self, self.settings) DBState.create(migration=key) else: if self.debug: print("Skipping " + key) def _find_migrations(self): """ Find any and all database migrations :return: """ migrations_path = os.path.realpath(os.path.join( get_base_path(), "db_migrations" )) files = sorted(os.listdir(migrations_path)) modules = [ "db_migrations." + filename[:-3] for filename in files if filename[-3:] == ".py" and filename != "__init__.py" ] return modules def get_models(self, channel): """ Get channel specific data models :param channel: Name of the channel :return: Dict with models """ raw_channel = channel channel = self._clean_channel(channel) db = self._get_db() settings = self.settings class Regulars(Model): nick = CharField(unique=True) class Meta: database = db db_table = "regulars_{channel}".format(channel=channel) class Commands(Model): command = CharField(unique=True) flags = TextField() user_level = CharField() code = TextField() _flag_data = None class Meta: database = db db_table = "commands_{channel}".format(channel=channel) class Data(Model): key = CharField(unique=True) value = TextField() class Meta: database = db db_table = "data_{channel}".format(channel=channel) class Blacklist(Model): match = CharField(unique=True) banTime = CharField() class Meta: database = db db_table = "blacklist_{channel}".format(channel=channel) class Whitelist(Model): match = CharField(unique=True) class Meta: database = db db_table = "whitelist_{channel}".format(channel=channel) class Quotes(Model): quote = TextField(unique=True) year = IntegerField() month = IntegerField() day = IntegerField() class Meta: database = db db_table = "quotes_{channel}".format(channel=channel) @staticmethod def get_random_quote(): """ Get a random quote from the DB :return: Quote ID and text, or None, None """ quote = Quotes.select().order_by(fn.Random()).limit(1).first() if quote: if settings.QUOTE_AUTO_SUFFIX: quoteText = quote.quote + Quotes._get_quote_suffix( quote ) else: quoteText = quote.quote return quote.id, quoteText else: return None, None @staticmethod def _get_quote_suffix(quote): return settings.QUOTE_AUTO_SUFFIX_TEMPLATE.format( streamer=settings.CHANNEL_LIST[raw_channel], year=quote.year, month=quote.month, day=quote.day ) model_map = { "regulars": Regulars, "commands": Commands, "data": Data, "blacklist": Blacklist, "whitelist": Whitelist, "quotes": Quotes } for key in model_map: model = model_map[key] if not model.table_exists(): model.create_table() return model_map def _get_db(self): """ Get a database connection, initialize it if not done so yet :return: SqliteDatabase instance """ if not self.db: self.db = SqliteDatabase(self.settings.DATABASE_PATH) self.db.connect() return self.db def _clean_channel(self, channel): """ Clean a channel name for use in table names :param channel: The channel name :return: A safe name """ return channel.replace("#", "_")
from playhouse.migrate import SqliteMigrator from playhouse.migrate import migrate as run_migrate from tmc.errors import NoCourseSelected, NoExerciseSelected target_file = os.environ.get("TMC_DATABASEFILE", os.path.join(os.path.expanduser("~"), ".config", "tmc.db")) # SqliteDatabase will fail if the parent directory isn't there. if not os.path.isdir(os.path.dirname(target_file)): os.mkdir(os.path.dirname(target_file), 0o700) sqlite = SqliteDatabase(target_file) sqlite.connect() class BaseModel(Model): class Meta: database = sqlite class SchemaVersion(BaseModel): version = IntegerField() class Course(BaseModel): tid = IntegerField(unique=True) name = CharField()
pw = SqliteDatabase('peewee-simple.db') class Book(Model): title = CharField(null=True, unique=True) year_published = IntegerField() class Meta: database = pw class BookResource(ModelResource): class Meta: name = 'book' model = Book class Schema: year_published = fields.Integer(minimum=1400) api = Api(app, default_manager=PeeweeManager) api.add_resource(BookResource) if not isfile('peewee-simple.db'): pw.connect() pw.create_tables([Book]) if __name__ == '__main__': app.run()
import os from peewee import SqliteDatabase, Model, PrimaryKeyField, CharField if not os.path.exists('./.data'): os.mkdir('./.data') _email_db = SqliteDatabase('./.data/email.db') class Address(Model): id_ = PrimaryKeyField() email = CharField(unique=True) password = CharField() forward_to = CharField() class Meta: database = _email_db _email_db.connect() Address.create_table(True)
# -*- coding:utf-8 -*- import sqlite3 from peewee import BaseModel, CharField, Model, SqliteDatabase try: import config # TODO: Fix relative import config is in ../config/config.py # and/or move config file DATABASE_LOCATION = config.DATABASE_LOCATION except ImportError: DATABASE_LOCATION = 'lite.db' database = SqliteDatabase(DATABASE_LOCATION) database.connect() class BaseModel(Model): '''Base class for the models''' class Meta: database = database class Tweet(BaseModel): '''Model for Tweet table, stores all the information that is gathered using the module Pattern's search function''' # TODO: Add primary key and index(es) # TODO: Move model(s) to separate file profile = CharField() language = CharField()
- project - status - position - time - Projects: - id - name - Statuses: - id - name """ tasks_db = SqliteDatabase('tasks.db') #DEFAULTSTATUS = 'new' #DEFAULTPROJECT = 'unknown' tasks_db.connect() class CustomModel(Model): class Meta: database = tasks_db class Projects(CustomModel): name = CharField() class Statuses(CustomModel): name = CharField() try:
start_time = time() movie.delete() session.commit() elixir_delete_time = (time()-start_time)*1000 print Movie.query.all() #inheritance Actor(name="boris", a="foo a") Director(name="dir", d="dddd") session.commit() print Person.query.all() # Peewee from peewee import SqliteDatabase, Model, CharField, IntegerField, TextField db_engine = SqliteDatabase("peewee.sqldb") db_engine.connect() class Movie(Model): title = CharField() year = IntegerField() description = TextField() def __repr__(self): return '<Movie "%s" (%d)>' % (self.title, self.year) Movie._meta.database = db_engine Movie.drop_table(fail_silently=True) Movie.create_table() start_time = time() movie = Movie(title=u"Blade Runner", year=1982) movie.save() peewee_create_time = (time()-start_time)*1000
#!/sur/bin/env python # -*- coding: utf8 -*- # vim: ai ts=4 sts=4 et sw=4 nu from __future__ import ( unicode_literals, absolute_import, division, print_function) from peewee import (CharField, DateTimeField, SqliteDatabase, Model, __version__) print("Peewee version : " + __version__) DB_FILE = "database.db" dbh = SqliteDatabase(DB_FILE) dbh.connect() class BaseModel(Model): class Meta: database = dbh @classmethod def all(cls): return list(cls.select()) def get_or_none(self, obj): try: return obj.get() except Exception as e: print("get_or_none : ", e)