Created
April 11, 2026 11:57
-
-
Save oprypin/56b2d3b56487acef34a44a47c943b7f6 to your computer and use it in GitHub Desktop.
Rebrickable: Find all minifigs that can be built from an exported part list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import collections | |
| import csv | |
| inventory_id_to_fig_num: dict[int, str] = {} | |
| print('Reading inventories') # From https://rebrickable.com/downloads/ | |
| with open('inventories.csv', encoding='utf-8') as f: | |
| for row in csv.DictReader(f): | |
| if row['set_num'].startswith('fig-'): | |
| inventory_id_to_fig_num[int(row['id'])] = row['set_num'] | |
| minifig_inventories: dict[str, collections.Counter[tuple[str, int]]] = {} | |
| print('Reading inventory parts') # From https://rebrickable.com/downloads/ | |
| with open('inventory_parts.csv', encoding='utf-8') as f: | |
| for row in csv.DictReader(f): | |
| fig_num = inventory_id_to_fig_num.get(int(row['inventory_id'])) | |
| if fig_num: | |
| inventory = minifig_inventories.setdefault(fig_num, collections.Counter()) | |
| inventory[row['part_num'], int(row['color_id'])] += int(row['quantity']) | |
| my_parts: collections.Counter[tuple[str, int]] = collections.Counter() | |
| print('Reading owned parts') # From a part list that you exported | |
| with open('rebrickable_parts_my-parts.csv', encoding='utf-8') as f: | |
| for row in csv.DictReader(f): | |
| my_parts[row['Part'], int(row['Color'])] += int(row['Quantity']) | |
| print('Finding matches') # By intersecting each minifig with the entire part list | |
| result_minifigs: list[str] = [] | |
| for fig_num, fig_inventory in minifig_inventories.items(): | |
| needed_count = fig_inventory.total() | |
| have_count = (fig_inventory & my_parts).total() | |
| if have_count == needed_count: | |
| result_minifigs.append(fig_num) | |
| print('Writing a new set list') | |
| with open('rebrickable_sets_matching-minifigs.csv', 'w', encoding='utf-8') as f: | |
| print('Set Number,Quantity,Includes Spares,Inventory Ver', file=f) | |
| for fig_num in result_minifigs: | |
| print(f'{fig_num},0,False,1', file=f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment