Skip to main content

peewee-ext is a peewee extension

Project description

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):
    """"""

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

peewee-ext-0.0.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

peewee_ext-0.0.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file peewee-ext-0.0.2.tar.gz.

File metadata

  • Download URL: peewee-ext-0.0.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for peewee-ext-0.0.2.tar.gz
Algorithm Hash digest
SHA256 591193bd9f8caf6587206d4791867e99bd4fb95a105aa10998711af1e9da055b
MD5 ef1f1f2ef2fe6b6320e5524064d22c53
BLAKE2b-256 182d193cd4350bb56d76ef677bc00d06e549269495120e44131524e1728f3332

See more details on using hashes here.

File details

Details for the file peewee_ext-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: peewee_ext-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for peewee_ext-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 87f38158700fe1af7ecd62133e903a10ac9bcb101a436fb0d5576f1ea730514c
MD5 c72c484c35113bd8f8bdf65b90a7383c
BLAKE2b-256 49474d907dfbd8170597cd79e2923e77ee046853f7c8af22dc83a360882962bf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page