Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MASTER PROGRAM, WITH ALL SUBPROGRAMS
- #-------------------------------------------------------------------------------------------#
- # writes componentDict into components.txt
- # get all keys in list, sort keys in order, construct string that is written to file by concatenating stuffi
- def turnComponentDictToComponentFile(componentDict):
- dKeys = componentDict.keys() # get all dictionary keys in a list and sort them
- dKeys.sort()
- # construct file object one component (key) at a time
- fileObject = ""
- for key in dKeys:
- valueTuple = componentDict[key]
- fileObject += key + "," + str(valueTuple[0]) + "," + str(valueTuple[1]) + "," + str(valueTuple[2]) + "\n"
- f = open("components.txt", "w")
- f.write(fileObject)
- f.close()
- # help:
- # 'recipeDict' - {recipeName:[(componentName, concentration, unit), (anotherTuple), ...]}
- # 'componentDict' - {componentName:(molarMass, phaseInNTP, density)}
- # 'fileObject' - componentName,molarMass,phaseInNTP,density
- #-------------------------------------------------------------------------------------------#
- # reads componentDict.txt into dictionaries
- def turnComponentFileToComponentDict():
- dictionary = {}
- dictionaryFile = open("components.txt", "r")
- for line in dictionaryFile:
- line = line[:] # change 'line' to reference the string that corresponds file object read from file
- line = line.replace("\n", "") # remove line feed
- splitData = line.split(",")
- name = splitData[0] # 'componentName' in components.txt
- molarMass = splitData[1] # 'molarMass' in components.txt
- phaseInNTP = splitData[2] # 'phaseInNTP' in components.txt
- density = splitData[3] # 'density' in components.txt
- # change number strings to floats, does nothing to strings with other characters
- dictionary[name] = (float(molarMass), phaseInNTP, float(density))
- dictionaryFile.close()
- return dictionary
- # help:
- # 'recipeDict': {recipeName:[(componentName, concentration, unit), (anotherTuple), ...]}
- # 'componentDict': {componentName:(molarMass, phaseInNTP, density)}
- #-------------------------------------------------------------------------------------------#
- # writes buffer recipes from dictionary to 'recipes.txt' in alphabetical order
- def turnRecipeDictToRecipeFile(recipeDict):
- dKeys = recipeDict.keys()
- dKeys.sort()
- fileObject = ""
- for key in dKeys:
- fileObject += key + ":"
- recipeContentList = recipeDict[key]
- for i in recipeContentList:
- fileObject += str(i[0]) + "," + str(i[1]) + "," + str(i[2]) + "-"
- fileObject += "\n"
- f = open("recipes.txt", "w")
- f.write(fileObject)
- f.close()
- # help
- # 'recipeDict' - {recipeName:[(componentName, concentration, unit), (anotherTuple), ...]}
- # 'componentDict' - {componentName:(molarMass, phaseInNTP, density)}
- # 'fileObject' - key:componentName,concentration,unit;componentName,concentration,unit-...\n'''repeat'''\n'''repeat'''\n
- #-------------------------------------------------------------------------------------------#
- # builds 'recipeDict' from 'recipes.txt'
- def turnRecipeFileToRecipeDict():
- dictionary = {}
- dictionaryFile = open("recipes.txt", "r")
- for line in dictionaryFile:
- line = line[:]
- line = line.replace("\n", "")
- splitData = line.split(":") # recipe name and component information is separated by ":"
- recipeKey = splitData[0] # name of a recipe in file, used as key in 'dictionary'
- allComponents = splitData[1] # all components of given recipe
- allComponentsStringList = allComponents.split("-") # different component of given recipe are separated by "-"
- allComponentsStringList.remove("")
- allComponentsList = []
- # build 'allComponentsList' which contains components as tuples
- for i in allComponentsStringList:
- componentInfo = i.split(",") # different component values are separated by ","
- componentInfoTuple = (componentInfo[0], float(componentInfo[1]), componentInfo[2])
- allComponentsList.append(componentInfoTuple)
- dictionary[recipeKey] = allComponentsList
- dictionaryFile.close()
- return dictionary
- # help
- # 'recipeDict' - {recipeName:[(componentName, concentration, unit), (anotherTuple), ...]}
- # 'componentDict' - {componentName:(molarMass, phaseInNTP, density)}
- # 'fileObject' - key:componentName,concentration,unit-componentName,concentration,unit-...\n'''repeat'''\n'''repeat'''\n
- #-------------------------------------------------------------------------------------------#
- ## NEW BUFFER RECIPE
- #-------------------------------------------------------------------------------------------#
- ## CALCULATE INSTRUCTIONS
- #-------------------------------------------------------------------------------------------#
- # INTERFACE
- print "MENU\n1. Buffer Recipes\n\ta. Add a new recipe\n\tb. Remove recipe\n\tc. Search for a recipe\n\td. Show all recipes\n2. Buffer Components\n\ta. Add a new component\n\tb. Remove component\n\tc. Search for a component\n\td. Show all components\n3. Buffer Instructions\n\ta. Calculate a new instruction file\n\tb. Remove all existing instruction files\n4. Quit"
- firstChoice = raw_input("Choose a category ('1', '2', '3' or '4'): ")
- print ""
- if firstChoice == "1":
- print "a. Add a new recipe\nb. Remove recipe\nc. Search for a recipe\nd. Show all recipes"
- secondChoice = raw_input("Choose an operation ('a', 'b', 'c' or 'd'): ")
- print ""
- if secondChoice == "a":
- # def addNewRecipe
- elif secondChoice == "b":
- #
- elif secondChoice == "c":
- #
- elif secondChoice == "d":
- #
- elif firstChoice == "2":
- print "a. Add a new component\nb. Remove component\nc. Search for a component\nd. Show all components"
- secondChoice = raw_input("Choose an operation ('a', 'b', 'c' or 'd'): ")
- print ""
- elif firstChoice == "3":
- print "a. Calculate a new instruction file\nb. Remove all existing instruction files"
- secondChoice = raw_input("Choose an operation ('a' or 'b'): ")
- print ""
- if secondChoice == "a":
- componentDict = turnComponentFileToComponentDict()
- recipeDict = turnRecipeFileToRecipeDict()
- calculateBufferInstructions(recipeDict, componentDict)
- elif secondChoice == "b":
- # defRemoveInstructions (one specific or all files)
- elif firstChoice == "4":
- print "BYE!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement