Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Recipe:
- def __init__(self, setname, piecename, minlevel, maxlevel):
- self.setname = setname
- self.piecename = piecename
- self.minlevel = minlevel
- self.maxlevel = maxlevel
- def serialize(self):
- return ";".join([self.setname, self.piecename, str(self.minlevel), str(self.maxlevel)])
- class Ingredient:
- def __init__(self, recipe, salvage, count):
- self.recipe = recipe
- self.salvage = salvage
- self.count = count
- def serialize(self):
- recipeString = self.recipe.serialize()
- return recipeString + ";" + self.salvage + ";" + str(self.count)
- def addOneToDict(d, k):
- if k in d.keys():
- d[k] += 1
- else:
- d[k] = 1
- def setDangerAdd(s, v):
- if v in s:
- raise ValueError(f"Value {v} already exists in set")
- else:
- s.add(v)
- def splitRow(row):
- (setname, piecename, minlevel, maxlevel,
- salvage1, salvage2, salvage3, salvage4, salvage5) = row.split(";")
- recipe = Recipe(setname, piecename, minlevel, maxlevel)
- ingredients = {}
- for salvage in [salvage1, salvage2, salvage3, salvage4, salvage5]:
- if salvage == "":
- continue
- addOneToDict(ingredients, salvage)
- ingredientsOut = []
- for k in ingredients.keys():
- ingredientsOut.append(Ingredient(recipe, k, ingredients[k]))
- return (recipe, ingredientsOut)
- if __name__ == "__main__":
- recipes = set()
- ingredients = set()
- with open("C:\\Users\\tvile\\Downloads\\Recipe.txt") as rFile:
- skip = True
- for line in rFile:
- if skip:
- skip = False
- continue
- line = line.strip()
- (cRecipe, cIngredients) = splitRow(line)
- setDangerAdd(recipes, cRecipe.serialize())
- for ing in cIngredients:
- setDangerAdd(ingredients, ing.serialize())
- with open("RecipesOut.txt","w") as recipesOut:
- rStrings = [e for e in recipes]
- recipesOut.write("\n".join(rStrings))
- with open("IngredientsOut.txt", "w") as ingredientsOut:
- iStrings = [e for e in ingredients]
- ingredientsOut.write("\n".join(iStrings))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement