ruff: Fix PERF401 Use a list comprehension to create a transformed list.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-10-30 12:07:35 -07:00
parent d546a39731
commit 5199c14077
5 changed files with 19 additions and 43 deletions

View file

@ -574,17 +574,12 @@ class Client:
# Otherwise, 15s should be plenty of time. # Otherwise, 15s should be plenty of time.
request_timeout = 90.0 if longpolling else timeout or 15.0 request_timeout = 90.0 if longpolling else timeout or 15.0
request = {} request = {
req_files = [] key: val if isinstance(val, str) else json.dumps(val)
for key, val in orig_request.items()
}
for key, val in orig_request.items(): req_files = [(f.name, f) for f in files]
if isinstance(val, str):
request[key] = val
else:
request[key] = json.dumps(val)
for f in files:
req_files.append((f.name, f))
self.ensure_session() self.ensure_session()
assert self.session is not None assert self.session is not None

View file

@ -40,13 +40,8 @@ class ConnectFourModel:
return self.current_board[row][column] == 0 return self.current_board[row][column] == 0
def available_moves(self) -> List[int]: def available_moves(self) -> List[int]:
available_moves = []
row = 0 row = 0
for column in range(7): return [column for column in range(7) if self.current_board[row][column] == 0]
if self.current_board[row][column] == 0:
available_moves.append(column)
return available_moves
def make_move( def make_move(
self, move: str, player_number: int, is_computer: bool = False self, move: str, player_number: int, is_computer: bool = False

View file

@ -98,11 +98,11 @@ def get_enabled_checkers(results: Dict) -> List:
:return: A list containing enabled checkers :return: A list containing enabled checkers
""" """
checkers = results["enabled_checkers"] checkers = results["enabled_checkers"]
enabled_checkers = [] return [
for checker in checkers: checker
if checkers[checker]: # == True/False for checker in checkers
enabled_checkers.append(checker) if checkers[checker] # == True/False
return enabled_checkers ]
def print_enabled_checkers(results: Dict) -> str: def print_enabled_checkers(results: Dict) -> str:

View file

@ -68,29 +68,15 @@ class TicTacToeModel:
def get_locations_of_char(self, board: Any, char: int) -> List[List[int]]: def get_locations_of_char(self, board: Any, char: int) -> List[List[int]]:
"""Gets the locations of the board that have char in them.""" """Gets the locations of the board that have char in them."""
locations = [] return [[row, col] for row in range(3) for col in range(3) if board[row][col] == char]
for row in range(3):
for col in range(3):
if board[row][col] == char:
locations.append([row, col])
return locations
def two_blanks(self, triplet: List[Tuple[int, int]], board: Any) -> List[Tuple[int, int]]: 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 """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.""" for the computer to move there. This is used when the computer makes its move."""
o_found = False o_found = any(self.get_value(board, position) == 2 for position in triplet)
for position in triplet:
if self.get_value(board, position) == 2:
o_found = True
break
blanks_list = []
if o_found: if o_found:
for position in triplet: blanks_list = [position for position in triplet if self.get_value(board, position) == 0]
if self.get_value(board, position) == 0:
blanks_list.append(position)
if len(blanks_list) == 2: if len(blanks_list) == 2:
return blanks_list return blanks_list
return [] return []

View file

@ -55,7 +55,6 @@ class YoutubeHandler:
def search_youtube(query: str, key: str, region: str, max_results: int = 1) -> List[List[str]]: def search_youtube(query: str, key: str, region: str, max_results: int = 1) -> List[List[str]]:
videos = []
params: Dict[str, Union[str, int]] = { params: Dict[str, Union[str, int]] = {
"part": "id,snippet", "part": "id,snippet",
"maxResults": max_results, "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() search_response = r.json()
# Add each result to the appropriate list, and then display the lists of # Add each result to the appropriate list, and then display the lists of
# matching videos, channels, and playlists. # matching videos, channels, and playlists.
for search_result in search_response.get("items", []): return [
if search_result["id"]["kind"] == "youtube#video": [search_result["snippet"]["title"], search_result["id"]["videoId"]]
videos.append([search_result["snippet"]["title"], search_result["id"]["videoId"]]) for search_result in search_response.get("items", [])
return videos if search_result["id"]["kind"] == "youtube#video"
]
def get_command_query(message: Dict[str, str]) -> Tuple[Optional[str], str]: def get_command_query(message: Dict[str, str]) -> Tuple[Optional[str], str]: