Created
May 21, 2011 20:20
-
-
Save albarrentine/984866 to your computer and use it in GitHub Desktop.
SQLAlchemy & pseudo-hash indexing in MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import binascii | |
def crc32(val): | |
"""Have to do this with binascii's crc32 which is signed (MySQL's is unsigned)""" | |
return binascii.crc32(val) & 0xffffffff | |
def crc32_of(other_field): | |
"""Context sensitive default for SQLAlchemy inserts | |
Usage (in model definition): | |
foo = Column('foo_col', VARCHAR(50)) | |
foo_crc32 = Column('foo_crc32', Integer, default=crc32_of('foo_col'), onupdate=crc32_of('foo_col')) | |
""" | |
def _crc32(context): | |
return crc32(context.current_parameters[other_field]) | |
return _crc32 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment