class DataBaseService(Service): """Manage all services""" def __init__(self, env, host, port, user, passwd, db): super(DataBaseService, self).__init__(env) self._db_proxy = Proxy() self._conn_info = dict(host=host, port=port, \ user=user, passwd=passwd, \ db=db) def on_active(self): super(DataBaseService, self).on_active() conn_info = self._conn_info.copy() db_name = conn_info.pop('db') database = PooledMySQLDatabaseWithReconnection( db_name, max_connections=DB_CONNECTION_MAX_NUM, stale_timeout=300, threadlocals=True, **conn_info ) self._db_proxy.initialize( database ) self._db_proxy.connect() def get_db(self): return self._db_proxy
class RepoConfig(object): def __init__(self): self._database_path = None self._database = None self._database_proxy = None def set_database_path(self, path): self._database_path = path self._database = SqliteDatabase(self._database_path) self._database_proxy = Proxy() self._database_proxy.initialize(self._database) @property def database_path(self): return self._database_path @property def database(self): return self._database @property def database_proxy(self): return self._database_proxy # singleton instance = None @classmethod def get(cls): if cls.instance is None: cls.instance = cls() return cls.instance
def test_binary_type_info(self): db_proxy = Proxy() class A(Model): blob_field = BlobField() class Meta: database = db_proxy self.assertTrue(A.blob_field._constructor is binary_construct) db = SqliteDatabase(':memory:') db_proxy.initialize(db) self.assertTrue(A.blob_field._constructor is sqlite3.Binary)
def test_binary_type_info(self): db_proxy = Proxy() class A(Model): blob_field = BlobField() class Meta: database = db_proxy self.assertTrue(A.blob_field._constructor is binary_construct) db = SqliteDatabase(':memory:') db_proxy.initialize(db) self.assertTrue(A.blob_field._constructor is sqlite3.Binary)
class PeeweePlugin(object): """ Integrate peewee to bottle. """ name = 'peewee' api = 2 default_connection = 'sqlite:///db.sqlite' def __init__(self, connection=None): self.database = None self.connection = connection or self.default_connection self.proxy = Proxy() self.serializer = Serializer() def setup(self, app): """ Initialize the application. """ app.config.setdefault('PEEWEE_CONNECTION', self.connection) self.connection = app.config.get('PEEWEE_CONNECTION') self.database = connect(self.connection) self.proxy.initialize(self.database) def apply(self, callback, route): def wrapper(*args, **kwargs): if self.connection.startswith('sqlite'): return callback(*args, **kwargs) try: self.database.connect() except Exception as e: pass # Error can now be leaked to Bottle, e.g.: as `peewee.OperationalError` try: with self.database.transaction(): response = callback(*args, **kwargs) except PeeweeException: self.database.rollback() raise finally: self.database.commit() if not self.database.is_closed(): self.database.close() return response return wrapper def to_dict(self, obj, **kwargs): return self.serializer.serialize_object(obj, **kwargs)
def prepare_database_with_table(name: str, rows: list): from peewee import IntegerField, Proxy, CharField, Model from playhouse.sqlite_ext import CSqliteExtDatabase db = Proxy() db.initialize(CSqliteExtDatabase(':memory:', bloomfilter=True)) NameModel = type( name, (Model, ), { 'id_': IntegerField(primary_key=True, column_name='id'), 'name': CharField(column_name='name') }) table: Model = NameModel() table.bind(db) db.create_tables([NameModel]) for row in rows: table.insert(row).execute() return db
class PeeweePlugin(object): """ Integrate peewee to bottle. """ name = 'peewee' api = 2 default_connection = 'sqlite:///db.sqlite' def __init__(self, connection=None): self.database = None self.connection = connection or self.default_connection self.proxy = Proxy() self.serializer = Serializer() def setup(self, app): """ Initialize the application. """ app.config.setdefault('PEEWEE_CONNECTION', self.connection) self.connection = app.config.get('PEEWEE_CONNECTION') self.database = connect(self.connection) self.proxy.initialize(self.database) def apply(self, callback, route): def wrapper(*args, **kwargs): if self.connection.startswith('sqlite'): return callback(*args, **kwargs) self.database.connect() try: with self.database.transaction(): response = callback(*args, **kwargs) except PeeweeException: self.database.rollback() raise finally: self.database.commit() if not self.database.is_closed(): self.database.close() return response return wrapper def to_dict(self, obj, **kwargs): return self.serializer.serialize_object(obj, **kwargs)
.join(GroupToCapability).join(Group) .join(UserToGroup).join(User) .where(User.id == self.id)) def can(self, domain, action): """Can perform `action` on the given `domain`.""" for cap in self.capabilities: if(cap.match(domain, action)): return True return False class GroupToCapability(BaseModel): group = ForeignKeyField(Group, on_delete='CASCADE') capability = ForeignKeyField(Capability, on_delete='CASCADE') class Meta: primary_key = CompositeKey('group', 'capability') class UserToGroup(BaseModel): user = ForeignKeyField(User, on_delete='CASCADE') group = ForeignKeyField(Group, on_delete='CASCADE') class Meta: primary_key = CompositeKey('user', 'group') GroupToCapabilityProxy.initialize(GroupToCapability) UserToGroupProxy.initialize(UserToGroup)
from peewee import (CharField, Proxy, ForeignKeyField, Model, PrimaryKeyField, SqliteDatabase, TextField) db = Proxy() database = SqliteDatabase("graphene-data-playground.db") db.initialize(database) class DataPlaygroundModel(Model): class Meta: database = db class User(DataPlaygroundModel): id = PrimaryKeyField() username = CharField() class Follow(DataPlaygroundModel): follower = ForeignKeyField(User, related_name="follower_set") followee = ForeignKeyField(User, related_name="followee_set") class Post(DataPlaygroundModel): id = PrimaryKeyField() author = ForeignKeyField(User, related_name="posts") content = TextField() class Like(DataPlaygroundModel): user = ForeignKeyField(User, related_name="likes")
database = tax_db db_table = "taxon" class TaxName(Model): ''' classdocs ''' taxon = ForeignKeyField(Tax, related_name="names", db_column="taxon_id") name = CharField(max_length=255) name_class = CharField(max_length=32) def __str__(self): return "TaxName(" + "".join( [x + "=" + str(y) + "," for x, y in self.__data__.items()]) + ")" class Meta: database = tax_db db_table = "taxon_name" primary_key = CompositeKey('taxon_id', 'name', 'name_class') if __name__ == '__main__': tax_db.initialize(MySQLDatabase('bioseqdb', user='******', passwd="mito")) tax = Tax.getTax(1872703) for x in tax.names: print x for t in Tax.parents(tax): print t
redis_addr = "10.152.183.234:6379" ray.init(redis_address=redis_addr) config = config.LunarLander() config.experience_threads = 1 config.num_epochs = 10 config.episode_batch_size = 40 steps = 0 db = PostgresqlDatabase('testpython', user='******', password='******', host='localhost', port=5432) database_proxy.initialize(db) db.create_tables([PolicyStore]) timings = TimingReport() total_time = util.SimpleInstrument() total_time.start() main() total_time.end() total_time_report = util.SimpleReport() total_time_report.append(total_time) print( f'local {local_mode} threads {config.experience_threads} epochs {config.num_epochs} episodes {config.episode_batch_size} steps {steps}'
class Peewee(object): def __init__(self, app=None): """ Initialize the plugin. """ self.app = app self.database = Proxy() if app is not None: self.init_app(app) def init_app(self, app, database=None): """ Initialize an application. """ if not app: raise RuntimeError('Invalid application.') self.app = app if not hasattr(app, 'extensions'): app.extensions = {} app.extensions['peewee'] = self app.config.setdefault('PEEWEE_CONNECTION_PARAMS', {}) app.config.setdefault('PEEWEE_DATABASE_URI', 'sqlite:///peewee.sqlite') app.config.setdefault('PEEWEE_MANUAL', False) app.config.setdefault('PEEWEE_MIGRATE_DIR', 'migrations') app.config.setdefault('PEEWEE_MIGRATE_TABLE', 'migratehistory') app.config.setdefault('PEEWEE_MODELS_CLASS', Model) app.config.setdefault('PEEWEE_MODELS_IGNORE', []) app.config.setdefault('PEEWEE_MODELS_MODULE', '') app.config.setdefault('PEEWEE_READ_SLAVES', '') app.config.setdefault('PEEWEE_USE_READ_SLAVES', True) # Initialize database params = app.config['PEEWEE_CONNECTION_PARAMS'] database = database or app.config.get('PEEWEE_DATABASE_URI') if not database: raise RuntimeError('Invalid database.') database = get_database(database, **params) # Configure read slaves slaves = app.config['PEEWEE_READ_SLAVES'] if isinstance(slaves, str): slaves = slaves.split(',') self.slaves = [ get_database(slave, **params) for slave in slaves if slave ] self.database.initialize(database) if self.database.database == ':memory:': app.config['PEEWEE_MANUAL'] = True # TODO: Replace this code, when a solution will be found #if not app.config['PEEWEE_MANUAL']: # app.before_request(self.connect) # app.teardown_request(self.close) def connect(self): """ Initialize connection to database. """ LOGGER.info('Connecting [%s]', os.getpid()) return self.database.connect() def close(self, response): """ Close connection to database. """ LOGGER.info('Closing [%s]', os.getpid()) if not self.database.is_closed(): self.database.close() return response @cached_property def Model(self): """ Bind model to self database. """ Model_ = self.app.config['PEEWEE_MODELS_CLASS'] meta_params = {'database': self.database} if self.slaves and self.app.config['PEEWEE_USE_READ_SLAVES']: meta_params['read_slaves'] = self.slaves Meta = type('Meta', (), meta_params) return type('Model', (Model_, ), {'Meta': Meta}) @property def models(self): """ Return self.application models.py. """ Model_ = self.app.config['PEEWEE_MODELS_CLASS'] ignore = self.app.config['PEEWEE_MODELS_IGNORE'] models = [] if Model_ is not Model: try: mod = import_module(self.app.config['PEEWEE_MODELS_MODULE']) for model in dir(mod): models = getattr(mod, model) if not isinstance(model, PeeweeModel): continue models.append(models) except ImportError: return models elif isinstance(Model_, BaseSignalModel): models = BaseSignalModel.models return [m for m in models if m._meta.name not in ignore] def cmd_create(self, name, auto=False): """ Create a new migration. """ LOGGER.setLevel('INFO') LOGGER.propagate = 0 router = Router(self.database, migrate_dir=self.app.config['PEEWEE_MIGRATE_DIR'], migrate_table=self.app.config['PEEWEE_MIGRATE_TABLE']) if auto: auto = self.models router.create(name, auto=auto) def cmd_migrate(self, name=None, fake=False): """ Run migrations. """ LOGGER.setLevel('INFO') LOGGER.propagate = 0 router = Router(self.database, migrate_dir=self.app.config['PEEWEE_MIGRATE_DIR'], migrate_table=self.app.config['PEEWEE_MIGRATE_TABLE']) migrations = router.run(name, fake=fake) if migrations: LOGGER.warn('Migrations are completed: %s' % ', '.join(migrations)) def cmd_rollback(self, name): """ Rollback migrations. """ LOGGER.setLevel('INFO') LOGGER.propagate = 0 router = Router(self.database, migrate_dir=self.app.config['PEEWEE_MIGRATE_DIR'], migrate_table=self.app.config['PEEWEE_MIGRATE_TABLE']) router.rollback(name) def cmd_list(self): """ List migrations. """ LOGGER.setLevel('DEBUG') LOGGER.propagate = 0 router = Router(self.database, migrate_dir=self.app.config['PEEWEE_MIGRATE_DIR'], migrate_table=self.app.config['PEEWEE_MIGRATE_TABLE']) LOGGER.info('Migrations are done:') LOGGER.info('\n'.join(router.done)) LOGGER.info('') LOGGER.info('Migrations are undone:') LOGGER.info('\n'.join(router.diff)) def cmd_merge(self): """ Merge migrations. """ LOGGER.setLevel('DEBUG') LOGGER.propagate = 0 router = Router(self.database, migrate_dir=self.app.config['PEEWEE_MIGRATE_DIR'], migrate_table=self.app.config['PEEWEE_MIGRATE_TABLE']) router.merge() @cached_property def manager(self): """ Integration with the Sanic-Script package. """ from sanic_script import Manager, Command manager = Manager(usage="Migrate database.") manager.add_command('create', Command(self.cmd_create)) manager.add_command('migrate', Command(self.cmd_migrate)) manager.add_command('rollback', Command(self.cmd_rollback)) manager.add_command('list', Command(self.cmd_list)) manager.add_command('merge', Command(self.cmd_merge)) return manager @cached_property def cli(self): import click @click.group() def cli(): pass @cli.command() @click.argument('name') @click.option('--auto', is_flag=True) def create(name, auto=False): """ Create a new migration. """ return self.cmd_create(name, auto) @cli.command() @click.argument('name', default=None, required=False) @click.option('--fake', is_flag=True) def migrate(name, fake=False): """ Run migrations. """ return self.cmd_migrate(name, fake) @cli.command() @click.argument('name') def rollback(name): """ Rollback migrations. """ return self.cmd_rollback(name) @cli.command() def list(): """ List migrations. """ return self.cmd_list() return cli
from peewee import Proxy from playhouse.db_url import connect from playhouse.migrate import PostgresqlMigrator import appglobals from models import * from models import Bot, User, Suggestion from models.keywordmodel import Keyword connection = connect(appglobals.DATABASE_PATH) # connection = connect("sqlite:///:memory:") connection.autorollback = True database = Proxy() database.initialize(connection) postgresql_migrator = PostgresqlMigrator(database) create_order = [ Country, User, APIAccess, Category, Revision, Bot, Channel, Favorite, Group, Keyword, Notifications,
from telegram import Bot from queue import Queue import os from playhouse.db_url import connect import dotenv dotenv.load_dotenv() def config(key: str, default=None): return os.environ.get(key) or os.environ.get(key.upper()) or default # db = SqliteDatabase(config('db', 'database.sqlite')) db = Proxy() db.initialize(connect(config('DATABASE_URL', 'sqlite:///database.sqlite'))) class CustomDispatcher(Dispatcher): def process_update(self, update): with db.atomic() as txn: super().process_update(update) db.close() updater = Updater(dispatcher=CustomDispatcher(Bot(config('token')), Queue(), job_queue=JobQueue()), workers=None) bot = updater.bot
return self.full_name class UserRoles(db.Model): user = ForeignKeyField(User, index=True, db_column='user_id') role = ForeignKeyField(Role, index=True, db_column='role_id') name = property(lambda self: self.role.name) description = property(lambda self: self.role.description) class Meta: db_table = "user_role" table_alias = 'ur' primary_key = CompositeKey('user', 'role') UserRolesProxy.initialize(UserRoles) class ApproverCompanies(db.Model): user = ForeignKeyField(User, index=True, db_column='user_id') company = ForeignKeyField(Company, index=True, db_column='company_id') name = property(lambda self: self.company.name) code = property(lambda self: self.company.code) class Meta: db_table = "approver_company" table_alias = "ac" primary_key = CompositeKey('user', 'company') ApproverCompaniesProxy.initialize(ApproverCompanies)
# we use the Proxy helper. # Proxy objects act as a placeholder, # and then at run-time you can swap it out for a different object. db_proxy = Proxy() # Different init options for heroku and local. # As mentioned above, we need "HEROKU" env variable in heroku if "HEROKU" in os.environ: ur.uses_netloc.append("postgres") url = ur.urlparse(os.environ["DATABASE_URL"]) db = PostgresqlDatabase(database=url.path[1:], user=url.username, password=url.password, host=url.hostname, port=url.port) db_proxy.initialize(db) else: # db = PostgresqlDatabase('my_postgres_db', user='******', password='******', host='localhost') db = SqliteDatabase("userdatabase.db") db_proxy.initialize(db) ## User mixin provides some useful stuff class User(UserMixin, Model): """User model object""" ##peewee automatically adds autoinc. id column. username = CharField(unique=True) password = CharField(max_length=50) email = CharField(unique=True) ## todo: answer why timestampfield but not datetimefield? ##also notice it is not now(), but just now
import urllib.parse as urlparse # import psycopg2 from peewee import SqliteDatabase, PostgresqlDatabase, Proxy import os database = Proxy() if os.environ.get('DATABASE_URL'): urlparse.uses_netloc.append('postgres') url = urlparse.urlparse(os.environ["DATABASE_URL"]) db = PostgresqlDatabase(database=url.path[1:], user=url.username, password=url.password, host=url.hostname, port=url.port) database.initialize(db) else: # SQLite database using WAL journal mode and 64MB cache. db = SqliteDatabase(database='resources/bot_persistence.db', pragmas={ 'journal_mode': 'wal', 'cache_size': -1024 * 64 }) database.initialize(db) # logger = logging.getLogger('peewee') # logger.addHandler(logging.StreamHandler()) # logger.setLevel(logging.DEBUG)
import asyncio import os from pathlib import Path from decouple import config from peewee import Proxy from playhouse.db_url import connect from telegram.ext import JobQueue ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) ACCOUNTS_DIR = Path(ROOT_DIR) / "accounts" DATABASE_PATH = config('DATABASE_URL') _auto_typed_db = connect(DATABASE_PATH) _auto_typed_db.autorollback = True db = Proxy() db.initialize(_auto_typed_db) loop = asyncio.get_event_loop() """ Global singleton ptb job_queue as I'm too lazy to rewrite everything to say `use_context=True` and propagating the `pass_job_queue` flag across all handlers would be an even bigger nightmare. At some point this is going to be replaced with `CallbackContext`, but for now we're gonna live with a global. """ job_queue: JobQueue = None
def peewee_count_queries(*args, **kwargs): """ Used to count and display number of queries """ try: if not hasattr(g, 'pqc'): g.pqc = 0 g.pqc += 1 except RuntimeError: pass return dex(*args, **kwargs) dbm.execute = peewee_count_queries db = Proxy() db.initialize(dbm) class BaseModel(Model): class Meta: database = db class User(BaseModel): uid = CharField(primary_key=True, max_length=40) crypto = IntegerField() # Password hash algo, 1 = bcrypt. email = CharField(null=True) joindate = DateTimeField(null=True) name = CharField(null=True, unique=True, max_length=64) password = CharField(null=True)
class AtomProperty(Model): id = AutoField(primary_key=True) atom = ForeignKeyField(PDB, related_name='properties', db_column="atom_id") property = ForeignKeyField(PDB, db_column="property_id") value = DoubleField(null=True) tag = CharField(null=True) class Meta: database = sqldb if __name__ == "__main__": from peewee import MySQLDatabase mysql_db = MySQLDatabase('pdbdb', user="******", password="******") sqldb.initialize(mysql_db) tables = [ PDB, Residue, Atom, ResidueSet, ResidueSetResidue, PDBProperty, ResidueProperty, ResidueSetProperty, ChainProperty, AtomProperty ] # for x in reversed(tables): # x.drop_table() # for x in tables: # x.create_table() # Property.create_table() # qmean_s = ['qr_solvation', 'qr_ss_agreement', 'qr_QMEANDisCo', 'qr_exposed', 'qr_cbeta', 'qr_all_atom', 'qr_QMEAN', # 'qr_acc_agreement', 'qr_dist_const', 'qr_torsion'] # qmean_r = ['q_torsion_zscore', 'q_QMEAN6_zscore', 'q_cbeta_zscore', 'q_interaction_norm', 'q_acc_agreement_zscore', # 'q_QMEAN4_norm', 'q_torsion_norm', 'q_packing_zscore', 'q_interaction_zscore', 'q_QMEAN6_norm', # 'q_QMEAN4_zscore', 'q_residues', 'q_packing_norm', 'q_ss_agreement_norm', 'q_ss_agreement_zscore',