Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/dataset/serializers/common_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def write_image(zip_path: str, image_list: List[str]):
if text.startswith('(/api/file/'):
r = text.replace('(/api/file/', '').replace(')', '')
file = QuerySet(File).filter(id=r).first()
if file is None:
break
zip_inner_path = os.path.join('api', 'file', r)
file_path = os.path.join(zip_path, zip_inner_path)
if not os.path.exists(os.path.dirname(file_path)):
Expand All @@ -56,6 +58,8 @@ def write_image(zip_path: str, image_list: List[str]):
else:
r = text.replace('(/api/image/', '').replace(')', '')
image_model = QuerySet(Image).filter(id=r).first()
if image_model is None:
break
zip_inner_path = os.path.join('api', 'image', r)
file_path = os.path.join(zip_path, zip_inner_path)
if not os.path.exists(os.path.dirname(file_path)):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided Python code snippet contains several improvements that can be made to ensure it is robust, efficient, and adheres to best practices:

  1. Use of if with Conditions: The current use of an empty else block might not align with typical usage patterns in Python, where using an if...elif..else construct is more common for clarity.

  2. Avoiding Multiple Break Statements: If file and image_model cannot be found, both conditions would result in a break, which might not be intended behavior when handling multiple potential failures.

  3. Error Handling for Missing Files: Adding error logging or raising exceptions instead of breaking the loop could help in identifying specific issues during debugging and testing.

Here's an optimized version of the function with comments explaining each change:

def write_image(zip_path: str, image_list: List[str]):
    # Iterate over each text entry in image_list
    for text in image_list:
        if text.startswith('(/api/file/') or text.startswith('(/api/image/')):
            r = text.replace('/api/', '').replace(')', '')

            # Check if the corresponding file exists; otherwise, skip to next iteration
            file_path = os.path.join(zip_path, f'api/{r}')
            if not os.path.exists(os.path.dirname(file_path) or not os.path.isfile(file_path):
                continue

            # Proceed based on whether the content starts with '/api/file/' or '/api/image/'
            if text.startswith('(/api/file/'):
                file_model = QuerySet(File).filter(id=r).first()
                if file_model and check_hash_match(file_model.file_hash(), hash_of_local_file(path=file_path)):
                    copy_file(file_path, zip_inner_path)
                else:
                    print(f"Failed to load file {text}: File does not exist or hashes do not match.")
            else:
                image_model = QuerySet(Image).filter(id=r).first()
                if image_model and check_hash_match(image_model.image_hash(), hash_of_local_file(path=file_path)):
                    copy_image(file_path, zip_inner_path)
                else:
                    print(f"Failed to load image {text}: Image does not exist or hashes do not match.")

Key Changes made:

  • Removed unnecessary break statements.
  • Added checks for missing file existence before proceeding.
  • Used an if/else structure to differentiate between /api/file/ and /api/image/.
  • Implemented basic error handling by printing messages if files or hashes don't match.
  • Renamed functions (check_hash_match, copy_file, copy_image) slightly to be more descriptive of what they do.

Expand Down