def session(): engine = create_engine('sqlite:///:memory:', echo=True) Session.configure(bind=engine) # You probably need to create some tables and # load some test data, do so here. # To create tables, you typically do: Base.metadata.create_all(engine) yield Session() Session.close_all()
def get_max(api, erase=False): session = Session() if erase: # reset the not found session.query(Moc).filter(Moc.moc_url.is_(None)).delete() session.commit() MOC_MAX = MOC_MIN for moc in session.query(Moc): i = int(moc.set_num[4:]) if i > MOC_MAX: MOC_MAX = i i = MOC_MAX while True: # geometrical growing if is_moc_absent(api, i): break i = i * 2 time.sleep(0.1) # now we bisect i0 = MOC_MIN # found i1 = i # not found while True: if i1 - i0 == 1: break i = int((i0 + i1) / 2) if is_moc_absent(api, i): i1 = i else: i0 = i print('%i %i' % (i0, i1)) print('MOC_MAX %i' % i0) return i0
# Used to answer https://bricks.stackexchange.com/q/10548/ import pprint from sqlalchemy import and_ from rebrickable.data.database import Session from rebrickable.data.models import Part, Inventory, InventoryPart, Color, Set from rebrickable.data.utils import object_as_dict session = Session() parts = session.query(Part) colors = session.query(Color) sets = session.query( Set.set_num).join(Inventory).join(InventoryPart).join(Part).join(Color) # cap cap = parts.get("86035") cone = parts.get('4589') knit_cap = parts.get("41334") white = colors.filter_by(name='White').one() blue = colors.filter_by(name='Blue').one() black = colors.filter_by(name='Black').one() light_bley = colors.filter_by(name='Light Bluish Gray').one() white_caps = sets.filter( and_(InventoryPart.part == cap, InventoryPart.color == white)).all() black_knit_cap = sets.filter( and_(InventoryPart.part == knit_cap, InventoryPart.color == black)).all() gray_cone = sets.filter(
from sqlalchemy import func from rebrickable.data.models import InventoryPart from rebrickable.data.database import Session session = Session() max_parts = session.query(func.max(InventoryPart.quantity)).filter(InventoryPart.quantity > 1).group_by(InventoryPart.part_num).all()
from pprint import pprint import six from sqlalchemy import not_, and_ from rebrickable.api import UsersApi from rebrickable.cli.utils import get_api_client, get_user_token from rebrickable.data.database import Session from rebrickable.data.models import Theme, Set, PartCategory session = Session() def get_parts(): theme = session.query(Theme).filter_by(name='Minecraft').first() sets = session.query(Set).filter( Set.theme == theme, and_(not_(Set.name.contains('Micro')), not_(Set.name.contains('Skin Pack')))) overworld = [ ('The Cave', '21113-1'), ('The Farm', '21114-1'), ('The First Night', '21115-1'), ('Crafting Box', '21116-1'), ('The Dungeon', '21119-1'), ('The Snow Hideout', '21120-1'), ('The Desert Outpost', '21121-1'), ('The Iron Golem', '21123-1'), ('The Fortress', '21127-1'),
def get_moc(api, i): session = Session() moc = 'MOC-%i' % i print('getting %s... ' % moc, end='') try: moc_data = session.query(Moc).get(moc) if moc_data: print(' in database') return moc_data moc_data = Moc(**api.lego_mocs_read(moc).to_dict()) mocs_parts_list = api.lego_mocs_parts_list(moc_data.set_num) inventory = Inventory(set_num=moc_data.set_num) for inventory_part in mocs_parts_list.results: inventory_part_dict = inventory_part.to_dict() inventory_part_dict.pop('inv_part_id') inventory_part_dict.pop('id') color = inventory_part_dict.pop('color') color.pop('external_ids') db_color = session.query(Color).get(color['id']) inventory_part_dict['color'] = db_color part = inventory_part_dict.pop('part') db_part = session.query(Part).get(part['part_num']) inventory_part_dict['part'] = db_part inventory.parts.append(InventoryPart(**inventory_part_dict)) session.add(inventory) print(' ok') except ApiException as e: # insert a -not-found- null value moc_data = Moc(set_num=moc) print(' absent') session.add(moc_data) session.commit() return moc_data
from rebrickable.data.models import Set, Theme, Part from rebrickable.data.database import Session session = Session() for set in session.query(Set).filter(): if len(set.inventories) == 5: # the 421-2 set from 1966 for inventory in set.inventories: print(str(len(inventory.parts)) + ' parts in inventory') # parts variants (printed, alt, model alt, patterned) for part in session.query(Part) \ .filter(Part.has_variants).limit(50): print(part) for p in part.printed_variants: print('printed : ' + str(p)) for p in part.alt_variants: print('alt : ' + str(p)) for p in part.mold_variants: print('mold alt : ' + str(p)) for p in part.patterned_variants: print('patterns : ' + str(p)) def print_theme(theme, indent=0): print(' ' * indent + theme.name) for child in theme.children: print_theme(child, indent + 4)
# Used to answer https://bricks.stackexchange.com/q/10233/9843 from sqlalchemy import and_ from rebrickable.data.models import Part, Inventory, InventoryPart, Color, Set from rebrickable.data.database import Session session = Session() parts = session.query(Part) colors = session.query(Color) sets = session.query(Set.set_num).join(Inventory).join(InventoryPart) # Plate, Modified 1 x 2 with Handle on Side - Free Ends part1 = parts.get("2540") # seems to be white # Hinge Plate 1 x 2 Locking with 2 Fingers on Side part2 = parts.get("60471") # in black # Plate 1 X 2 part3 = parts.get("3023") # in transparent black = colors.filter_by(name='Black').one() trans_clear = colors.filter_by(name='Trans-Clear').one() red = colors.filter_by(name='Red').one() part1s = sets.filter(InventoryPart.part == part1).all() part2s = sets.filter( and_(InventoryPart.part == part2, InventoryPart.color == black)).all()