Skip to content

Conversation

@frx08
Copy link
Contributor

@frx08 frx08 commented Sep 22, 2025

Description of proposed changes

Prevent ZeroDivisionError in progressBar when span is zero and clamp
percent values to the [0,100] range. This makes the progress bar more
robust and avoids showing negative or >100% values.

Reminders

  • Fix #xxxx
  • Pass Pre-commit check (green)
  • Pass Codacy code review (green)
  • Pass Circle CI test (green)
  • Make sure that your code follows our style. Use the other functions/files as a basis.
  • If modifying functionality, describe changes to function behavior and arguments in a comment below the function declaration.
  • If adding new functionality, add a detailed description to the documentation and/or an example.

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:

  • Handle zero span in progress bar to avoid ZeroDivisionError
  • Clamp percentDone to the range [0,100] to prevent negative or over-100% values

@welcome
Copy link

welcome bot commented Sep 22, 2025

💖 Thanks for opening this pull request! Please check out our contributing guidelines. 💖
Keep in mind that all new features should be documented. It helps to write the comments next to the code or below your functions describing all arguments, and return types before writing the code. This will help you think about your code design and usually results in better code.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Sep 22, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

In 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 logic

classDiagram
    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
Loading

File-Level Changes

Change Details Files
Prevent division by zero when span is zero
  • Add conditional to check if span equals zero
  • Assign percentDone = 100 when span is zero
src/mintpy/objects/progress.py
Clamp computed percentage within valid range
  • Enforce percentDone to be between 0 and 100 using max/min
src/mintpy/objects/progress.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant