Code: Alles auswählen
import os
class Variable:
def __init__(self, path, name, inputValue = "ValueIsNA"):
self.executerpath = path
self.variabledirectory = os.path.join(self.executerpath,"VariableData")
self.name = str(name) #variable name
self.impData()
#the system finds out which of these actions happened, and sets itself to the corresponding value
if inputValue == "ValueIsNA" and self.importedValue == "ValueIsNA":
#completely new to the system
self.value="ValueIsNA"
elif inputValue != "ValueIsNA":
#declare a new variable
self.value=inputValue
self.save()
elif inputValue == "ValueIsNA" and self.importedValue != "ValueIsNA":
#imports the variable
self.value=self.importedValue
def impData(self): #imports variable data
self.varNamePath = os.path.join(self.executerpath,"VariableData","varName.lcs")
self.varDataPath = os.path.join(self.executerpath,"VariableData","varData.lcs")
#create directories, if they don't exist
if os.path.exists(self.variabledirectory) == False:
os.makedirs(self.variabledirectory)
self.varNameP = open(self.varNamePath, "a")
self.varDataP = open(self.varDataPath, "a")
self.varNameP.close()
self.varDataP.close()
self.varNameP = open(self.varNamePath,"r")
self.varNameI = self.varNameP.readlines()
if (self.name+"\n") in self.varNameI: #sees if a variable with this name exists
self.index=self.varNameI.index(self.name+"\n")
#finds out index and returns the value for the index
self.varDataP = open(self.varDataPath,"r")
self.varDataI = self.varDataP.readlines()
self.importedValue=self.varDataI[int(self.index)]
self.varDataP.close()
else: #if variable does not exists, the return value is "ValueIsNA"
self.importedValue="ValueIsNA"
self.index="X"
self.varNameP.close()
def save(self): #saves the variable
if self.index == "X":
#for new variables: appends it onto the end
self.varNameP = open(self.varNamePath,"a")
self.varDataP = open(self.varDataPath,"a")
self.varNameP.write(str(self.name )+ "\n")
self.varDataP.write(str(self.value)+ "\n")
self.varNameP.close()
self.varDataP.close()
#finds itself in the list and sets it's own index
self.varNameP=open(self.varNamePath,"r")
self.varNameI=self.varNameP.readlines()
self.index=self.varNameI.index(self.name+"\n")
self.varNameP.close()
else:
#opens the file, copies the data, replaces itself and saves it again
self.varDataP= open(self.varDataPath,"r")
self.varDataRaw=self.varDataP.readlines()
self.varDataP.close()
self.varDataP= open(self.varDataPath,"w")
self.varDataRaw[self.index]=str(self.value)+"\n"
self.varDataP.writelines(self.varDataRaw)
self.varDataP.close()
def setValue(self, value): #sets itself to a value
self.value=value
self.save()
def delete(self): #deletes variable
#imports raw values from the files
self.varNameP = open(self.varNamePath,"r")
self.varDataP = open(self.varDataPath,"r")
self.varNameRaw=self.varNameP.readlines()
self.varDataRaw=self.varDataP.readlines()
self.varNameP.close()
self.varDataP.close()
#removes itself from the list
del self.varNameRaw[self.index]
del self.varDataRaw[self.index]
#saves the altered raw data back into the file
self.varNameP = open(self.varNamePath,"w")
self.varDataP = open(self.varDataPath,"w")
self.varNameP.writelines(self.varNameRaw)
self.varDataP.writelines(self.varDataRaw)
self.varNameP.close()
self.varDataP.close()