You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.2 KiB

import json
import math
def printList(data):
print(f"\t\t Lenght: {len(data)}")
def printDict(data):
print(f"\t\t Lenght: {len(data)}")
def getListIndexElements(data, index):
i=0
item=data[index]
print(f"===================================")
print(f"Type: {type(item)}")
print(f"Item No: {index}")
print(f"===================================")
for key in item.keys():
content = item[str(key)]
if type(content) is int:
print(f" -> {key} | Integer | {content}")
elif type(content) is str:
print(f" -> {key} | String | {len(content)}")
elif type(content) is list:
print(f" -> {key} | List | {len(content)}")
elif type(content) is dict:
print(f" -> {key} | Dictionary | {len(content)}")
print(f"===================================")
def getLongerString(data, key, ceilMutpilpe):
len_max = 0
longest_string=""
for item in data:
content = item[key]
if isinstance(content, str):
len_cur = len(content)
if len_cur > len_max:
len_max = len_cur
longest_string = content
return (ceilMutpilpe * math.ceil(len_max/ceilMutpilpe))
def getLongerDict(data, key, ceilMutpilpe):
len_max = 0
longest_string=""
for item in data:
content = str(item[key])
if isinstance(content, str):
len_cur = len(content)
if len_cur > len_max:
len_max = len_cur
longest_string = content
return (ceilMutpilpe * math.ceil(len_max/ceilMutpilpe))
def printSqlAlchemyOnlyString(data, index, primaryKey, dbName):
item = data[index]
print(f"Type: {type(item)}")
print(f"Item No: {index}")
print(f"=============================================")
for key in item.keys():
content = item[str(key)]
if type(content) is int:
if key == primaryKey:
print(f"{key} = {dbName}Column({dbName}Integer, primary_key=True)")
else:
print(f"{key} = {dbName}Column({dbName}Integer, nullable=True)")
elif type(content) is str:
content_lenght = getLongerString(data, key,5)
print(f"{key} = {dbName}Column({dbName}String({content_lenght}), unique=False, nullable=True)")
elif type(content) is list:
content_lenght = getLongerDict(data, key,5)
if content_lenght < 255:
print(f"{key} = {dbName}Column({dbName}String({content_lenght}), unique=False, nullable=True)")
else:
print(f"{key} = {dbName}Column({dbName}Text, unique=False, nullable=True)")
elif type(content) is dict:
content_lenght = getLongerDict(data, key,5)
if content_lenght < 255:
print(f"{key} = {dbName}Column({dbName}String({content_lenght}), unique=False, nullable=True)")
else:
print(f"{key} = {dbName}Column({dbName}Text, unique=False, nullable=True)")
print(f"=============================================")
with open("minibase/blueprints/utils/countries/states.json") as json_data:
data = json.load(json_data)
#getListIndexElements(data,0)
printSqlAlchemyOnlyString(data,0,'id','db.')