44
55
66RecipeItem = namedtuple ('RecipeItem' , 'id meta amount' )
7- Recipe = namedtuple ('Recipe' , 'result ingredients in_shape out_shape' )
8- # TODO make Recipe a class, make the helpers its methods
97
108
11- def to_recipe (raw ):
12- def reformat_item (raw , default_meta = None ):
13- if isinstance (raw , dict ):
14- raw = raw .copy () # do not modify arg
15- if 'metadata' not in raw :
16- raw ['metadata' ] = default_meta
17- if 'count' not in raw :
18- raw ['count' ] = 1
19- return RecipeItem (raw ['id' ], raw ['metadata' ], raw ['count' ])
20- elif isinstance (raw , list ):
21- return RecipeItem (raw [0 ], raw [1 ], 1 )
22- else : # single ID or None
23- return RecipeItem (raw or None , default_meta , 1 )
9+ class Recipe (object ):
10+ def __init__ (self , raw ):
11+ self .result = reformat_item (raw ['result' ], None )
12+ if 'ingredients' in raw :
13+ self .ingredients = [reformat_item (item , 0 ) for item in raw ['ingredients' ]]
14+ self .in_shape = None
15+ self .out_shape = None
16+ else :
17+ self .in_shape = reformat_shape (raw ['inShape' ])
18+ self .out_shape = reformat_shape (raw ['outShape' ]) \
19+ if 'outShape' in raw else None
20+ self .ingredients = [item for row in self .in_shape for item in row ] # flatten
2421
25- def reformat_shape (shape ):
26- return [[reformat_item (item , None ) for item in row ] for row in shape ]
22+ @property
23+ def total_ingredient_amounts (self ):
24+ totals = defaultdict (int )
25+ for id , meta , amount in self .ingredients :
26+ totals [(id , meta )] += amount
27+ return totals
2728
28- result = reformat_item (raw ['result' ], None )
29- if 'ingredients' in raw :
30- ingredients = [reformat_item (item , 0 ) for item in raw ['ingredients' ]]
31- in_shape = out_shape = None
32- else :
33- in_shape = reformat_shape (raw ['inShape' ])
34- out_shape = reformat_shape (raw ['outShape' ]) \
35- if 'outShape' in raw else None
36- ingredients = [item for row in in_shape for item in row ] # flatten
37- return Recipe (result , ingredients , in_shape , out_shape )
29+ @property
30+ def ingredient_positions (self ):
31+ """
32+ Returns:
33+ dict: In the form { (item_id, metadata) -> [ (x, y, amount), ... ] }
34+ """
35+ positions = defaultdict (list )
36+ for y , row in enumerate (self .in_shape ):
37+ for x , (item_id , metadata , amount ) in enumerate (row ):
38+ positions [(item_id , metadata )].append ((x , y , amount ))
39+ return positions
40+
41+
42+ def reformat_item (raw , default_meta = None ):
43+ if isinstance (raw , dict ):
44+ raw = raw .copy () # do not modify arg
45+ if 'metadata' not in raw :
46+ raw ['metadata' ] = default_meta
47+ if 'count' not in raw :
48+ raw ['count' ] = 1
49+ return RecipeItem (raw ['id' ], raw ['metadata' ], raw ['count' ])
50+ elif isinstance (raw , list ):
51+ return RecipeItem (raw [0 ], raw [1 ], 1 )
52+ else : # single ID or None
53+ return RecipeItem (raw or None , default_meta , 1 )
54+
55+
56+ def reformat_shape (shape ):
57+ return [[reformat_item (item , None ) for item in row ] for row in shape ]
3858
3959
4060def iter_recipes (item_id , meta = None ):
@@ -46,7 +66,7 @@ def iter_recipes(item_id, meta=None):
4666 return # no recipe found, do not yield anything
4767 else :
4868 for raw in recipes_for_item :
49- recipe = to_recipe (raw )
69+ recipe = Recipe (raw )
5070 if meta is None or meta == recipe .result .meta :
5171 yield recipe
5272
@@ -56,22 +76,3 @@ def get_any_recipe(item, meta=None):
5676 for matching in iter_recipes (item , meta ):
5777 return matching
5878 return None
59-
60-
61- def total_ingredient_amounts (recipe ):
62- totals = defaultdict (int )
63- for id , meta , amount in recipe .ingredients :
64- totals [(id , meta )] += amount
65- return totals
66-
67-
68- def ingredient_positions (recipe ):
69- """
70- Returns:
71- dict: In the form { (item_id, metadata) -> [ (x, y, amount), ... ] }
72- """
73- positions = defaultdict (list )
74- for y , row in enumerate (recipe .in_shape ):
75- for x , (item_id , metadata , amount ) in enumerate (row ):
76- positions [(item_id , metadata )].append ((x , y , amount ))
77- return positions
0 commit comments