Skip to content
Open
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
23 changes: 13 additions & 10 deletions Cachyos/Scripts/WIP/gphotos/Splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@ def create_new_folder(root_folder, folder_name):

def get_latest_group_info(photos_folder):
"""Finds the highest numbered Group_N folder under photos_folder and its size."""
if not os.path.exists(photos_folder):
return 1, None, 0

max_group_num = 0
latest_group_folder = None

if os.path.exists(photos_folder):
for entry in os.scandir(photos_folder):
if entry.name.startswith("Group_") and entry.is_dir():
try:
num = int(entry.name.split("_")[1])
if num > max_group_num:
max_group_num = num
latest_group_folder = entry.path
except (ValueError, IndexError):
continue
for entry in os.scandir(photos_folder):
if not (entry.name.startswith("Group_") and entry.is_dir()):
continue
try:
num = int(entry.name.split("_")[1])
if num > max_group_num:
max_group_num = num
latest_group_folder = entry.path
except (ValueError, IndexError):
continue
Comment on lines +37 to +45
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While the refactoring to use a guard clause is a good improvement, the logic for parsing the group number could be made more robust and self-documenting by using a regular expression. This avoids using a try-except block for control flow and makes the expected format of the directory name more explicit.

You would need to add import re at the top of the file.

Suggested change
if not (entry.name.startswith("Group_") and entry.is_dir()):
continue
try:
num = int(entry.name.split("_")[1])
if num > max_group_num:
max_group_num = num
latest_group_folder = entry.path
except (ValueError, IndexError):
continue
match = re.match(r"Group_(\d+)", entry.name)
if not (entry.is_dir() and match):
continue
num = int(match.group(1))
if num > max_group_num:
max_group_num = num
latest_group_folder = entry.path


if max_group_num == 0 or latest_group_folder is None:
return 1, None, 0
Expand Down
Loading