Example #1
0
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
Example #2
0
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
Example #3
0
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
Example #4
0
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
Example #5
0
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
Example #6
0
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']
Example #7
0
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']
Example #8
0
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")
Example #9
0
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_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
Example #11
0
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
Example #12
0
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
Example #13
0
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
Example #14
0
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
Example #15
0
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
Example #16
0
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()})
Example #17
0
 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()})
Example #18
0
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()})
Example #19
0
    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
Example #20
0
    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]
Example #21
0
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)
Example #22
0
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()})
Example #23
0
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')
Example #24
0
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.")
Example #25
0
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())
Example #26
0
            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,
})
Example #27
0
def test_readonly_dict_created_from_dict_iterates_over_same_keys():
    test_dict = ReadOnlyDict(dict_content)
    assert set(test_dict) == set(dict_content)
Example #28
0
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)
Example #29
0
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]})
Example #30
0
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
Example #31
0
def test_str_conversion():
    # check bool and str conversions
    test_dict = ReadOnlyDict(dict_content)
    assert str(test_dict) == str(dict_content)
Example #32
0
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())
Example #33
0
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)