Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.46 KB | None | 0 0
  1. # MASTER PROGRAM, WITH ALL SUBPROGRAMS
  2.  
  3. #-------------------------------------------------------------------------------------------#
  4.  
  5. # writes componentDict into components.txt
  6. # get all keys in list, sort keys in order, construct string that is written to file by concatenating stuffi
  7. def turnComponentDictToComponentFile(componentDict):
  8.     dKeys = componentDict.keys() # get all dictionary keys in a list and sort them
  9.     dKeys.sort()
  10.     # construct file object one component (key) at a time
  11.     fileObject = ""
  12.     for key in dKeys:
  13.         valueTuple = componentDict[key]
  14.         fileObject += key + "," + str(valueTuple[0]) + "," + str(valueTuple[1]) + "," + str(valueTuple[2]) + "\n"
  15.     f = open("components.txt", "w")
  16.     f.write(fileObject)
  17.     f.close()
  18.  
  19. # help:        
  20. # 'recipeDict' - {recipeName:[(componentName, concentration, unit), (anotherTuple), ...]}
  21. # 'componentDict' - {componentName:(molarMass, phaseInNTP, density)}
  22. # 'fileObject' - componentName,molarMass,phaseInNTP,density
  23.  
  24. #-------------------------------------------------------------------------------------------#
  25.  
  26. # reads componentDict.txt into dictionaries
  27. def turnComponentFileToComponentDict():
  28.     dictionary = {}
  29.     dictionaryFile = open("components.txt", "r")
  30.     for line in dictionaryFile:
  31.         line = line[:] # change 'line' to reference the string that corresponds file object read from file
  32.         line = line.replace("\n", "") # remove line feed
  33.         splitData = line.split(",")
  34.         name = splitData[0]         # 'componentName' in components.txt
  35.         molarMass = splitData[1]    # 'molarMass' in components.txt
  36.         phaseInNTP = splitData[2]   # 'phaseInNTP' in components.txt
  37.         density = splitData[3]      # 'density' in components.txt
  38.         # change number strings to floats, does nothing to strings with other characters
  39.         dictionary[name] = (float(molarMass), phaseInNTP, float(density))
  40.     dictionaryFile.close()
  41.     return dictionary
  42.  
  43. # help:
  44. # 'recipeDict': {recipeName:[(componentName, concentration, unit), (anotherTuple), ...]}
  45. # 'componentDict': {componentName:(molarMass, phaseInNTP, density)}
  46.  
  47. #-------------------------------------------------------------------------------------------#
  48.  
  49. # writes buffer recipes from dictionary to 'recipes.txt' in alphabetical order
  50. def turnRecipeDictToRecipeFile(recipeDict):
  51.     dKeys = recipeDict.keys()
  52.     dKeys.sort()
  53.     fileObject = ""
  54.     for key in dKeys:
  55.         fileObject += key + ":"
  56.         recipeContentList = recipeDict[key]
  57.         for i in recipeContentList:
  58.             fileObject += str(i[0]) + "," + str(i[1]) + "," + str(i[2]) + "-"
  59.         fileObject += "\n"
  60.     f = open("recipes.txt", "w")
  61.     f.write(fileObject)
  62.     f.close()
  63.  
  64. # help
  65. # 'recipeDict' - {recipeName:[(componentName, concentration, unit), (anotherTuple), ...]}
  66. # 'componentDict' - {componentName:(molarMass, phaseInNTP, density)}
  67. # 'fileObject' - key:componentName,concentration,unit;componentName,concentration,unit-...\n'''repeat'''\n'''repeat'''\n
  68.  
  69. #-------------------------------------------------------------------------------------------#
  70.  
  71. # builds 'recipeDict' from 'recipes.txt'
  72. def turnRecipeFileToRecipeDict():
  73.     dictionary = {}
  74.     dictionaryFile = open("recipes.txt", "r")
  75.     for line in dictionaryFile:
  76.         line = line[:]
  77.         line = line.replace("\n", "")
  78.         splitData = line.split(":") # recipe name and component information is separated by ":"
  79.         recipeKey = splitData[0] # name of a recipe in file, used as key in 'dictionary'
  80.         allComponents = splitData[1] # all components of given recipe
  81.         allComponentsStringList = allComponents.split("-") # different component of given recipe are separated by "-"
  82.         allComponentsStringList.remove("")
  83.         allComponentsList = []
  84.         # build 'allComponentsList' which contains components as tuples
  85.         for i in allComponentsStringList:
  86.             componentInfo = i.split(",") # different component values are separated by ","
  87.             componentInfoTuple = (componentInfo[0], float(componentInfo[1]), componentInfo[2])
  88.             allComponentsList.append(componentInfoTuple)
  89.            
  90.         dictionary[recipeKey] = allComponentsList
  91.     dictionaryFile.close()
  92.     return dictionary
  93.  
  94. # help
  95. # 'recipeDict' - {recipeName:[(componentName, concentration, unit), (anotherTuple), ...]}
  96. # 'componentDict' - {componentName:(molarMass, phaseInNTP, density)}
  97. # 'fileObject' - key:componentName,concentration,unit-componentName,concentration,unit-...\n'''repeat'''\n'''repeat'''\n
  98.  
  99. #-------------------------------------------------------------------------------------------#
  100. ## NEW BUFFER RECIPE
  101. #-------------------------------------------------------------------------------------------#
  102. ## CALCULATE INSTRUCTIONS
  103. #-------------------------------------------------------------------------------------------#
  104.  
  105. # INTERFACE
  106. 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"
  107. firstChoice = raw_input("Choose a category ('1', '2', '3' or '4'): ")
  108. print ""
  109. if firstChoice == "1":
  110.     print "a. Add a new recipe\nb. Remove recipe\nc. Search for a recipe\nd. Show all recipes"
  111.     secondChoice = raw_input("Choose an operation ('a', 'b', 'c' or 'd'): ")
  112.     print ""
  113.     if secondChoice == "a":
  114.         # def addNewRecipe
  115.     elif secondChoice == "b":
  116.         #
  117.     elif secondChoice == "c":
  118.         #
  119.     elif secondChoice == "d":
  120.         #
  121. elif firstChoice == "2":
  122.     print "a. Add a new component\nb. Remove component\nc. Search for a component\nd. Show all components"
  123.     secondChoice = raw_input("Choose an operation ('a', 'b', 'c' or 'd'): ")
  124.     print ""
  125. elif firstChoice == "3":
  126.     print "a. Calculate a new instruction file\nb. Remove all existing instruction files"
  127.     secondChoice = raw_input("Choose an operation ('a' or 'b'): ")
  128.     print ""
  129.     if secondChoice == "a":
  130.         componentDict = turnComponentFileToComponentDict()
  131.         recipeDict = turnRecipeFileToRecipeDict()
  132.         calculateBufferInstructions(recipeDict, componentDict)
  133.     elif secondChoice == "b":
  134.         # defRemoveInstructions (one specific or all files)
  135. elif firstChoice == "4":
  136.     print "BYE!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement