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.
request_timeout = 90.0 if longpolling else timeout or 15.0
request = {}
req_files = []
request = {
key: val if isinstance(val, str) else json.dumps(val)
for key, val in orig_request.items()
}
for key, val in orig_request.items():
if isinstance(val, str):
request[key] = val
else:
request[key] = json.dumps(val)
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

View file

@ -40,13 +40,8 @@ class ConnectFourModel:
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

View file

@ -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:

View file

@ -68,29 +68,15 @@ class TicTacToeModel:
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 []

View file

@ -55,7 +55,6 @@ class YoutubeHandler:
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]: