ruff: Fix SIM113 Use `enumerate()` for index variable in `for` loop.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-02-01 16:38:52 -08:00 committed by Tim Abbott
parent f165ba0fb3
commit 53e80c41ea
6 changed files with 12 additions and 34 deletions

View File

@ -634,12 +634,10 @@ def run_parallel_wrapper(
logging.info("Distributing %s items across %s threads", len(full_items), threads) logging.info("Distributing %s items across %s threads", len(full_items), threads)
with ProcessPoolExecutor(max_workers=threads) as executor: with ProcessPoolExecutor(max_workers=threads) as executor:
count = 0 for count, future in enumerate(
for future in as_completed( as_completed(executor.submit(wrapping_function, f, item) for item in full_items), 1
executor.submit(wrapping_function, f, item) for item in full_items
): ):
future.result() future.result()
count += 1
if count % 1000 == 0: if count % 1000 == 0:
logging.info("Finished %s items", count) logging.info("Finished %s items", count)

View File

@ -1,3 +1,4 @@
import itertools
import logging import logging
import os import os
import posixpath import posixpath
@ -754,17 +755,7 @@ def convert_slack_workspace_messages(
zerver_subscription=realm["zerver_subscription"], zerver_subscription=realm["zerver_subscription"],
) )
while True: while message_data := list(itertools.islice(all_messages, chunk_size)):
message_data = []
_counter = 0
for msg in all_messages:
_counter += 1
message_data.append(msg)
if _counter == chunk_size:
break
if len(message_data) == 0:
break
( (
zerver_message, zerver_message,
zerver_usermessage, zerver_usermessage,

View File

@ -1715,9 +1715,8 @@ def export_files_from_s3(
def export_uploads_from_local( def export_uploads_from_local(
realm: Realm, local_dir: Path, output_dir: Path, attachments: List[Attachment] realm: Realm, local_dir: Path, output_dir: Path, attachments: List[Attachment]
) -> None: ) -> None:
count = 0
records = [] records = []
for attachment in attachments: for count, attachment in enumerate(attachments, 1):
# Use 'mark_sanitized' to work around false positive caused by Pysa # Use 'mark_sanitized' to work around false positive caused by Pysa
# thinking that 'realm' (and thus 'attachment' and 'attachment.path_id') # thinking that 'realm' (and thus 'attachment' and 'attachment.path_id')
# are user controlled # are user controlled
@ -1741,8 +1740,6 @@ def export_uploads_from_local(
) )
records.append(record) records.append(record)
count += 1
if count % 100 == 0: if count % 100 == 0:
logging.info("Finished %s", count) logging.info("Finished %s", count)
@ -1831,9 +1828,8 @@ def get_emoji_path(realm_emoji: RealmEmoji) -> str:
def export_emoji_from_local( def export_emoji_from_local(
realm: Realm, local_dir: Path, output_dir: Path, realm_emojis: List[RealmEmoji] realm: Realm, local_dir: Path, output_dir: Path, realm_emojis: List[RealmEmoji]
) -> None: ) -> None:
count = 0
records = [] records = []
for realm_emoji in realm_emojis: for count, realm_emoji in enumerate(realm_emojis, 1):
emoji_path = get_emoji_path(realm_emoji) emoji_path = get_emoji_path(realm_emoji)
# Use 'mark_sanitized' to work around false positive caused by Pysa # Use 'mark_sanitized' to work around false positive caused by Pysa
@ -1862,7 +1858,6 @@ def export_emoji_from_local(
) )
records.append(record) records.append(record)
count += 1
if count % 100 == 0: if count % 100 == 0:
logging.info("Finished %s", count) logging.info("Finished %s", count)

View File

@ -786,12 +786,7 @@ def import_uploads(
bucket_name = settings.S3_AUTH_UPLOADS_BUCKET bucket_name = settings.S3_AUTH_UPLOADS_BUCKET
bucket = get_bucket(bucket_name) bucket = get_bucket(bucket_name)
count = 0 for count, record in enumerate(records, 1):
for record in records:
count += 1
if count % 1000 == 0:
logging.info("Processed %s/%s uploads", count, len(records))
if processing_avatars: if processing_avatars:
# For avatars, we need to rehash the user ID with the # For avatars, we need to rehash the user ID with the
# new server's avatar salt # new server's avatar salt
@ -878,6 +873,9 @@ def import_uploads(
os.makedirs(os.path.dirname(file_path), exist_ok=True) os.makedirs(os.path.dirname(file_path), exist_ok=True)
shutil.copy(orig_file_path, file_path) shutil.copy(orig_file_path, file_path)
if count % 1000 == 0:
logging.info("Processed %s/%s uploads", count, len(records))
if processing_avatars: if processing_avatars:
# Ensure that we have medium-size avatar images for every # Ensure that we have medium-size avatar images for every
# avatar. TODO: This implementation is hacky, both in that it # avatar. TODO: This implementation is hacky, both in that it

View File

@ -15,10 +15,9 @@ def reset_is_private_flag(apps: StateApps, schema_editor: BaseDatabaseSchemaEdit
# zerver/migrations/0100_usermessage_remove_is_me_message.py # zerver/migrations/0100_usermessage_remove_is_me_message.py
# didn't clean the field after removing it. # didn't clean the field after removing it.
i = 0
total = len(user_profile_ids) total = len(user_profile_ids)
print("Setting default values for the new flag...", flush=True) print("Setting default values for the new flag...", flush=True)
for user_id in user_profile_ids: for i, user_id in enumerate(user_profile_ids, 1):
while True: while True:
# Ideally, we'd just do a single database query per user. # Ideally, we'd just do a single database query per user.
# Unfortunately, Django doesn't use the fancy new index on # Unfortunately, Django doesn't use the fancy new index on
@ -39,7 +38,6 @@ def reset_is_private_flag(apps: StateApps, schema_editor: BaseDatabaseSchemaEdit
if count < 1000: if count < 1000:
break break
i += 1
if i % 50 == 0 or i == total: if i % 50 == 0 or i == total:
percent = round((i / total) * 100, 2) percent = round((i / total) * 100, 2)
print(f"Processed {i}/{total} {percent}%", flush=True) print(f"Processed {i}/{total} {percent}%", flush=True)

View File

@ -996,8 +996,7 @@ class EditMessageTest(EditMessageTestCase):
# We reverse the message history view output so that the IDs line up with the above. # We reverse the message history view output so that the IDs line up with the above.
message_history = list(reversed(json_response["message_history"])) message_history = list(reversed(json_response["message_history"]))
i = 0 for i, entry in enumerate(message_history):
for entry in message_history:
expected_entries = {"content", "rendered_content", "topic", "timestamp", "user_id"} expected_entries = {"content", "rendered_content", "topic", "timestamp", "user_id"}
if i in {0, 2, 4}: if i in {0, 2, 4}:
expected_entries.add("prev_topic") expected_entries.add("prev_topic")
@ -1009,7 +1008,6 @@ class EditMessageTest(EditMessageTestCase):
if i in {0, 3}: if i in {0, 3}:
expected_entries.add("prev_stream") expected_entries.add("prev_stream")
expected_entries.add("stream") expected_entries.add("stream")
i += 1
self.assertEqual(expected_entries, set(entry.keys())) self.assertEqual(expected_entries, set(entry.keys()))
self.assert_length(message_history, 7) self.assert_length(message_history, 7)
self.assertEqual(message_history[0]["topic"], "topic 4") self.assertEqual(message_history[0]["topic"], "topic 4")