def processResponse(self, r): self.json = r.text j = loads(r.text) if isinstance(j, dict): self.__dict__.update() elif isinstance(j, list): map(lambda d: self.__dict__.update(d), j) else: raise ValueError(one_line(r.text))
def detect_and_load(data_str): json_data = load_JSON(data_str) if json_data != None: return read_JSON(json_data) xml_data = load_XML(data_str) if xml_data != None: return read_XML(xml_data) if is_NFG(data_str): return read_NFG(data_str) if is_NE(data_str): return read_NE(data_str) raise IOError(one_line("could not detect format: " + data_str, 71))
def regret(game, prof, role=None, strategy=None, deviation=None, bound=False): if role == None and len(game.roles) == 1: role = game.roles[0] #in symmetric games we can deduce the role if is_pure_profile(prof): return profile_regret(game, prof, role, strategy, deviation, bound) if is_mixture_array(prof): return mixture_regret(game, prof, role, deviation, bound) if is_mixed_profile(prof): return mixture_regret(game, game.toArray(prof), role, deviation, bound) if is_profile_array(prof): return profile_regret(game, game.toProfile(prof), role, strategy, \ deviation, bound) raise TypeError(one_line("unrecognized profile type: " + str(prof), 69))
def toArray(self, prof): if is_mixed_profile(prof): a = self.zeros(dtype=float) elif is_pure_profile(prof): a = self.zeros(dtype=int) else: raise TypeError(one_line("unrecognized profile type: " + \ str(prof), 71)) for role in prof.keys(): i = self.index(role) for strategy in prof[role].keys(): j = self.index(role, strategy) a[i,j] = prof[role][strategy] return a
def read(source): if isinstance(source, basestring): try: #assume source is a filename or url u = urlopen(source) data = u.read() u.close() except: #assume source is already a data string data = source elif hasattr(source, 'read'): #source is file-like data = source.read() source.close() else: raise IOError(one_line("could not read: " + str(source), 71)) return detect_and_load(data)
def read_game_JSON(gameJSON): if "players" in gameJSON and "strategies" in gameJSON: return read_GA_game(gameJSON) elif len(gameJSON["profiles"]) == 0: return Game(*parse_roles(gameJSON["roles"])) elif "symmetry_groups" in gameJSON["profiles"][0]: return read_game_JSON_v3(gameJSON) elif "observations" in gameJSON["profiles"][0]: if "players" in gameJSON["profiles"][0]["observations"][0][ \ "symmetry_groups"][0]: return read_game_JSON_v3_players(gameJSON) else: return read_game_JSON_v3_samples(gameJSON) elif "strategy_array" in gameJSON["roles"][0]: return read_game_JSON_old(gameJSON) elif "strategies" in gameJSON["roles"][0]: return read_game_JSON_v2(gameJSON) else: raise IOError(one_line("invalid game JSON: " + str(gameJSON), 71))
def __repr__(self): return join(map(lambda p: one_line(repr(p[0]) + ": " + repr(p[1])), \ self.__dict__.items()), "\n")