Refactor: Replace magic numbers with constants in ScienceLab#277
Open
TejasAnalyst wants to merge 2 commits intofossasia:mainfrom
Open
Refactor: Replace magic numbers with constants in ScienceLab#277TejasAnalyst wants to merge 2 commits intofossasia:mainfrom
TejasAnalyst wants to merge 2 commits intofossasia:mainfrom
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors ScienceLab to replace inline magic numbers in temperature calculation and CTMU ADC conversion with named constants and a calibration dictionary, improving readability and maintainability without changing behavior. Class diagram for ScienceLab temperature and CTMU refactorclassDiagram
class ScienceLabModule {
<<module>>
+float ADC_VMAX
+int ADC_RESOLUTION
+dict TEMP_CALIB
}
class ScienceLab {
+temperature float
+_get_ctmu_voltage(channel int, current_range int, tgen bool) float
}
ScienceLabModule <.. ScienceLab : uses
class TEMP_CALIB_entry {
+float offset
+float slope
}
ScienceLabModule "1" --> "*" TEMP_CALIB_entry : maps_current_source
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
temperature, usingTEMP_CALIB.get(cs)without a fallback will raise an exception ifcsever changes to an unsupported value; consider either validatingcsor using a default/explicit error branch to make failures clearer. - The new constants (
ADC_VMAX,ADC_RESOLUTION,TEMP_CALIB) are defined at module level; if these are tightly coupled toScienceLab, consider making them class attributes or namespacing them to avoid accidental reuse or modification elsewhere. - The inline comments in
TEMP_CALIBare currently just#placeholders; either remove them or replace them with brief descriptions (e.g., units or source of calibration) to clarify the meaning ofoffsetandslope.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `temperature`, using `TEMP_CALIB.get(cs)` without a fallback will raise an exception if `cs` ever changes to an unsupported value; consider either validating `cs` or using a default/explicit error branch to make failures clearer.
- The new constants (`ADC_VMAX`, `ADC_RESOLUTION`, `TEMP_CALIB`) are defined at module level; if these are tightly coupled to `ScienceLab`, consider making them class attributes or namespacing them to avoid accidental reuse or modification elsewhere.
- The inline comments in `TEMP_CALIB` are currently just `#` placeholders; either remove them or replace them with brief descriptions (e.g., units or source of calibration) to clarify the meaning of `offset` and `slope`.
## Individual Comments
### Comment 1
<location> `pslab/sciencelab.py:66-67` </location>
<code_context>
- return (760 - V * 1000) / 1.56 # current source = 3
+
+ # Clean lookup from global constants
+ cal = TEMP_CALIB.get(cs)
+ return (cal["offset"] - V * 1000) / cal["slope"]
def _get_ctmu_voltage(self, channel: int, current_range: int, tgen: bool = True):
</code_context>
<issue_to_address>
**issue (bug_risk):** Consider handling the case where `cs` is not present in `TEMP_CALIB` to avoid a runtime error.
Because `cs` is currently hard-coded to 3 this is safe today, but the new dict lookup changes previously exhaustive handling into something that can return `None`, which would cause a `TypeError` when subscripting `cal`. Using `TEMP_CALIB[cs]` (letting a `KeyError` surface) or adding explicit validation/fallback (e.g., clear error or default calibration) would make failures more predictable if `cs` is later parameterized or modified elsewhere.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Author
|
I have updated the code to include a safety check for the current source lookup, as suggested by the Sourcery AI review. This ensures the function handles unsupported current sources gracefully by raising a ValueError. |
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.
his PR addresses a 5-year-old TODO in sciencelab.py. I have replaced hardcoded magic numbers in temperature and _get_ctmu_voltage with named constants and a calibration dictionary to improve code maintainability.
Summary by Sourcery
Refactor ScienceLab ADC and temperature handling to replace hardcoded magic numbers with shared constants and a calibration table.
Enhancements: