Advertisement
SoobNauce

Untitled

Aug 4th, 2021
1,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. class Recipe:
  2.     def __init__(self, setname, piecename, minlevel, maxlevel):
  3.         self.setname = setname
  4.         self.piecename = piecename
  5.         self.minlevel = minlevel
  6.         self.maxlevel = maxlevel
  7.     def serialize(self):
  8.         return ";".join([self.setname, self.piecename, str(self.minlevel), str(self.maxlevel)])
  9.  
  10. class Ingredient:
  11.     def __init__(self, recipe, salvage, count):
  12.         self.recipe = recipe
  13.         self.salvage = salvage
  14.         self.count = count
  15.     def serialize(self):
  16.         recipeString = self.recipe.serialize()
  17.         return recipeString + ";" + self.salvage + ";" + str(self.count)
  18.  
  19. def addOneToDict(d, k):
  20.     if k in d.keys():
  21.         d[k] += 1
  22.     else:
  23.         d[k] = 1
  24.  
  25. def setDangerAdd(s, v):
  26.     if v in s:
  27.         raise ValueError(f"Value {v} already exists in set")
  28.     else:
  29.         s.add(v)
  30.  
  31. def splitRow(row):
  32.     (setname, piecename, minlevel, maxlevel,
  33.      salvage1, salvage2, salvage3, salvage4, salvage5) = row.split(";")
  34.     recipe = Recipe(setname, piecename, minlevel, maxlevel)
  35.     ingredients = {}
  36.     for salvage in [salvage1, salvage2, salvage3, salvage4, salvage5]:
  37.         if salvage == "":
  38.             continue
  39.         addOneToDict(ingredients, salvage)
  40.     ingredientsOut = []
  41.     for k in ingredients.keys():
  42.         ingredientsOut.append(Ingredient(recipe, k, ingredients[k]))
  43.     return (recipe, ingredientsOut)
  44.  
  45. if __name__ == "__main__":
  46.     recipes = set()
  47.     ingredients = set()
  48.     with open("C:\\Users\\tvile\\Downloads\\Recipe.txt") as rFile:
  49.         skip = True
  50.         for line in rFile:
  51.             if skip:
  52.                 skip = False
  53.                 continue
  54.             line = line.strip()
  55.             (cRecipe, cIngredients) = splitRow(line)
  56.             setDangerAdd(recipes, cRecipe.serialize())
  57.             for ing in cIngredients:
  58.                 setDangerAdd(ingredients, ing.serialize())
  59.         with open("RecipesOut.txt","w") as recipesOut:
  60.             rStrings = [e for e in recipes]
  61.             recipesOut.write("\n".join(rStrings))
  62.         with open("IngredientsOut.txt", "w") as ingredientsOut:
  63.             iStrings = [e for e in ingredients]
  64.             ingredientsOut.write("\n".join(iStrings))
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement