Skip to content

Instantly share code, notes, and snippets.

@vincepota-zz
Created July 3, 2018 08:58
Show Gist options
  • Save vincepota-zz/cffc43b297616d346e1999d84b9abdd9 to your computer and use it in GitHub Desktop.
Save vincepota-zz/cffc43b297616d346e1999d84b9abdd9 to your computer and use it in GitHub Desktop.
Get vendor-specific data types in sqlalchemy
import importlib
class VendorType:
"""
Return a list of vendor-specific datatype as a list.
Parameters
----------
vendor :str
One of:
-oracle
-mysql
-postgresql
-sqlite
-mssql
"""
def __init__(self, vendor):
self.vendor = vendor
def type_path(self):
return 'sqlalchemy.dialects.{}'.format(self.vendor)
def get_types(self):
mt = importlib.import_module(self.type_path())
type_list = mt.__all__
# only return uppercase types. This step also removes non-datatypes
# that somehow end up in __all__
return [t for t in type_list if t.isupper()]
# For example :
vt = VendorType('sqlite').get_types()
print(vt)
# ['BLOB', 'BOOLEAN', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'FLOAT', 'INTEGER', 'NUMERIC', 'SMALLINT', 'TEXT', 'TIME', 'TIMESTAMP', 'VARCHAR', 'REAL']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment