progressBar: prevent ZeroDivisionError when span is zero#1417
Merged
yunjunz merged 3 commits intoinsarlab:mainfrom Nov 23, 2025
Merged
progressBar: prevent ZeroDivisionError when span is zero#1417yunjunz merged 3 commits intoinsarlab:mainfrom
yunjunz merged 3 commits intoinsarlab:mainfrom
Conversation
|
💖 Thanks for opening this pull request! Please check out our contributing guidelines. 💖 |
Contributor
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
|
Contributor
There was a problem hiding this comment.
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.
yunjunz
approved these changes
Nov 22, 2025
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 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: