def get_score(self, game: str, version: int, userid: UserID, songid: int, songchart: int) -> Optional[Score]: # Helper function so we can iterate over all servers for a single card def get_scores_for_card(cardid: str) -> List[Score]: return Parallel.flatten( Parallel.call( [client.get_records for client in self.clients], game, version, APIConstants.ID_TYPE_INSTANCE, [songid, songchart, cardid], )) relevant_cards = self.__get_cardids(userid) if RemoteUser.is_remote(userid): # No need to look up local score for this user scores = Parallel.flatten( Parallel.map( get_scores_for_card, relevant_cards, )) localscore = None else: localscore, scores = Parallel.execute([ lambda: self.music.get_score(game, version, userid, songid, songchart), lambda: Parallel.flatten( Parallel.map( get_scores_for_card, relevant_cards, )), ]) topscore = localscore for score in scores: if int(score['song']) != songid: continue if int(score['chart']) != songchart: continue newscore = self.__format_score(game, version, songid, songchart, score) if topscore is None: # No merging needed topscore = newscore continue topscore = self.__merge_score(game, version, topscore, newscore) return topscore
def test_empty(self) -> None: results = Parallel.execute([]) self.assertEqual(results, []) results = Parallel.map(lambda x: x, []) self.assertEqual(results, []) results = Parallel.call([]) self.assertEqual(results, []) results = Parallel.flatten([]) self.assertEqual(results, [])
def test_map(self) -> None: def fun(x: int) -> int: return x * 2 results = Parallel.map(fun, [1, 2, 3, 4, 5]) self.assertEqual(results, [2, 4, 6, 8, 10])