def _(): # *** First, we need to break down # the set into a list of known units. *** unit_ids = set() sets = [self] while sets: set_ids = set() for set_ in sets: unit_ids.update({ member['id'] for member in set_.data.get('members') if member['kind'] == 'unit' }) set_ids.update({ member['id'] for member in set_.data.get('members') if member['kind'] == 'set' }) sets = Set.list_by_entity_ids(set_ids) # *** Second, we need to find all # the required connecting units. *** next_grab, units, unit_requires = unit_ids, [], {} while next_grab: tier_units = Unit.list_by_entity_ids(next_grab) units += tier_units next_grab = set() for unit in tier_units: if 'require_ids' not in unit: continue unit_id = unit['entity_id'] require_ids = unit_requires[unit_id] = \ set(unit['require_ids']) for require_id in require_ids: if require_id in unit_ids: ids = {unit_id} while ids: unit_ids.update(ids) ids = { unit_id for unit_id, require_ids in unit_requires.items() if unit_id not in unit_ids and require_ids & ids } elif require_id not in unit_requires: next_grab.add(require_id) units = [ unit.data for unit in units if unit['entity_id'] in unit_ids ] return units
def get_my_recently_created_units(current_user, db_conn): """ Get the user's most recently created units. """ proposals = get_my_recent_proposals(current_user, db_conn) unit_ids = get_proposal_entities(proposals, 'unit') units = Unit.list_by_entity_ids(db_conn, unit_ids) return units
def test_match_unit_dependents(db_conn, units_table): """ Expect to order units by the number of depending units. """ add_test_set(db_conn, units_table=units_table) units = Unit.list_by_entity_ids(db_conn, ["add", "subtract", "multiply", "divide"]) deps = match_unit_dependents(units) assert len(deps["add"]) == 3 assert len(deps["multiply"]) == 1 assert len(deps["subtract"]) == 1 assert len(deps["divide"]) == 0
def _(): # *** First, we need to break down # the set into a list of known units. *** unit_ids = set() sets = [self] while sets: set_ids = set() for set_ in sets: unit_ids.update({member['id'] for member in set_.data.get('members') if member['kind'] == 'unit'}) set_ids.update({member['id'] for member in set_.data.get('members') if member['kind'] == 'set'}) sets = Set.list_by_entity_ids(set_ids) # *** Second, we need to find all # the required connecting units. *** next_grab, units, unit_requires = unit_ids, [], {} while next_grab: tier_units = Unit.list_by_entity_ids(next_grab) units += tier_units next_grab = set() for unit in tier_units: if 'require_ids' not in unit: continue unit_id = unit['entity_id'] require_ids = unit_requires[unit_id] = \ set(unit['require_ids']) for require_id in require_ids: if require_id in unit_ids: ids = {unit_id} while ids: unit_ids.update(ids) ids = {unit_id for unit_id, require_ids in unit_requires.items() if unit_id not in unit_ids and require_ids & ids} elif require_id not in unit_requires: next_grab.add(require_id) units = [unit.data for unit in units if unit['entity_id'] in unit_ids] return units
def test_order(db_conn, units_table): """ Expect to order units by the number of depending units. """ add_test_set(db_conn, units_table=units_table) units = Unit.list_by_entity_ids(db_conn, ["add", "subtract", "multiply", "divide"]) units = order_units_by_need(units) entity_ids = [unit["entity_id"] for unit in units] assert entity_ids[0] == "add" assert entity_ids[1] in ("subtract", "multiply") assert entity_ids[2] in ("subtract", "multiply") assert entity_ids[3] == "divide"
def test_match_unit_dependents(db_conn, units_table): """ Expect to order units by the number of depending units. """ add_test_set(db_conn, units_table=units_table) units = Unit.list_by_entity_ids([ 'add', 'subtract', 'multiply', 'divide', ]) deps = match_unit_dependents(units) assert len(deps['add']) == 3 assert len(deps['multiply']) == 1 assert len(deps['subtract']) == 1 assert len(deps['divide']) == 0
def test_match_unit_dependents(db_conn, units_table): """ Expect to order units by the number of depending units. """ add_test_subject(db_conn, units_table=units_table) units = Unit.list_by_entity_ids(db_conn, [ 'add', 'subtract', 'multiply', 'divide', ]) deps = match_unit_dependents(units) assert len(deps['add']) == 3 assert len(deps['multiply']) == 1 assert len(deps['subtract']) == 1 assert len(deps['divide']) == 0
def test_order(db_conn, units_table): """ Expect to order units by the number of depending units. """ add_test_set(db_conn, units_table=units_table) units = Unit.list_by_entity_ids([ 'add', 'subtract', 'multiply', 'divide', ]) units = order_units_by_need(units) entity_ids = [unit['entity_id'] for unit in units] assert entity_ids[0] == 'add' assert entity_ids[1] in ('subtract', 'multiply') assert entity_ids[2] in ('subtract', 'multiply') assert entity_ids[3] == 'divide'
def test_order(db_conn, units_table): """ Expect to order units by the number of depending units. """ add_test_subject(db_conn, units_table=units_table) units = Unit.list_by_entity_ids(db_conn, [ 'add', 'subtract', 'multiply', 'divide', ]) units = order_units_by_need(units) entity_ids = [unit['entity_id'] for unit in units] assert entity_ids[0] == 'add' assert entity_ids[1] in ('subtract', 'multiply') assert entity_ids[2] in ('subtract', 'multiply') assert entity_ids[3] == 'divide'