Repository navigation
R23-T058: Refactor resistor_color_code.py to share colour lookup and validation helpers - #68
Open
code-paul-creator wants to merge 1 commit into
Open
code-paul-creator wants to merge 1 commit into
code-paul-creator wants to merge 1 commit into
Conversation
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.
Description
Refactors
electronics/resistor_color_code.pywithout changing behaviour: the repeated "look the colour up in this band's table or raise ValueError" code in four functions is replaced by one shared helper, and the nested validation / resistance code is flattened. An equivalence test compares the original and refactored code to prove nothing changed.Related Issue
Fixes: #58
Type of Change
Changes Made
get_color_value(color, color_values, band_name)(with 2 doctests) and used it inget_significant_digits,get_multiplier,get_tolerance,get_temperature_coeffecient. Error messages are unchanged.check_validity: guard clauses instead of nested if/else (nesting depth 4 -> 2).calculate_resistance: guard clause, removedNoneplaceholders, fixed local typoresisitance.raisestatements kept.work/R23-T058/REFACTOR.md,equivalence_test.py, and before/after complexity and coverage output.Screenshots / Recordings
Not applicable (no UI changes).
Testing
python -m pytest --doctest-modules electronics/resistor_color_code.py-> 26 passedpython -m pytest work/R23-T058/equivalence_test.py-> 60 passed (original vs refactored: same results and same exception type and message on thousands of inputs and every raise path)ruff check electronics/resistor_color_code.py-> all checks passedChecklist
Additional Notes
check_validitystays at complexity 5 (its three error conditions and the colour loop are inherent), so the worst score is unchanged;calculate_resistance, the longest function, drops from 5 to 3. Theraise ValueError("Input is invalid")at the end ofcalculate_resistanceis unreachable (check_validityraises first); it was kept so the 10 raise paths stay identical. Details inwork/R23-T058/REFACTOR.md.