Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
根据 csv 生成 SQLAlchemy models 代码
#!/usr/bin/env python3
# Author : weaming
# Mail : garden.yuen@gmail.com
# Created : 2020-07-19 21:01:50
# Usage:
# csv_json: pip3 install pretty-format-json
# xsv: brew install xsv
# cat esf-nn-rst.csv | xsv stats | csv_json | generate-sqalchemy-model-code.py
import sys
import json
def read_stdin():
return json.load(sys.stdin)
def try_load_as_json(v):
if not (str(v).startswith('[') or str(v).startswith('{')):
return v
try:
return json.loads(v)
except Exception:
return v
def parse_stat(stat):
"""
{
"field": "bieming",
"type": "Unicode",
"sum": "",
"min": "别名:21时代公寓",
"max": "别名:龙胜小区",
"min_length": "0",
"max_length": "141",
"mean": "",
"stddev": ""
}
"""
stat['min_length'] = int(stat['min_length'])
stat['max_length'] = int(stat['max_length'])
stat['min'] = try_load_as_json(stat['min'])
stat['max'] = try_load_as_json(stat['max'])
return stat
def gen_field(stat):
tt = type(stat['max'])
t = {
int: 'db.Integer',
float: 'db.Float',
dict: 'JSONField',
list: 'JSONField',
str: 'db.String({})',
}[tt]
if tt is str:
l = max(stat['max_length'], 64)
while l % 64 != 0:
l += 1
if l >= 1024:
t = 'db.Text'
else:
t = t.format(l)
nullable = stat['min_length'] == 0 if tt not in [dict, list] else not stat['min']
return f"{stat['field']} = db.Column({t}, nullable={nullable}, comment='{str(stat['field'])}') # {stat['field']}"
def main():
stats = read_stdin()
for stat in stats:
stat = parse_stat(stat)
print(gen_field(stat))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment