Skip to main content

additional column types for SQLAlchemy

Project description

This repo contains a custom column types for SQLAlchemy which I use in different projects.

The code should be compatible with SQLAlchemy 1.2.3 - 1.4.x.

pip install ColumnAlchemy

UTCDateTime

UTCDateTime stores a Python tz-aware datetime.datetime value as UTC datetime in the database (without explicit timezone information). I use this to introduce tz-aware timezones in systems which expect "naive" datetimes in the database.

from schwarz.column_alchemy import UTCDateTime

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

ShiftedDecimal

ShiftedDecimal stores a Decimal as integer in the database (with limited precision). This is especially useful to store decimal values even in sqlite which requires special treatment to store decimals.

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

TODO

IntValuesEnum

TODO

YearMonthColumn

YearMonth is similar to datetime.date but without a day attribute. It can be used to represent a calendar month and provides some convenience methods like first_date_of_month() and last_date_of_month(). A YearMonthColumn stores a YearMonth instance as "YYYY-MM" in the database.

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

Very similar to YearMonthColumn but stores YearMonth(2020, 7) as 202007 (integer).

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

ColumnAlchemy-0.9.2.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

ColumnAlchemy-0.9.2-py2.py3-none-any.whl (28.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ColumnAlchemy-0.9.2.tar.gz.

File metadata

  • Download URL: ColumnAlchemy-0.9.2.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.4

File hashes

Hashes for ColumnAlchemy-0.9.2.tar.gz
Algorithm Hash digest
SHA256 4be35785f38e4400a0e87be525722e2705c50b17f415d1dbcaf81eb5bbea24fc
MD5 bf5be135a664a807663926f46ad6d1ed
BLAKE2b-256 ff38938882d6e951e78e6957a30cad17f98bba6154dfb249d2817ea530ae5559

See more details on using hashes here.

File details

Details for the file ColumnAlchemy-0.9.2-py2.py3-none-any.whl.

File metadata

  • Download URL: ColumnAlchemy-0.9.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.4

File hashes

Hashes for ColumnAlchemy-0.9.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 68c97c365c424ada34922a5ef111df5fd2d79fa4615d9e8b21b629de15b8c480
MD5 724662eea7b39d3543f62246da93f1d1
BLAKE2b-256 f875a7a217b98c8414fba2ab74751ad60dff35a5b2632edfb7a3fe3e1e051efc

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