Refactor: Replace magic numbers with constants in ScienceLab#277
Refactor: Replace magic numbers with constants in ScienceLab#277TejasAnalyst wants to merge 2 commits into
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.
|
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. |
There was a problem hiding this comment.
Pull request overview
This PR refactors ScienceLab ADC and MCU temperature calculations by replacing inline numeric literals with named module-level constants and a temperature calibration mapping.
Changes:
- Added ADC voltage/resolution constants.
- Added a temperature calibration dictionary for CTMU current sources.
- Updated temperature and CTMU voltage calculations to use the new constants.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Clean lookup from global constants | ||
| cal = TEMP_CALIB.get(cs) | ||
| if cal is None: | ||
| raise ValueError(f"Unsupported current source: {cs}") |
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: