Skip to main content

Peewee-ext

PyPI PyPI - Downloads PyPI - Python Version PyPI - License

简介

peewee-ext 对 peewee 进行了部分扩展,在其基础上进行了增强

安装

pip install peewee-ext

使用方式

1、连接方式的增强

db_config = {
    'scheme': 'mysql',    # 增加数据库类型
    'host': '127.0.0.1',
    'port': 3306,
    'user': 'root',
    'password': '123456',
    'database': 'data'
}

# 用peewee_ext.connect 替换peewee的connect
# from playhouse.db_url import connect
from peewee_ext import connect


# 可以传入关键字参数,当然第一个位置参数也支持url地址链接
db = connect(**db_config)

2、打印sql执行时间

# from peewee import MySQLDatabase 
from peewee_ext import TimerMySQLDatabase


db_config = {
    'host': '127.0.0.1',
    'port': 3306,
    'user': 'root',
    'password': '123456',
    'database': 'data'
}

# 打印sql执行时间
# db = MySQLDatabase(**db_config)
db = TimerMySQLDatabase(**db_config)

# 对应connect参数 :
# 'scheme': 'mysql+timer'

3、Model转dict字典对象

from peewee_ext import DictModel, to_dict, to_data

# 用 DictModel 替换 Model
# class BaseModel(Model): 
class BaseModel(DictModel):
    class Meta:
        database = db

3.1、Model对象调用方法转dict

model = BaseModel()
model.to_data()
model.to_dict()

3.2、使用装饰器转dict

from peewee_ext import to_dict, to_data

# 装饰器将返回值是DictModel对象转换为dict,其他类型原样返回
@to_dict
def get_row():
    """
    可以返回 DictModel 或 List[DictModel]
    """
    return BaseModel.select().first()


# 可选参数参考:playhouse.shortcuts.model_to_dict
@to_data
def get_row():
    """
    可以返回 DictModel 或 List[DictModel]
    """
    return BaseModel.select().first()

说明

增加一个 DictModel 增加两个方法

from peewee import Model

class DictModel(Model):

    def to_data(self, recurse=True, backrefs=False,
                only=None, exclude=None,
                seen=None, extra_attrs=None,
                fields_from_query=None, max_depth=None,
                manytomany=False):
        """model to dict"""

    def to_dict(self):
        """model to dict"""


    def __str__(self):
        """friendly for human"""

增加两个装饰器

def to_dict(func):
    """
    model to dict , shallow convert
    """

# 返回嵌套字典
def to_data(func=None, recurse=True, backrefs=False,
            only=None, exclude=None,
            seen=None, extra_attrs=None,
            fields_from_query=None, max_depth=1,
            manytomany=False):
    """model to dict , deep convert"""

增加执行时间打印

from peewee import MySQLDatabase

# 已注册scheme: 'mysql+timer'
class TimerMySQLDatabase(MySQLDatabase):
    """"""

Release files for peewee-ext 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for peewee-ext 0.0.2
File Size Uploaded
peewee-ext-0.0.2.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for peewee-ext 0.0.2
File Interpreter ABI Platform
peewee_ext-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / peewee-ext-0.0.2.tar.gz

Download URL peewee-ext-0.0.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
591193bd9f8caf6587206d4791867e99bd4fb95a105aa10998711af1e9da055b
BLAKE2b-256 checksum
How to use checksums
182d193cd4350bb56d76ef677bc00d06e549269495120e44131524e1728f3332
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.0

Release files / peewee_ext-0.0.2-py3-none-any.whl

Download URL peewee_ext-0.0.2-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
87f38158700fe1af7ecd62133e903a10ac9bcb101a436fb0d5576f1ea730514c
BLAKE2b-256 checksum
How to use checksums
49474d907dfbd8170597cd79e2923e77ee046853f7c8af22dc83a360882962bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.0

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page