1
\$\begingroup\$

I have this function which converts a couple of 2D NumPy arrays into strings and then saves them in a text file (grid, quality, roadMap, fireEffectMap, hospitalEffectMap, policeEffectMap, fireMap, densityMap, errorMap, waterMap) as well as a couple of other vars (cash, size), size being 48, but this is long and ugly. Also, I have 3 different strings/lines for a variable, because each of the values in the waterMap var can be 3 characters long, e.g. 0-255, that and density, but the rest are 1 character per "slot" in the 2d array. How do I make this more compact, easier to understand/read and also add more in the future if needed?

 def save(saves, seed, cash, grid, quality, roadMap, fireEffectMap, hospitalEffectMap, policeEffectMap, fireMap, densityMap, errorMap, waterMap, size):
    print("Saving File...")
    currentDir = os.getcwd() #Get current directory
    os.chdir('saves') #Set the current dir to saves
    newFileName = "Save" + str(len(saves) + 1) + ".simcity" #Set the name of the save to Save + biggest number.simcity
    file = open(newFileName, "w") #Open/Create a new file

    seedString = ""
    for i in range(size):
        for j in range(size):
            seedString = seedString + str(seed[i, j])
    file.write(seedString + "\n")

    file.write(str(cash) + "\n") #Store your cash in line 2

    gridString = ""
    qualityString = ""
    roadMapString = ""
    fireEffectMapString = ""
    hospitalEffectMapString = ""
    policeEffectMapString = ""
    fireMapString = ""
    errorMapString = ""

    densityMapStringValue = ""
    densityMapString1 = ""
    densityMapString2 = ""

    waterMapString = ""
    waterMapString1 = ""
    waterMapString2 = ""
    waterMapString3 = ""

    for j in range(size):
        for i in range(size): #Storing all of the 2d numpy arrays in strings on seperate lines

            gridString = gridString + grid[i, j]
            qualityString = qualityString + str(quality[i, j])
            roadMapString = roadMapString + str(roadMap[i, j])
            fireEffectMapString = fireEffectMapString + str(fireEffectMap[i, j])
            hospitalEffectMapString = hospitalEffectMapString + str(hospitalEffectMap[i, j])
            policeEffectMapString = policeEffectMapString + str(policeEffectMap[i, j])
            fireMapString = fireMapString + str(fireMap[i, j])
            errorMapString = errorMapString + str(errorMap[i, j])
            densityMapStringValue = str(densityMap[i, j])                
            waterMapStringValue = str(waterMap[i, j])



            if len(str(densityMap[i,j])) == 2:
                densityMapString1 = densityMapString1 + str(densityMapStringValue[0])
                densityMapString2 = densityMapString2 + str(densityMapStringValue[1])
            else:
                densityMapString1 = densityMapString1 + "0"
                densityMapString2 = densityMapString2 + str(densityMapStringValue[0])

            if len(str(waterMap[i,j])) == 3:
                waterMapString1 = waterMapString1 + str(waterMapStringValue[0])
                waterMapString2 = waterMapString2 + str(waterMapStringValue[1])
                waterMapString3 = waterMapString3 + str(waterMapStringValue[2])

            elif len(str(waterMap[i,j])) == 2:
                waterMapString1 = waterMapString1 + "0"
                waterMapString2 = waterMapString2 + str(waterMapStringValue[0])
                waterMapString3 = waterMapString3 + str(waterMapStringValue[1])
            else:
                waterMapString1 = waterMapString1 + "0"
                waterMapString2 = waterMapString2 + "0"
                waterMapString3 = waterMapString3 + str(waterMapStringValue[0])


    file.write(gridString + "\n")
    file.write(qualityString + "\n")
    file.write(roadMapString + "\n")
    file.write(fireEffectMapString + "\n")
    file.write(hospitalEffectMapString + "\n")
    file.write(policeEffectMapString + "\n")
    file.write(fireMapString + "\n")
    file.write(errorMapString + "\n")

    file.write(densityMapString1 + "\n")
    file.write(densityMapString2 + "\n")

    file.write(waterMapString1 + "\n")
    file.write(waterMapString2 + "\n")
    file.write(waterMapString3 + "\n")

    file.close()
    os.chdir(currentDir)
\$\endgroup\$
  • 1
    \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly. \$\endgroup\$ – Toby Speight Nov 14 '18 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.