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
Conversation
Рефакторинг: переписал код асинхронно, сократил объём, добавил аннотаци
If you really need it, maybe I'll rewrite it in Go and compile later.
…y/GFL-Character-Dialogue-Extract into feature/new-improvements
…o the repository. Check back later!
A GUI in the form of a browser site has been added!
Author
|
Well, I actually added GUI (it's a local website, though), but I also kept the console version. You wanted GUI, right? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.