def change_state(state): rows = 9 columns = 9 list_S = list() list_Q = list() list_H = list() new_dict = {} list_Q_new = list() for pos, value in state.items(): new_dict[pos] = value if str(value[0]) == 'H': list_H = list_H + [pos] if str(value[0]) == 'S': list_S = list_S + [pos] if str(value[0]) == 'Q': list_Q = list_Q + [pos] for pos, value in state.items(): if value == ('Q', None): new_dict[pos] = ('Q', 0) list_Q_new = list_Q_new + [pos] list1 = list() for i in list_S: if int(i[0] + 1) < rows and (int(i[0] + 1), i[1]) in list_H: list1 = list1 + [(int(i[0] + 1), i[1])] if int(i[0]) != 0 and (int(i[0] - 1), i[1]) in list_H: list1 = list1 + [(int(i[0] - 1), i[1])] if int(i[1] + 1) < columns and (i[0], int(i[1] + 1)) in list_H: list1 = list1 + [(i[0], int(i[1] + 1))] if int(i[1]) != 0 and (i[0], int(i[1] - 1)) in list_H: list1 = list1 + [(i[0], int(i[1] - 1))] for a in list1: new_dict[a] = ('S', 0) if a in list_H: list_H.remove(a) list_S.append(a) for j in list(list_S): if j not in list1: temp = int(new_dict[j][1]) if int(temp + 1) >= 3: new_dict[j] = ('H', 0) list_H.append(j) list_S.remove(j) else: new_dict[j] = ('S', int(temp + 1)) for p in list_Q: if p not in list_Q_new: temp = int(new_dict[p][1]) if int(temp + 1) >= 2: new_dict[p] = ('H', 0) list_H.append(p) list_Q.remove(p) else: new_dict[p] = ('Q', int(temp + 1)) return utils.hashabledict(new_dict)
def change_state(state): S_coordinates = [] Q_coordinates = [] H_coordinates = [] new_dict = {} rows = 9 columns = 9 Q_coordinatesNew = [] for coordinates, value in state.items(): new_dict[coordinates] = value if 'H' in value: H_coordinates = H_coordinates + [coordinates] if 'S' in value: S_coordinates = S_coordinates + [coordinates] if 'Q' in value: Q_coordinates = Q_coordinates + [coordinates] for coordinates, value in state.items(): if value == ('Q', None): new_dict[coordinates] = ('Q', 0) Q_coordinatesNew = Q_coordinatesNew + [coordinates] list1 = list() for i in S_coordinates: if int(i[0] + 1) < rows and (int(i[0] + 1), i[1]) in H_coordinates: list1 = list1 + [(int(i[0] + 1), i[1])] if int(i[0]) != 0 and (int(i[0] - 1), i[1]) in H_coordinates: list1 = list1 + [(int(i[0] - 1), i[1])] if int(i[1] + 1) < columns and (i[0], int(i[1] + 1)) in H_coordinates: list1 = list1 + [(i[0], int(i[1] + 1))] if int(i[1]) != 0 and (i[0], int(i[1] - 1)) in H_coordinates: list1 = list1 + [(i[0], int(i[1] - 1))] for a in list1: new_dict[a] = ('S', 0) if a in H_coordinates: H_coordinates.remove(a) S_coordinates.append(a) for j in list(S_coordinates): if j not in list1: temp = int(new_dict[j][1]) if int(temp + 1) >= 3: new_dict[j] = ('H', 0) H_coordinates.append(j) S_coordinates.remove(j) else: new_dict[j] = ('S', int(temp + 1)) for p in Q_coordinates: if p not in Q_coordinatesNew: temp = int(new_dict[p][1]) if int(temp + 1) >= 2: new_dict[p] = ('H', 0) H_coordinates.append(p) Q_coordinates.remove(p) else: new_dict[p] = ('Q', int(temp + 1)) return utils.hashabledict(new_dict)
async def add_to_cache(path, kwargs, data): cache_time_seconds = cache_paths[path] if cache_time_seconds not in caches: caches[cache_time_seconds] = {} if path not in caches[cache_time_seconds]: caches[cache_time_seconds][path] = {} caches[cache_time_seconds][path][hashabledict(kwargs)] = data if path == 'player' and 'name' in kwargs: uuid = data['player']['uuid'] await add_to_cache('player', {'uuid': uuid}, data)
async def make_request(path, key, fast=False, only_cached=False, lazy=False, **kwargs): global caches original_kwargs = kwargs for cache_time_seconds in caches: for cache_path in caches[cache_time_seconds]: if hashabledict( original_kwargs) in caches[cache_time_seconds][cache_path]: if cache_path == path: return caches[cache_time_seconds][cache_path][hashabledict( original_kwargs)] if lazy: return keys_joined = key keys = keys_joined.split(',') if len(keys) > 1: key = await apikeys.find_working_key(keys) if key: kwargs = {'key': key, **kwargs} async with aiohttp.ClientSession() as s: async with s.get(f'https://api.hypixel.net/{path}', params=kwargs) as r: data = await r.json() if data.get('throttle'): key_usage = float('inf') apikeys.set_usage(key, key_usage) if 'key' in kwargs: del kwargs['key'] return await make_request(path, keys_joined, **kwargs) else: apikeys.add_one(key) if 'player' in data and data['player'] == None: raise errors.InvalidUser() if path in cache_paths: await add_to_cache(path, original_kwargs, data) return data
def do_change(state): list_S, list_Q, list_H = list(), list(), list() result = dict() r, c = 9, 9 newQ = list() l1 = list() for k, v in state.items(): if v[0] == 'S': list_S.append(k) if v[0] == 'Q': list_Q.append(k) if v[0] == 'H': list_H.append(k) result[k] = v for i in list_S: l1.append(decide_neighbors(i, r, c, list_H)) for i in list_S: if i not in l1: tmp1 = result[i][1] if tmp1 >= 2: result[i] = ('H', 0) list_H.append(i) list_S.remove(i) else: result[i] = ('S', tmp1 + 1) for i in l1: result[i] = ('S', 0) if i in list_H: list_H.remove(i) list_S.append(i) for k, v in state.items(): if v == ('Q', None): newQ.append(k) result[k] = ('Q', 0) for i in list_Q: if i not in newQ: tmp2 = result[i][1] if tmp2 >= 1: result[i] = ('H', 0) list_H.append(i) list_Q.remove(i) else: result[i] = ('Q', tmp2 + 1) return utils.hashabledict(result)
def result(self, state, action): new_state = hashabledict(state) # copy to prevent hash issues new_state[action[0]] = action[1] return new_state
def __init__(self, initial=None, goal=None, decoder=None): super().__init__(initial or hashabledict(), goal) self.decoder = decoder
def __init__(self, initial=None, goal=None, decoder=None): self.initial = initial or hashabledict() self.decoder = decoder
def __hash__(self): return hash(hashabledict(self._grid))