Code Quality improvement / Refactoring: introduce DrawTreeOptions dataclass and extract _try_pdf_to_png helper#53
Open
sruju333 wants to merge 1 commit intogambitproject:mainfrom
Open
Conversation
…_png helper - Add DrawTreeOptions dataclass to consolidate the 11-parameter signature shared across generate_tikz, generate_pdf, generate_tex, generate_png, and draw_tree (Introduce Parameter Object technique) - Extract PDF-to-PNG conversion logic from generate_png into a private _try_pdf_to_png helper, replacing ~50 lines of nested try/except with a single readable call (Extract Method technique)
Author
|
Hello @edwardchalstrey1 - I just wanted to contribute a little more while waiting, so I decided to work on some code quality improvements in draw_tree repo! This one has two small refactors to core.py:
Both are additive and don't touch any existing functionality. |
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.
Refactoring
This PR introduces two code quality improvements to core.py based on standard refactoring techniques, submitted as part of my GSoC '26 application.
Introduce Parameter Object - DrawTreeOptions dataclass
generate_tikz, generate_pdf, generate_tex, generate_png, and draw_tree all share an identical 11-parameter signature, meaning any future addition (such as a new display option) requires the same change in five places. This PR introduces a DrawTreeOptions dataclass to consolidate those parameters in one place. Existing function signatures are untouched - this is additive only, laying the groundwork for a cleaner API going forward.
Extract Method - _try_pdf_to_png helper
generate_png contained ~50 lines of nested try/except blocks inline, making it hard to follow the function's overall logic. The three conversion attempts (ImageMagick → Ghostscript → pdftoppm) have been extracted into a private _try_pdf_to_png helper with a clear return value (True/False), reducing the call site to a single readable line. No logic has changed.