Skip to content

Instantly share code, notes, and snippets.

@malexmave
Created March 20, 2014 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malexmave/9660246 to your computer and use it in GitHub Desktop.
Save malexmave/9660246 to your computer and use it in GitHub Desktop.
Dynamic datatype generation
# A few notes:
# data.structure.STRUCT contains the definitions as described in the blog post
# data.structure.SQL_TO_PY_TYPE contains a dictionary mapping SQL to python data types
# data.structure.CamelCase() is a function converting strings_with_underscore to stringsInCamelcase
# The checkRep-Function is later used to verify that the data type is sane
from data import structure
cd = "from copy import deepcopy\n\n"
for tbl in structure.STRUCT:
# Class header
S2P = structure.SQL_TO_PY_TYPE
camelTbl = structure.CamelCase(tbl)
cd += "class %s():\n" % camelTbl
cd += " "*4 + "def __init__(self,dat):\n"
# Add data type checks
for field in structure.STRUCT[tbl]:
fd = structure.STRUCT[tbl][field]
if fd["notNull"]:
cd += " "*8 +"assert type(dat['{0}']) == {1}, '{0} is no {1}'\n" \
.format(field, str(S2P[fd["type"].upper()]))
else:
cd += " "*8 +"assert type(dat['{0}']) in ".format(field) + \
"[{1}, type(None)], '{0} is neither {1} nor None'\n" \
.format(field, str(S2P[fd["type"].upper()]))
# Copy data
cd += " "*8 + "for key in dat:\n"
cd += " "*12 + "setattr(self, '_' + key, deepcopy(dat[key]))\n\n"
# Create equality defitions
cd += " "*4 + "def __eq__(self, other):\n"
cd += " "*8 + "return (isinstance(other, self.__class__) and \\\n"
cd += " "*12 + "self.__dict__ == other.__dict__)\n\n"
cd += " "*4 + "def __neq__(self, other):\n"
cd += " "*8 + "return not self.__eq__(other)\n\n"
# Create checkRep function
cd += " "*4 + "def checkRep(self):\n"
for field in structure.STRUCT[tbl]:
fd = structure.STRUCT[tbl][field]
if fd["notNull"]:
cd += " "*8 +"assert type(self._{0}) == {1}, '{0} is no {1}'\n" \
.format(field, str(S2P[fd["type"].upper()]))
else:
cd += " "*8 +"assert type(self._{0}) in ".format(field) + \
"[{1}, type(None)], '{0} is neither {1} nor None'\n" \
.format(field, str(S2P[fd["type"].upper()]))
# Create getters
for field in structure.STRUCT[tbl]:
camelField = structure.CamelCase(field)
cd += " "*4 + "def get{0}(self):\n".format(camelField)
cd += " "*8 + "return self._{0}\n\n".format(field)
cd += "\n"
# Evaluate String to generate actual datatypes
exec cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment