jeudi 24 novembre 2016

How to generate same json file multiple times with only one parameter change?

I am generating a topology deployment file which has coordinates and device specifications, I want to generate multiple files which keeps all paramaters same except destination parameter which is random number from a range. Here is the code below, I receive errors such as unexpected indent etc, Since I am new on coding thought maybe my way is wrong so if you have an idea or suggestion, please let me know!

Thanks in advance,

def RSD_Function(devNum,file,devProperties):

    for i in range(0,devNum):
        JsonDump={};
        JsonDump["address"]=devProperties[i][14]
        .
        .
        JsonDump["destination"]=devProperties[i][11]
        .
        .
        ConfigDump[i]= JsonDump;
        JsonDump ={}
        print("sim.devices[",i,"].devTrafficChar.address = ",devProperties[i][14],file=file)
        .
        .
        print("sim.devices[",i,"].trafficModel.destination = ",devProperties[i][11],file=file)
        .
        .

if name == "main":

for num in NumberofDevice:        
    DirName = "Config_"+str(DeploymentChar[2])+"_"+str(num)
    TotalSenDev = math.ceil(num*DeploymentChar[2])
    TotalMainDev = math.ceil(num * ((1-DeploymentChar[2])/2))
    TotalRecDev = math.ceil(num * ((1-DeploymentChar[2])/2))


    devProperties = num * [None]
    DT = ['BLE_DEV_CONNECTED_SUPPLY'] * int(num * DeploymentChar[0]+1) + ['BLE_DEV_BATTRERY_RECHARGEABLE'] * int(num * DeploymentChar[1]+1) +['BLE_DEV_BATTRERY_NON_RECHARGEABLE'] * int(num * DeploymentChar[2]+1)
    random.shuffle(DT)

    for dep in range(0,NumberofDeploy):
        SensorLoc = numpy.random.uniform(0,30,size=(TotalSenDev,2))
        MainLoc = numpy.random.uniform(0,30,size=(TotalMainDev,2))
        RecLoc = numpy.random.uniform(0,30,size=(TotalRecDev,2))
        print("////////////////////")
        print(SensorLoc)
        print("////////////////////")
        print(MainLoc)
        print("////////////////////")
        print(RecLoc)

        AddressAssignment = [i for i in range(num)]
        random.shuffle(AddressAssignment)
        for i in range(0,num):
            if (i < TotalSenDev):
                devProperties[i]=["Random",
                                  SensorLoc[i][0],                                      #X-Pos
                                  SensorLoc[i][1],                                      #Y-Pos
                                  random.randint(1, rangeZ),                            #Z-Pos
                                  random.randint(rangeTxRx, rangeTxRx),                 #RxTx-Range
                                  ComputeClass[random.randint(0, 2)],                   #Compute Class
                                  MemoryClass[random.randint(0, 2)],                    #Memory Class
                                  'BLE_DEV_BATTRERY_NON_RECHARGEABLE',                  #Energy Source
                                  random.randint(50, 99),                               #Battery Percentage
                                  random.randint(50, 99),                               #Battery Size
                                  ValidFreq[random.randint(0,3)],                       #Period of Packet Tx
                                  AddressAssignment[TotalSenDev + i % (num - TotalSenDev)],                             #Peer Address
                                  random.randint(1, num-1)*1000,                        #No of Packets to be Tx
                                  7,                                                    #Delay for the first packet
                                  AddressAssignment[i]];                                       

                devProperties[i][5] = ComputeClass[random.randint(0, 1)] if devProperties[i][7] == "BLE_DEV_BATTRERY_NON_RECHARGEABLE" else ComputeClass[random.randint(0, 2)]
                devProperties[i][6] = MemoryClass[random.randint(0, 1)] if devProperties[i][7] == "BLE_DEV_BATTRERY_NON_RECHARGEABLE" else MemoryClass[random.randint(0, 2)]
                devProperties[i][8] = 99 if devProperties[i][7] == "BLE_DEV_BATTRERY_NON_RECHARGEABLE" else random.randint(50, 99)
                devProperties[i][9] = BatterySize[ 0 if devProperties[i][7] == "BLE_DEV_BATTRERY_NON_RECHARGEABLE" else 1]
                devProperties[i][13] = 7

elif ........................

........................................................

for traf in range(0,NumberofTrafficModel):

            if not os.path.exists(DirName):
                os.makedirs(DirName)

             AddressAssignment = [i for i in range(num)]

             random.shuffle(AddressAssignment)

                devProperties[i][11]=AddressAssignment[TotalSenDev + i % (num - TotalSenDev)]

HERE I want to change destination only for number of each traffic model

fileName =DirName+"\"+str(num)+"Deployment"+str(dep)+"Traf"+str(traf)+".rsd" file = open(fileName,'w+') print("sim.depId = ",dep,file=file) RSD_Function(num,file,devProperties) fileJSON=open(DirName+"\"+str(num)+"Deployment"+str(dep)+"Traf"+str(traf)+".json",'w+') json.dump(ConfigDump,fileJSON,sort_keys=True, indent=4, separators=(',', ': ')) fileJSON.close() file.close()




Aucun commentaire:

Enregistrer un commentaire