Skip to content

Instantly share code, notes, and snippets.

@albarrentine
Created May 21, 2011 20:20
Show Gist options
  • Save albarrentine/984866 to your computer and use it in GitHub Desktop.
Save albarrentine/984866 to your computer and use it in GitHub Desktop.
SQLAlchemy & pseudo-hash indexing in MySQL
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