-
Notifications
You must be signed in to change notification settings - Fork 292
fix progress bar reset giving ZeroDivisionError #1417
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
base: main
Are you sure you want to change the base?
Conversation
|
💖 Thanks for opening this pull request! Please check out our contributing guidelines. 💖 |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideIn update_amount, add a guard to set percentDone to 100 when span is zero to avoid ZeroDivisionError, and clamp computed percentDone values to the 0–100 range to prevent invalid progress values. Class diagram for updated ProgressBar logicclassDiagram
class ProgressBar {
- amount
- min
- span
- width
+ update_amount(newAmount=0, suffix='')
}
ProgressBar : update_amount() modifies percentDone calculation
ProgressBar : update_amount() clamps percentDone to [0, 100]
ProgressBar : update_amount() sets percentDone=100 if span==0
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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 there - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/mintpy/objects/progress.py:109` </location>
<code_context>
def update_amount(self, newAmount=0, suffix=''):
""" Update the progress bar with the new amount (with min and max
values set at initialization; if it is over or under, it takes the
min or max value as a default.
"""
newAmount = max(newAmount, self.min)
newAmount = min(newAmount, self.max)
self.amount = newAmount
# Add whitespace to suffix if turned ON
if suffix:
suffix = ' '+suffix
# Figure out the new percent done (round to an integer)
diffFromMin = float(self.amount - self.min)
if self.span == 0:
percentDone = 100
else:
percentDone = (diffFromMin / float(self.span)) * 100.0
percentDone = int(np.round(percentDone))
percentDone = max(0, min(100, percentDone))
# Figure out how many hash bars the percentage should be
allFull = self.width - 2 - 18
numHashes = (percentDone / 100.0) * allFull
numHashes = int(np.round(numHashes))
# Build a progress bar with an arrow of equal signs; special cases for empty and full
if numHashes == 0:
self.prog_bar = '{}[>{}]'.format(self.prefix, ' '*(allFull-1))
elif numHashes == allFull:
self.prog_bar = '{}[{}]'.format(self.prefix, '='*allFull)
self.prog_bar += suffix
else:
self.prog_bar = '[{}>{}]'.format('='*(numHashes-1), ' '*(allFull-numHashes))
# figure out where to put the percentage (roughly centered)
percentPlace = int(len(self.prog_bar)/2 - len(str(percentDone)))
percentString = f' {percentDone}% '
# slice the percentage into the bar
self.prog_bar = ''.join([self.prog_bar[0:percentPlace],
percentString,
self.prog_bar[percentPlace+len(percentString):]])
# prefix and suffix
self.prog_bar = self.prefix + self.prog_bar + suffix
# time info - elapsed time and estimated remaining time
if percentDone > 0:
elapse_time = time.time() - self.start_time
remain_time = int(elapse_time * (100./percentDone-1))
self.prog_bar += f'{int(elapse_time):5d}s / {int(remain_time):5d}s'
</code_context>
<issue_to_address>
**issue (code-quality):** We've found these issues:
- Replace if statement with if expression ([`assign-if-exp`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/assign-if-exp/))
- Replace call to format with f-string [×2] ([`use-fstring-for-formatting`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/use-fstring-for-formatting/))
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] ([`remove-redundant-slice-index`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/remove-redundant-slice-index/))
- Remove unnecessary casts to int, str, float or bool ([`remove-unnecessary-cast`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/remove-unnecessary-cast/))
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Description of proposed changes
Prevent
ZeroDivisionErrorinprogressBarwhen span is zero and clamppercent values to the [0,100] range. This makes the progress bar more
robust and avoids showing negative or >100% values.
Reminders
Summary by Sourcery
Prevent division by zero in the progress bar when span is zero and clamp computed percentages to the 0–100 range for robustness
Bug Fixes: