from tortoise import Tortoise, run_async import asyncio import json import os cwd = os.getcwd() PATHPaths = os.path.join(cwd, "algorithm", "Paths.json") PATHWeights = os.path.join(cwd, "algorithm", "Weights.json") Paths = None Weights = None with open(PATHPaths, "r") as fp: Paths = json.load(fp) with open(PATHWeights, "r") as fp: Weights = json.load(fp) # print("Paths: ", Paths) # print("Weights: ", Weights) async def init(): # Here we create a SQLite DB using file "db.sqlite3" # also specify the app name of "models" # which contain models from "app.models" await Tortoise.init( config={ "connections": { "default": "mysql://root:123456@127.0.0.1:3306/agv?charset=utf8mb4" }, "apps": { "models": { "models": ["aerich.models", "models"], "default_connection": "default" } }, 'use_tz': False, # 'timezone': setting.TIMEZONE } ) # Generate the schema # await Tortoise.generate_schemas() # run_async is a helper function to run simple async Tortoise scripts. run_async(init()) from models.path import Path async def insertData(): cntRow = len(Weights) #exclude itself cntColumn = len(Weights[0]) - 1 keyRow = sorted(Paths.keys(), key=lambda x: int(x)) print("cntRow: {}, cntColumn: {}".format(cntRow, cntColumn)) for idxRow in range(0, cntRow): keySource = keyRow[idxRow] print("Path Source: ", keySource) paths = Paths[keySource] pathsKey = sorted(paths.keys(), key=lambda x: int(x)) for idxColumn in range(0, cntColumn): print("{} -> {}".format(keySource, str(pathsKey[idxColumn]))) print("Weight: ", Weights[idxRow][idxColumn]) print("Path: ", Paths[str(keySource)][str(pathsKey[idxColumn])]) await Path.create( start = keySource, stop = str(pathsKey[idxColumn]), weight = Weights[idxRow][idxColumn], paths = Paths[str(keySource)][str(pathsKey[idxColumn])] ) async def getCnt(): return await Path.all().count() async def main(): print("cnt: ", await getCnt()) await insertData() asyncio.run( main() ) exit()