Skip to content

Commit

Permalink
ruff: Fix PERF401 Use a list comprehension to create a transformed list.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Oct 30, 2023
1 parent d546a39 commit 5199c14
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 43 deletions.
15 changes: 5 additions & 10 deletions zulip/zulip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions zulip_bots/zulip_bots/bots/connect_four/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions zulip_bots/zulip_bots/bots/monkeytestit/lib/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 3 additions & 17 deletions zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand Down
10 changes: 5 additions & 5 deletions zulip_bots/zulip_bots/bots/youtube/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]:
Expand Down

0 comments on commit 5199c14

Please sign in to comment.