From 5199c140772cfde93b437a0a190891299db79ca7 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 30 Oct 2023 12:07:35 -0700 Subject: [PATCH] ruff: Fix PERF401 Use a list comprehension to create a transformed list. Signed-off-by: Anders Kaseorg --- zulip/zulip/__init__.py | 15 +++++--------- .../bots/connect_four/controller.py | 7 +------ .../bots/monkeytestit/lib/report.py | 10 +++++----- .../zulip_bots/bots/tictactoe/tictactoe.py | 20 +++---------------- zulip_bots/zulip_bots/bots/youtube/youtube.py | 10 +++++----- 5 files changed, 19 insertions(+), 43 deletions(-) diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index a2307e7cd..0c7c3ce29 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -574,17 +574,12 @@ def do_api_query( # Otherwise, 15s should be plenty of time. request_timeout = 90.0 if longpolling else timeout or 15.0 - request = {} - req_files = [] - - for key, val in orig_request.items(): - if isinstance(val, str): - request[key] = val - else: - request[key] = json.dumps(val) + request = { + key: val if isinstance(val, str) else json.dumps(val) + for key, val in orig_request.items() + } - for f in files: - req_files.append((f.name, f)) + req_files = [(f.name, f) for f in files] self.ensure_session() assert self.session is not None diff --git a/zulip_bots/zulip_bots/bots/connect_four/controller.py b/zulip_bots/zulip_bots/bots/connect_four/controller.py index b8e49a04c..095250c11 100644 --- a/zulip_bots/zulip_bots/bots/connect_four/controller.py +++ b/zulip_bots/zulip_bots/bots/connect_four/controller.py @@ -40,13 +40,8 @@ def validate_move(self, column_number: int) -> bool: return self.current_board[row][column] == 0 def available_moves(self) -> List[int]: - available_moves = [] row = 0 - for column in range(7): - if self.current_board[row][column] == 0: - available_moves.append(column) - - return available_moves + return [column for column in range(7) if self.current_board[row][column] == 0] def make_move( self, move: str, player_number: int, is_computer: bool = False diff --git a/zulip_bots/zulip_bots/bots/monkeytestit/lib/report.py b/zulip_bots/zulip_bots/bots/monkeytestit/lib/report.py index 7cb7f7f50..8db71f483 100644 --- a/zulip_bots/zulip_bots/bots/monkeytestit/lib/report.py +++ b/zulip_bots/zulip_bots/bots/monkeytestit/lib/report.py @@ -98,11 +98,11 @@ def get_enabled_checkers(results: Dict) -> List: :return: A list containing enabled checkers """ checkers = results["enabled_checkers"] - enabled_checkers = [] - for checker in checkers: - if checkers[checker]: # == True/False - enabled_checkers.append(checker) - return enabled_checkers + return [ + checker + for checker in checkers + if checkers[checker] # == True/False + ] def print_enabled_checkers(results: Dict) -> str: diff --git a/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py b/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py index 625402b03..47f5f6f7f 100644 --- a/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py +++ b/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py @@ -68,29 +68,15 @@ def contains_winning_move(self, board: Any) -> bool: def get_locations_of_char(self, board: Any, char: int) -> List[List[int]]: """Gets the locations of the board that have char in them.""" - locations = [] - for row in range(3): - for col in range(3): - if board[row][col] == char: - locations.append([row, col]) - return locations + return [[row, col] for row in range(3) for col in range(3) if board[row][col] == char] def two_blanks(self, triplet: List[Tuple[int, int]], board: Any) -> List[Tuple[int, int]]: """Determines which rows/columns/diagonals have two blank spaces and an 2 already in them. It's more advantageous for the computer to move there. This is used when the computer makes its move.""" - o_found = False - for position in triplet: - if self.get_value(board, position) == 2: - o_found = True - break - - blanks_list = [] + o_found = any(self.get_value(board, position) == 2 for position in triplet) if o_found: - for position in triplet: - if self.get_value(board, position) == 0: - blanks_list.append(position) - + blanks_list = [position for position in triplet if self.get_value(board, position) == 0] if len(blanks_list) == 2: return blanks_list return [] diff --git a/zulip_bots/zulip_bots/bots/youtube/youtube.py b/zulip_bots/zulip_bots/bots/youtube/youtube.py index 3d1a5cbca..3a7a2b101 100644 --- a/zulip_bots/zulip_bots/bots/youtube/youtube.py +++ b/zulip_bots/zulip_bots/bots/youtube/youtube.py @@ -55,7 +55,6 @@ def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> No def search_youtube(query: str, key: str, region: str, max_results: int = 1) -> List[List[str]]: - videos = [] params: Dict[str, Union[str, int]] = { "part": "id,snippet", "maxResults": max_results, @@ -76,10 +75,11 @@ def search_youtube(query: str, key: str, region: str, max_results: int = 1) -> L search_response = r.json() # Add each result to the appropriate list, and then display the lists of # matching videos, channels, and playlists. - for search_result in search_response.get("items", []): - if search_result["id"]["kind"] == "youtube#video": - videos.append([search_result["snippet"]["title"], search_result["id"]["videoId"]]) - return videos + return [ + [search_result["snippet"]["title"], search_result["id"]["videoId"]] + for search_result in search_response.get("items", []) + if search_result["id"]["kind"] == "youtube#video" + ] def get_command_query(message: Dict[str, str]) -> Tuple[Optional[str], str]: