Skip to content

Feature/new improvements - Refactor to Asynchronous & Concise Code with Improved Error Handling and Flexible Character Name Lookup#1

Open
itsqwint wants to merge 14 commits intoOreoJuice:mainfrom
itsqwint:feature/new-improvements
Open

Feature/new improvements - Refactor to Asynchronous & Concise Code with Improved Error Handling and Flexible Character Name Lookup#1
itsqwint wants to merge 14 commits intoOreoJuice:mainfrom
itsqwint:feature/new-improvements

Conversation

@itsqwint
Copy link
Copy Markdown

Refactor to Asynchronous & Concise Code with Improved Error Handling and Flexible Character Name Lookup

This pull request replaces the original synchronous extraction code with a cleaner, asynchronous version. The new code is roughly half the size (~122 lines vs. 211 lines) and is designed to be faster (around 2.5× speedup in extraction time) while improving readability and maintainability. Below is a summary of the key changes:

Asynchronous File Operations:
All file I/O operations (search, read, and write) have been migrated to use aiofiles and asyncio constructs. This change allows the program to efficiently handle multiple files without blocking the main thread, which is especially beneficial when the input folder contains many text files.

Improved Character Name Handling:

The function now converts both user input and dictionary keys to lowercase for comparison.

It also supports partial matches (e.g., entering "usp" will match "USP Compact").

The original casing from the dictionary is preserved in the output (e.g., the user input "famas" will yield "Fr FAMAS").

Enhanced Error Handling:
The directory creation function (make_dir) now handles specific exceptions (e.g., FileExistsError, PermissionError, OSError) with clear, themed messages. This provides better feedback to the user in case of errors without crashing the program.

Modular & Readable Structure:
The code is organized into small, well-documented asynchronous functions that each handle a specific task in the workflow:

search_files(): Asynchronously searches for .txt files.

read_files(): Reads file contents concurrently.

extract_lines(): Processes the text to extract lines for the specified character, removing unwanted symbols.

write_files(): Writes the extracted dialogue to both TXT and CSV formats using asynchronous and threaded I/O respectively.

task_scheduler(): Orchestrates the workflow: fixes character names, searches files, extracts dialogue, and writes output.

continue_main() and make_dir(): Handle user prompting and directory setup respectively.

Consistent Theming & Messaging:
The code retains a fun, military-inspired theme in its print messages (e.g., "Commander!", "Thank you, Commander!"), making the user experience engaging without compromising functionality.

Why This Is Beneficial:

Performance: The asynchronous approach significantly speeds up file I/O operations compared to the original synchronous version.

Maintainability: With a modular design and clear comments, the code is easier to understand, modify, and extend.

User Experience: Better error handling and clear messaging improve the overall usability of the program.

Flexibility: The enhanced character name lookup makes the program more robust against various user inputs (different cases, partial matches, etc.).

Additional Notes:

The input() function remains synchronous, which is acceptable for our console-based use case. Future work might consider an asynchronous input solution (e.g., using aioconsole), but the current implementation suffices given that the program naturally waits for user input.

This refactor is intended to serve as a solid base for further enhancements. If there are additional features or scaling needs in the future, the code’s modular nature will facilitate such updates.

@itsqwint
Copy link
Copy Markdown
Author

Well, I actually added GUI (it's a local website, though), but I also kept the console version. You wanted GUI, right?

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.

1 participant