跳转到主要内容

为SQLAlchemy添加额外的列类型

项目描述

此仓库包含我用于不同项目的自定义列类型。

代码应与SQLAlchemy 1.2.3 - 1.4.x兼容。

pip install ColumnAlchemy

UTCDateTime

UTCDateTime 将Python tz-aware datetime.datetime值存储为数据库中的UTC日期时间(没有显式的时间信息)。我使用它来引入在数据库中期望“天真”日期时间的系统中对tz-aware时区。

from schwarz.column_alchemy import UTCDateTime

class Foo(Base)
    __tablename__ = 'foo'
    id = Column(Integer, autoincrement=True, primary_key=True)
    timestamp = Column(UTCDateTime)

ShiftedDecimal

ShiftedDecimalDecimal作为整数存储在数据库中(具有有限的精度)。这对于在需要特殊处理以存储十进制的sqlite中存储十进制值特别有用。

from decimal import Decimal
from schwarz.column_alchemy import ShiftedDecimal

class Foo(Base)
    __tablename__ = 'foo'
    id = Column(Integer, autoincrement=True, primary_key=True)
    percentage = Column(ShiftedDecimal(4))

foo = Foo(percentage=Decimal('1.2324'))
# stores percentage as 12324 in the database but returns the
# correct Decimal value after loading.

ValuesEnum

待办事项

IntValuesEnum

待办事项

YearMonthColumn

YearMonth类似于datetime.date,但没有day属性。它可以用来表示日历月份,并提供一些方便的方法,如first_date_of_month()last_date_of_month()。一个YearMonthColumnYearMonth实例存储为数据库中的“YYYY-MM”。

from schwarz.column_alchemy import YearMonth, YearMonthColumn

class Foo(Base)
    __tablename__ = 'foo'
    id = Column(Integer, autoincrement=True, primary_key=True)
    month = Column(YearMonthColumn())

foo = Foo(month=YearMonth(2020, 7))
# stores "month" as "2020-07" in the database but returns a
# YearMonth instance after loading.

YearMonthIntColumn

YearMonthColumn非常相似,但将YearMonth(2020, 7)存储为整数202007

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

ColumnAlchemy-0.9.2.tar.gz (23.5 kB 查看散列)

上传时间:

构建分发

ColumnAlchemy-0.9.2-py2.py3-none-any.whl (28.5 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下支持