Skip to content

Commit 5199c14

Browse files
committed
ruff: Fix PERF401 Use a list comprehension to create a transformed list.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent d546a39 commit 5199c14

File tree

5 files changed

+19
-43
lines changed

5 files changed

+19
-43
lines changed

zulip/zulip/__init__.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -574,17 +574,12 @@ def do_api_query(
574574
# Otherwise, 15s should be plenty of time.
575575
request_timeout = 90.0 if longpolling else timeout or 15.0
576576

577-
request = {}
578-
req_files = []
579-
580-
for key, val in orig_request.items():
581-
if isinstance(val, str):
582-
request[key] = val
583-
else:
584-
request[key] = json.dumps(val)
577+
request = {
578+
key: val if isinstance(val, str) else json.dumps(val)
579+
for key, val in orig_request.items()
580+
}
585581

586-
for f in files:
587-
req_files.append((f.name, f))
582+
req_files = [(f.name, f) for f in files]
588583

589584
self.ensure_session()
590585
assert self.session is not None

zulip_bots/zulip_bots/bots/connect_four/controller.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@ def validate_move(self, column_number: int) -> bool:
4040
return self.current_board[row][column] == 0
4141

4242
def available_moves(self) -> List[int]:
43-
available_moves = []
4443
row = 0
45-
for column in range(7):
46-
if self.current_board[row][column] == 0:
47-
available_moves.append(column)
48-
49-
return available_moves
44+
return [column for column in range(7) if self.current_board[row][column] == 0]
5045

5146
def make_move(
5247
self, move: str, player_number: int, is_computer: bool = False

zulip_bots/zulip_bots/bots/monkeytestit/lib/report.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ def get_enabled_checkers(results: Dict) -> List:
9898
:return: A list containing enabled checkers
9999
"""
100100
checkers = results["enabled_checkers"]
101-
enabled_checkers = []
102-
for checker in checkers:
103-
if checkers[checker]: # == True/False
104-
enabled_checkers.append(checker)
105-
return enabled_checkers
101+
return [
102+
checker
103+
for checker in checkers
104+
if checkers[checker] # == True/False
105+
]
106106

107107

108108
def print_enabled_checkers(results: Dict) -> str:

zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,15 @@ def contains_winning_move(self, board: Any) -> bool:
6868

6969
def get_locations_of_char(self, board: Any, char: int) -> List[List[int]]:
7070
"""Gets the locations of the board that have char in them."""
71-
locations = []
72-
for row in range(3):
73-
for col in range(3):
74-
if board[row][col] == char:
75-
locations.append([row, col])
76-
return locations
71+
return [[row, col] for row in range(3) for col in range(3) if board[row][col] == char]
7772

7873
def two_blanks(self, triplet: List[Tuple[int, int]], board: Any) -> List[Tuple[int, int]]:
7974
"""Determines which rows/columns/diagonals have two blank spaces and an 2 already in them. It's more advantageous
8075
for the computer to move there. This is used when the computer makes its move."""
8176

82-
o_found = False
83-
for position in triplet:
84-
if self.get_value(board, position) == 2:
85-
o_found = True
86-
break
87-
88-
blanks_list = []
77+
o_found = any(self.get_value(board, position) == 2 for position in triplet)
8978
if o_found:
90-
for position in triplet:
91-
if self.get_value(board, position) == 0:
92-
blanks_list.append(position)
93-
79+
blanks_list = [position for position in triplet if self.get_value(board, position) == 0]
9480
if len(blanks_list) == 2:
9581
return blanks_list
9682
return []

zulip_bots/zulip_bots/bots/youtube/youtube.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> No
5555

5656

5757
def search_youtube(query: str, key: str, region: str, max_results: int = 1) -> List[List[str]]:
58-
videos = []
5958
params: Dict[str, Union[str, int]] = {
6059
"part": "id,snippet",
6160
"maxResults": max_results,
@@ -76,10 +75,11 @@ def search_youtube(query: str, key: str, region: str, max_results: int = 1) -> L
7675
search_response = r.json()
7776
# Add each result to the appropriate list, and then display the lists of
7877
# matching videos, channels, and playlists.
79-
for search_result in search_response.get("items", []):
80-
if search_result["id"]["kind"] == "youtube#video":
81-
videos.append([search_result["snippet"]["title"], search_result["id"]["videoId"]])
82-
return videos
78+
return [
79+
[search_result["snippet"]["title"], search_result["id"]["videoId"]]
80+
for search_result in search_response.get("items", [])
81+
if search_result["id"]["kind"] == "youtube#video"
82+
]
8383

8484

8585
def get_command_query(message: Dict[str, str]) -> Tuple[Optional[str], str]:

0 commit comments

Comments
 (0)