Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Default Routine #15

Merged
merged 10 commits into from
May 18, 2024
Merged

Refactor Default Routine #15

merged 10 commits into from
May 18, 2024

Conversation

rjwignar
Copy link
Owner

@rjwignar rjwignar commented May 18, 2024

This fixes #13

Summary of Changes

  • Removed unnecessary print() statements
  • Extracted folder creation logic to createFolder()
  • Extracted summary display logic to dictionarySummary()
  • Added comments

The main refactoring was done by adding the fileCategories dictionary and populating it with addFileCategory:

fileCategories = {}
def addFileCategory(categoryName, extensions):
    fileCategories[categoryName] = {"folderName": f"{categoryName}", "extensions" : extensions, "fileCount" : 0, "movedCount" : 0}

Explanation for Dictionary Approach

Before these changes, any addition/removal of file categories would require adding/removing lines from this if/else block and reusing code lines:

    # initialize counts
    videoCount = 0
    movedVideosCount = 0
    textCount = 0
    movedTextFilesCount = 0
                        elif os.path.splitext(entry.name)[1] in videos:
                            videoCount+=1
                            createFolder(target_path, "videos")
                            # move file to videos folder
                            oldPath = os.path.join(target_path, entry.name)
                            newPath = os.path.join(target_path, "videos", entry.name)
                            # attempt rename
                            os.rename(oldPath, newPath)
                            movedVideosCount+=1
                        elif os.path.splitext(entry.name)[1] in text:
                            textCount+=1
                            createFolder(target_path, "textFiles")
                            # move file to textFiles folder
                            oldPath = os.path.join(target_path, entry.name)
                            newPath = os.path.join(target_path, "textFiles", entry.name)
                            os.rename(oldPath, newPath)
                            movedTextFilesCount+=1
                         # reuse above code for new file category

If enough categories are added, the if/else block can become difficult to maintain.
Additionally, since we're reusing common logic, it is easy to make a mistake when adding/removing logic in an el(if) block.

After these changes, it's easier to maintain file categories, and file counts are handled when adding to the dictionary:

    # Populate fileCategories with category and extensions
    addFileCategory("images", imageExtensions) # "fileCount" : 0, "movedCount" : 0
    addFileCategory("videos", videoExtensions)   # "fileCount" : 0, "movedCount" : 0
    addFileCategory("textFiles", textExtensions)   # "fileCount" : 0, "movedCount" : 0

Additionally, when a file category is added/removed from the codebase, no changes are required to the file sorting logic,
as the logic for moving any files (regardless of cateogory) to their appropriate folder is handled by this block:

                        # Extract file extension from entry
                        fileExtension = os.path.splitext(entry.name)[1]
                        # Search for extension match in fileCategories
                        for fileType, properties in fileCategories.items():
                            if fileExtension in properties['extensions']:
                                properties['fileCount'] +=1
                                createFolder(target_path, fileType)
                                oldPath = os.path.join(target_path, entry.name)
                                newPath = os.path.join(target_path, fileType, entry.name)
                                # Move file into appropriate folder
                                os.rename(oldPath, newPath)
                                properties['movedCount'] +=1

@rjwignar rjwignar self-assigned this May 18, 2024
@rjwignar rjwignar merged commit f3cdec2 into main May 18, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor default behaviour
1 participant