def test_bool(): test_dict = ReadOnlyDict(dict_content) assert bool(test_dict) assert test_dict empty_dict = ReadOnlyDict() assert not len(empty_dict) assert not empty_dict
def test_membership(): test_dict = ReadOnlyDict(dict_content) # check for membership checks and retrieval for key, value in dict_content.items(): assert key in test_dict assert test_dict[key] == value assert test_dict.get(key, -99999) == value
def test_membership(): test_dict = ReadOnlyDict(dict_content) # check for membership checks and retrieval for key, value in dict_content.iteritems(): assert key in test_dict assert test_dict[key] == value assert test_dict.get(key, -99999) == value
def test_readonly_dict_get_existing_key_works_same_as_for_dict(): test_dict = ReadOnlyDict(dict_content) # check for membership checks and retrieval for key, value in dict_content.items(): assert key in test_dict assert test_dict[key] == value assert test_dict.get(key, -99999) == value
def test_readonly_dict_evaluated_to_bool_same_as_collection(): test_dict = ReadOnlyDict(dict_content) assert bool(test_dict) assert test_dict empty_dict = ReadOnlyDict() assert not len(empty_dict) assert not empty_dict
def test_non_existing_keys(): test_dict = ReadOnlyDict(dict_content) # check correct functionality if keys not in dict assert 'INVALID_KEY' not in test_dict assert test_dict.get('INVALID_KEY', -99999) == -99999 with pytest.raises(KeyError, message="Invalid key lookup didn't raise a KeyError", match="'INVALID_KEY'"): # noinspection PyStatementEffect test_dict['INVALID_KEY']
def test_readonly_dict_get_non_existiong_key_works_same_as_for_dict(): test_dict = ReadOnlyDict(dict_content) # check correct functionality if keys not in dict assert 'INVALID_KEY' not in test_dict assert test_dict.get('INVALID_KEY', -99999) == -99999 with pytest.raises(KeyError, match="'INVALID_KEY'"): # noinspection PyStatementEffect test_dict['INVALID_KEY'] pytest.fail("Invalid key lookup didn't raise a KeyError")
def test_non_existing_keys(): test_dict = ReadOnlyDict(dict_content) # check correct functionality if keys not in dict assert 'INVALID_KEY' not in test_dict assert test_dict.get('INVALID_KEY', -99999) == -99999 try: _ = test_dict['INVALID_KEY'] raise AssertionError("Invalid key lookup didn't raise a KeyError") except KeyError: pass
def test_nonhashable_values(): # make sure can't store unhashable items (as heuristic for mutable items) try: _ = ReadOnlyDict({1: [1]}) raise AssertionError("Can store mutable items in dict.") except TypeError: pass
def test_setitem(): test_dict = ReadOnlyDict(dict_content) try: test_dict['INVALID_KEY'] = 1 raise AssertionError("Can add items to the dict.") except TypeError: pass assert 'INVALID_KEY' not in test_dict
def test_setitem(): test_dict = ReadOnlyDict(dict_content) with pytest.raises( TypeError, message='Can add items to the dict.', match="'ReadOnlyDict' object does not support item assignment"): test_dict['INVALID_KEY'] = 1 assert 'INVALID_KEY' not in test_dict
def test_deletion(): test_dict = ReadOnlyDict(dict_content) # check that dict can not be modified try: del test_dict[1] raise AssertionError("Can delete items from the dict.") except TypeError: pass
def test_readonly_dict_set_value_raises_error(): test_dict = ReadOnlyDict(dict_content) with raises( TypeError, match="'ReadOnlyDict' object does not support item assignment"): test_dict['INVALID_KEY'] = 1 pytest.fail('Can add items to the dict.') assert 'INVALID_KEY' not in test_dict
def get_empire_drydocks() -> Mapping[SystemId, Tuple[PlanetId]]: """ Return a map from system ids to planet ids where empire drydocks are located. """ universe = fo.getUniverse() drydocks = {} for pid in BuildingType.SHIPYARD_ORBITAL_DRYDOCK.built_at(): planet = universe.getPlanet(pid) drydocks.setdefault(planet.systemID, []).append(pid) return ReadOnlyDict({k: tuple(v) for k, v in drydocks.items()})
def __update_buildings(self): universe = fo.getUniverse() empire_id = fo.empireID() drydocks = {} for building_id in universe.buildingIDs: building = universe.getBuilding(building_id) if not building: continue if building.buildingTypeName == AIDependencies.BLD_SHIPYARD_ORBITAL_DRYDOCK and building.ownedBy(empire_id): drydocks.setdefault(building.systemID, []).append(building.planetID) self.__drydock_locations = ReadOnlyDict({k: tuple(v) for k, v in drydocks.items()})
def get_empire_drydocks() -> Mapping[SystemId, Tuple[PlanetId]]: """ Return a map from system ids to planet ids where empire drydocks are located. """ universe = fo.getUniverse() empire_id = fo.empireID() drydocks = {} for building_id in universe.buildingIDs: building = universe.getBuilding(building_id) if not building: continue if building.buildingTypeName == AIDependencies.BLD_SHIPYARD_ORBITAL_DRYDOCK and building.ownedBy(empire_id): drydocks.setdefault(building.systemID, []).append(building.planetID) return ReadOnlyDict({k: tuple(v) for k, v in drydocks.items()})
def __init__(self): self.__have_gas_giant = False self.__have_asteroids = False self.__have_ruins = False self.__have_nest = False self.__have_computronium = False self.__best_pilot_rating = 1e-8 self.__medium_pilot_rating = 1e-8 self.__planet_info = {} # map from planet_id to PlanetInfo self.__num_researchers = 0 # population with research focus self.__num_industrialists = 0 # population with industry focus # supply info - negative values indicate jumps away from supply self.__system_supply = {} # map from system_id to supply self.__systems_by_jumps_to_supply = {} # map from supply to list of system_ids self.__empire_planets_by_system = {} # building info self.__drydock_locations = ReadOnlyDict() # map from system id to planet id where empire has a drydock
def get_empire_planets_by_system(self, sys_id=None, include_outposts=True): """ Return dict from system id to planet ids of empire with species. :rtype: ReadOnlyDict[int, list[int]] """ # TODO: as currently used, is duplicative with combo of get_aistate().popCtrSystemIDs if include_outposts not in self.__empire_planets_by_system: empire_id = fo.empireID() empire_planets = (x for x in self.__planet_info.values() if x.owner == empire_id and (x.species_name or include_outposts)) result = {} for x in empire_planets: result.setdefault(x.system_id, []).append(x.pid) self.__empire_planets_by_system[include_outposts] = ReadOnlyDict( {k: tuple(v) for k, v in result.items()} ) if sys_id is not None: return self.__empire_planets_by_system[include_outposts].get(sys_id, tuple()) return self.__empire_planets_by_system[include_outposts]
def test_dict_content(): test_dict = ReadOnlyDict(dict_content) assert test_dict.keys() == dict_content.keys() assert test_dict.values() == dict_content.values() assert test_dict.items() == dict_content.items() assert len(test_dict) == len(dict_content)
def _get_system_planets_map(planet_filter: Callable[[PlanetInfo], bool]) -> Mapping[int, Tuple[int]]: result = {} for planet_info in (planet_info for planet_info in _get_planets_info().values() if planet_filter): result.setdefault(planet_info.system_id, []).append(planet_info.pid) return ReadOnlyDict({k: tuple(v) for k, v in result.items()})
def test_readonly_create_from_dict_with_hashable_values_raises_error(): with raises(TypeError, match="unhashable type: 'list'"): ReadOnlyDict({1: [1]}) pytest.fail('Can store mutable items in dict')
def test_readonly_dict_remove_value_raises_error(): test_dict = ReadOnlyDict(dict_content) with raises(TypeError): del test_dict[1] raise AssertionError("Can delete items from the dict.")
def test_conversion_to_dict(): read_only_dict = ReadOnlyDict(dict_content) normal_dict = dict(read_only_dict) assert len(normal_dict) == len(dict_content) assert list(normal_dict.items()) == list(dict_content.items())
val.planets.add(pid) val.systems.add(planet.systemID) return ret _prerequisites = ReadOnlyDict({ BuildingType.SHIPYARD_ORBITAL_DRYDOCK: BuildingType.SHIPYARD_BASE, BuildingType.SHIPYARD_CON_NANOROBO: BuildingType.SHIPYARD_ORBITAL_DRYDOCK, BuildingType.SHIPYARD_CON_GEOINT: BuildingType.SHIPYARD_ORBITAL_DRYDOCK, BuildingType.SHIPYARD_CON_ADV_ENGINE: BuildingType.SHIPYARD_ORBITAL_DRYDOCK, BuildingType.SHIPYARD_AST_REF: BuildingType.SHIPYARD_AST, BuildingType.SHIPYARD_ORG_ORB_INC: BuildingType.SHIPYARD_BASE, BuildingType.SHIPYARD_ORG_CELL_GRO_CHAMB: BuildingType.SHIPYARD_ORG_ORB_INC, BuildingType.SHIPYARD_ORG_XENO_FAC: BuildingType.SHIPYARD_ORG_ORB_INC, BuildingType.SHIPYARD_ENRG_COMP: BuildingType.SHIPYARD_BASE, BuildingType.SHIPYARD_ENRG_SOLAR: BuildingType.SHIPYARD_ENRG_COMP, # not a technical prerequisite, but it has no purpose without a shipyard BuildingType.NEUTRONIUM_FORGE: BuildingType.SHIPYARD_BASE, })
def test_readonly_dict_created_from_dict_iterates_over_same_keys(): test_dict = ReadOnlyDict(dict_content) assert set(test_dict) == set(dict_content)
def test_readonly_dict_created_from_dict_behaves_the_same(): test_dict = ReadOnlyDict(dict_content) assert set(test_dict.keys()) == set(dict_content.keys()) assert set(test_dict.values()) == set(dict_content.values()) assert set(test_dict.items()) == set(dict_content.items()) assert len(test_dict) == len(dict_content)
def test_nonhashable_values(): # make sure can't store unhashable items (as heuristic for mutable items) with pytest.raises(TypeError, message='Can store mutable items in dict', match="unhashable type: 'list'"): ReadOnlyDict({1: [1]})
def test_setitem(): test_dict = ReadOnlyDict(dict_content) with pytest.raises(TypeError, message='Can add items to the dict.'): test_dict['INVALID_KEY'] = 1 assert 'INVALID_KEY' not in test_dict
def test_str_conversion(): # check bool and str conversions test_dict = ReadOnlyDict(dict_content) assert str(test_dict) == str(dict_content)
def test_readonly_dict_created_from_dict_can_be_converted_to_the_same_dict(): read_only_dict = ReadOnlyDict(dict_content) normal_dict = dict(read_only_dict) assert len(normal_dict) == len(dict_content) assert set(normal_dict.items()) == set(dict_content.items())
def test_dict_content(): test_dict = ReadOnlyDict(dict_content) assert set(test_dict.keys()) == set(dict_content.keys()) assert set(test_dict.values()) == set(dict_content.values()) assert set(test_dict.items()) == set(dict_content.items()) assert len(test_dict) == len(dict_content)