Last active
August 29, 2015 14:06
-
-
Save mocyuto/c3ed871300adbdb4d09c to your computer and use it in GitHub Desktop.
SQLをcase classに変換
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
# -*- coding:utf-8 -*- | |
import re | |
import sys | |
def to_camel_case(snake_str): | |
components = snake_str.split('_') | |
return components[0] + "".join(x.title() for x in components[1:]) | |
def caseClass(list): | |
for i,elem in enumerate(list[0]): | |
k = list[0][i].rstrip().strip() | |
v = list[1][i].rstrip().strip() | |
if (re.match("[0-9]",v)): | |
print to_camel_case(k) +": Option[Int]", "= Option("+ v +")," | |
elif(re.search("NULL",v)): | |
print to_camel_case(k) +": Option[String]," | |
elif(re.search("''",v)): | |
print to_camel_case(k) +": Option[String]", "= Option(\"\")," | |
else: | |
print to_camel_case(k) +": Option[String]", "= Option(\""+ v +"\")," | |
def methods(list): | |
for i,elem in enumerate(list[0]): | |
k = list[0][i].rstrip().strip() | |
v = list[1][i].rstrip().strip() | |
if (re.match("[0-9]",v)): | |
print "def " + to_camel_case(k) +" = column[Option[Int]]" + "(\"" + k + "\")" | |
else: | |
print "def " + to_camel_case(k) +" = column[Option[String]]" + "(\"" + k + "\")" | |
def hlist(list): | |
for i,elem in enumerate(list[0]): | |
k = list[0][i].rstrip().strip() | |
print ":: " + to_camel_case(k), | |
def hlistType(list): | |
for i,elem in enumerate(list[0]): | |
v = list[1][i].rstrip().strip() | |
if(re.match("[0-9]",v)): | |
print ":: Option[Int]", | |
else: | |
print ":: Option[String]", | |
if __name__ == "__main__": | |
if (len(sys.argv) < 1): | |
print "please input file" | |
filename = sys.argv[1] | |
list = [] | |
with open(filename) as f: | |
for line in f: | |
list.append(line.rstrip().split(",")) | |
caseClass(list) | |
methods(list) | |
hlist(list) | |
hlistType(list) |
regist_date, initial_rate, deliver_status, start_date, end_date, unit_price,wholesale_price
Now(), 100, 0, '2014-09-24', '2030-12-31', NULL, NUL
みたいな感じのcsvです。
👍
早速使わせていただきます。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
で変換できます。
1行目:カラム名のカンマ区切り
2行目:値のカンマ区切り