/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
peewee__examples/SqliteQueueDatabase/db.py
142 строки
4 KB
gil9red
Refactoring
19 сен 2024, 01:20
19 сен 2024, 01:20
57b9738
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" import enum import sys import time from typing import Type, Iterable, Self, Any # pip install peewee from peewee import ( Model, TextField, ForeignKeyField, CharField, ) from playhouse.shortcuts import model_to_dict from playhouse.sqliteq import SqliteQueueDatabase from config import DB_FILE_NAME, DIR sys.path.append(str(DIR.parent)) from shorten import shorten # This working with multithreading # SOURCE: http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#sqliteq db = SqliteQueueDatabase( DB_FILE_NAME, pragmas={ "foreign_keys": 1, "journal_mode": "wal", # WAL-mode "cache_size": -1024 * 64, # 64MB page-cache }, use_gevent=False, # Use the standard library "threading" module. autostart=True, queue_max_size=64, # Max. # of pending writes that can accumulate. results_timeout=5.0, # Max. time to wait for query to be executed. ) class BaseModel(Model): class Meta: database = db def get_new(self) -> Self: return type(self).get(self._pk_expr()) @classmethod def get_first(cls) -> Self: return cls.select().first() @classmethod def get_last(cls) -> Self: return cls.select().order_by(cls.id.desc()).first() @classmethod def get_inherited_models(cls) -> list[Type[Self]]: return sorted(cls.__subclasses__(), key=lambda x: x.__name__) @classmethod def print_count_of_tables(cls): items = [] for sub_cls in cls.get_inherited_models(): name = sub_cls.__name__ count = sub_cls.select().count() items.append(f"{name}: {count}") print(", ".join(items)) @classmethod def count(cls, filters: Iterable = None) -> int: query = cls.select() if filters: query = query.filter(*filters) return query.count() def to_dict(self) -> dict[str, Any]: return model_to_dict(self, recurse=False) def __str__(self): fields = [] for k, field in self._meta.fields.items(): v = getattr(self, k) if isinstance(field, (TextField, CharField)): if isinstance(v, enum.Enum): v = v.value if v: v = repr(shorten(v, length=30)) elif isinstance(field, ForeignKeyField): k = f"{k}_id" if v: v = v.id fields.append(f"{k}={v}") return self.__class__.__name__ + "(" + ", ".join(fields) + ")" class Parameter(BaseModel): name = TextField(primary_key=True) value = TextField() description = TextField(null=True) @classmethod def get_by_name(cls, name: str) -> Self | None: return cls.get_or_none(cls.name == name) @classmethod def add( cls, name: str, value: str, description: str = None, ) -> Self: obj = cls.get_by_name(name) if obj: return obj return cls.create( name=name, value=value, description=description, ) db.connect() db.create_tables(BaseModel.get_inherited_models()) # Задержка в 50мс, чтобы дать время на запуск SqliteQueueDatabase и создание таблиц # Т.к. в SqliteQueueDatabase запросы на чтение выполняются сразу, а на запись попадают в очередь time.sleep(0.050) if __name__ == "__main__": BaseModel.print_count_of_tables()