Last active
June 4, 2020 16:44
-
-
Save KennethNielsen/63e729627db62b7bfcce9672dddd5c48 to your computer and use it in GitHub Desktop.
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
def __repr__(self): | |
kwarg = [] | |
if self.key != self.name: | |
kwarg.append("key") | |
if self.primary_key: | |
kwarg.append("primary_key") | |
if not self.nullable: | |
kwarg.append("nullable") | |
if self.onupdate: | |
kwarg.append("onupdate") | |
if self.default: | |
kwarg.append("default") | |
# if self.server_default: | |
# kwarg.append("server_default") | |
if self.comment: | |
kwarg.append("comment") | |
return "Column(%s)" % ", ".join( | |
[repr(self.name)] | |
+ [repr(self.type)] | |
+ [repr(x) for x in self.foreign_keys if x is not None] | |
+ [repr(x) for x in self.constraints] | |
# + [ | |
# ( | |
# self.table is not None | |
# and "table=<%s>" % self.table.description | |
# or "table=None" | |
# ) | |
# ] | |
+ ["%s=%s" % (k, repr(getattr(self, k))) for k in kwarg] | |
) | |
def print_tables(meta): | |
"""Export reflected tables to Python code""" | |
replacements = [] | |
def column_str(column): | |
string = __repr__(column) | |
for from_, to_ in replacements: | |
string = string.replace(from_, to_) | |
return string | |
def print_table(table, indent=""): | |
indented_components = [repr(table.name), "metadata"] + [ | |
column_str(c) for c in table.columns | |
] | |
if table.schema: | |
1 / 0 | |
print(indent + "Table(\n" + indent + " " * 4, end="") | |
print((",\n" + indent + " " * 4).join(indented_components)) | |
print(indent + ")") | |
for t in meta.sorted_tables: | |
print("\n") | |
print_table(t, " ") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment