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.
This is especially useful to store decimal values even in sqlite which requires special treatment to store decimals.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ColumnAlchemy-0.9.1.tar.gz
.
File metadata
- Download URL: ColumnAlchemy-0.9.1.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f0ebc26dc86022ff811ee3fdf3848932fb3599552c7329a4ffb699833d42b3a |
|
MD5 | 1f2ff89c7354317bd929e1b2c916ac26 |
|
BLAKE2b-256 | 2a62f63b8a958dd15f2637eb03a52a954a33be18e11ba366389e88f17f4b9401 |
File details
Details for the file ColumnAlchemy-0.9.1-py2.py3-none-any.whl
.
File metadata
- Download URL: ColumnAlchemy-0.9.1-py2.py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c817a75f2d1cc5127ec847944a66ca9800e0d3ede83be59f3b4a17bb38bec24f |
|
MD5 | 2a990a329de53c10cefe5a1be512a7e0 |
|
BLAKE2b-256 | 11f9b01d7de1b0b524dfff5e9e629b7c4db95b231110ec13bc9e743605c821b1 |