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 )
14+ for item in raw ['ingredients' ]]
15+ self . in_shape = None
16+ self . out_shape = None
17+ else :
18+ self . in_shape = reformat_shape (raw [ 'inShape' ])
19+ self . out_shape = reformat_shape (raw ['outShape' ]) \
20+ if 'outShape' in raw else None
21+ self . ingredients = [ item for row in self . in_shape for item in row ]
2422
25- def reformat_shape (shape ):
26- return [[reformat_item (item , None ) for item in row ] for row in shape ]
23+ @property
24+ def total_ingredient_amounts (self ):
25+ """
26+ Returns:
27+ dict: In the form { (item_id, metadata) -> amount }
28+ """
29+ totals = defaultdict (int )
30+ for id , meta , amount in self .ingredients :
31+ totals [(id , meta )] += amount
32+ return totals
2733
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 )
34+ @property
35+ def ingredient_positions (self ):
36+ """
37+ Returns:
38+ dict: In the form { (item_id, metadata) -> [(x, y, amount), ...] }
39+ """
40+ positions = defaultdict (list )
41+ for y , row in enumerate (self .in_shape ):
42+ for x , (item_id , metadata , amount ) in enumerate (row ):
43+ positions [(item_id , metadata )].append ((x , y , amount ))
44+ return positions
45+
46+
47+ def reformat_item (raw , default_meta = None ):
48+ if isinstance (raw , dict ):
49+ raw = raw .copy () # do not modify arg
50+ if 'metadata' not in raw :
51+ raw ['metadata' ] = default_meta
52+ if 'count' not in raw :
53+ raw ['count' ] = 1
54+ return RecipeItem (raw ['id' ], raw ['metadata' ], raw ['count' ])
55+ elif isinstance (raw , list ):
56+ return RecipeItem (raw [0 ], raw [1 ], 1 )
57+ else : # single ID or None
58+ return RecipeItem (raw or None , default_meta , 1 )
59+
60+
61+ def reformat_shape (shape ):
62+ return [[reformat_item (item , None ) for item in row ] for row in shape ]
3863
3964
4065def iter_recipes (item_id , meta = None ):
@@ -46,7 +71,7 @@ def iter_recipes(item_id, meta=None):
4671 return # no recipe found, do not yield anything
4772 else :
4873 for raw in recipes_for_item :
49- recipe = to_recipe (raw )
74+ recipe = Recipe (raw )
5075 if meta is None or meta == recipe .result .meta :
5176 yield recipe
5277
@@ -56,22 +81,3 @@ def get_any_recipe(item, meta=None):
5681 for matching in iter_recipes (item , meta ):
5782 return matching
5883 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