-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] Add convert_in_si
Function
#96
base: main
Are you sure you want to change the base?
Conversation
Reviewer's Guide by SourceryThis pull request introduces the Sequence diagram for convert_in_si() functionsequenceDiagram
participant User
participant convert_in_si
participant Frame
participant TreeMap
participant Quantity
User->>convert_in_si: call convert_in_si()
activate convert_in_si
convert_in_si->>Frame: get caller frame
Frame-->>convert_in_si: return caller globals
loop For each global variable
convert_in_si->>TreeMap: map _convert_in_si
activate TreeMap
TreeMap->>Quantity: factorless()
Quantity-->>TreeMap: SI unit quantity
TreeMap-->>convert_in_si: converted value
end
convert_in_si->>Frame: update globals
convert_in_si-->>User: return
Class diagram showing Quantity class with factorless methodclassDiagram
class Quantity {
+mantissa
+unit
+factorless()
+__mul__(other)
+__add__(other)
+__sub__(other)
+to_decimal()
}
class Unit {
+factor
+dimension
+name
+factorless()
}
Quantity --> Unit: has
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
convert_in_si
Function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Routhleck - I've reviewed your changes - here's some feedback:
Overall Comments:
- The
convert_in_si()
function modifies global state by changing variables in the caller's scope. Consider adding a warning in the docstring about potential side effects and thread-safety concerns when using this function.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
return x | ||
|
||
|
||
def convert_in_si(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider using a context manager to explicitly define the scope of SI unit conversion and automatically restore original units afterwards.
The frame inspection and global variable manipulation in convert_in_si()
makes program flow difficult to reason about. Consider using a context manager pattern instead, which provides clearer scope boundaries while maintaining the same functionality:
@contextmanager
def si_units():
"""Context manager for converting quantities to SI units"""
# Store original values
frame = inspect.currentframe().f_back
original = {k: v for k, v in frame.f_locals.items()
if isinstance(v, (Quantity, Unit))}
try:
# Convert to SI
for k, v in original.items():
frame.f_locals[k] = v.factorless()
yield
finally:
# Restore original values
for k, v in original.items():
frame.f_locals[k] = v
# Usage:
with si_units():
# All quantities in this block use SI units
result = time1 * length1
This approach:
- Makes the scope of SI unit conversion explicit
- Automatically restores original units after the block
- Eliminates global state modification
- Provides clearer error boundaries
The context manager pattern is more idiomatic Python and makes the code's behavior more predictable.
The
convert_in_si()
function has been added to convert allQuantity
variables in the current scope (including those nested in lists, tuples, or dictionaries) to their SI unit equivalents. This function achieves this by calling thefactorless()
method on eachQuantity
instance, which convert the unit and returns the quantities in SI units.Usage Examples
Below are some examples demonstrating the use of convert_in_si():
Summary by Sourcery
New Features:
convert_in_si()
that converts allQuantity
variables in the current scope to their SI unit equivalents.