~fabrixxm/confy-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2 2

[PATCH v3] Fix division by zero for events with zero duration

Details
Message ID
<20210614001813.872791-1-devrtz@fortysixandtwo.eu>
DKIM signature
missing
Download raw message
Patch: +16 -4
When a event has duration zero because the start and end times are identical,
ZeroDivisionError's are raised:

```
Traceback (most recent call last):
  File "/home/fortysixandtwo/install/lib/python3.9/site-packages/confy/widgets.py", line 130, in _on_draw
    prc = min(max(pos/dur, 0.0), 1.0)
ZeroDivisionError: float division by zero
```

This patch ensures that prc is either 0 if the event is in the future
or 1, if it's in the past.

Closes #18
---
 src/pages.py   | 10 ++++++++--
 src/widgets.py | 10 ++++++++--
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/pages.py b/src/pages.py
index 5e6c09f..4920aae 100644
--- a/src/pages.py
+++ b/src/pages.py
@@ -506,8 +506,14 @@ class EventDetailPage(BasePage):
        w = self.get_allocated_width()

        dur = (obj.end.timestamp() - obj.start.timestamp())
        pos = (now.timestamp() - obj.start.timestamp())
        prc = min(max(pos/dur, 0.0), 1.0)
        if dur > 0:
            pos = (now.timestamp() - obj.start.timestamp())
            prc = min(max(pos/dur, 0.0), 1.0)
        else:
            # not a valid event duration
            # set prc to 0 if the event is in the future or 1 if in the past
            prc = int(pos >= 0)

        x = w * prc

        cr.set_source_rgb(0, 0, 0)
diff --git a/src/widgets.py b/src/widgets.py
index 528244d..16969f5 100644
--- a/src/widgets.py
+++ b/src/widgets.py
@@ -126,8 +126,14 @@ class EventActionRow(Handy.ActionRow):
        h = self.get_allocated_height()

        dur = (obj.end.timestamp() - obj.start.timestamp())
        pos = (now.timestamp() - obj.start.timestamp())
        prc = min(max(pos/dur, 0.0), 1.0)
        if dur > 0:
            pos = (now.timestamp() - obj.start.timestamp())
            prc = min(max(pos/dur, 0.0), 1.0)
        else:
            # not a valid event duration
            # set prc to 0 if the event is in the future or 1 if in the past
            prc = int(pos >= 0)

        y = h * prc

        cr.set_source_rgb(0, 0, 0)
-- 
2.30.2
Details
Message ID
<4883ba77d5a468765f6ced47c9449abe40e3706c.camel@kirgroup.net>
In-Reply-To
<20210614001813.872791-1-devrtz@fortysixandtwo.eu> (view parent)
DKIM signature
pass
Download raw message
Il giorno lun, 14/06/2021 alle 02.18 +0200, Evangelos Ribeiro Tzaras ha
scritto:
> --- a/src/pages.py
> +++ b/src/pages.py
> @@ -506,8 +506,14 @@ class EventDetailPage(BasePage):
>          w = self.get_allocated_width()
>  
>          dur = (obj.end.timestamp() - obj.start.timestamp())
> -        pos = (now.timestamp() - obj.start.timestamp())
> -        prc = min(max(pos/dur, 0.0), 1.0)
> +        if dur > 0:
> +            pos = (now.timestamp() - obj.start.timestamp())

If you move pos inside this branch...


> +            prc = min(max(pos/dur, 0.0), 1.0)
> +        else:
> +            # not a valid event duration
> +            # set prc to 0 if the event is in the future or 1 if in
> the past
> +            prc = int(pos >= 0)

... it will not be valid here
Details
Message ID
<4596ba8765d628b71064e7a93ee90dae81287e66.camel@fortysixandtwo.eu>
In-Reply-To
<4883ba77d5a468765f6ced47c9449abe40e3706c.camel@kirgroup.net> (view parent)
DKIM signature
missing
Download raw message
Hi,

On Mon, 2021-06-14 at 08:23 +0200, Fabio wrote:
> Il giorno lun, 14/06/2021 alle 02.18 +0200, Evangelos Ribeiro Tzaras
> ha
> scritto:
> > --- a/src/pages.py
> > +++ b/src/pages.py
> > @@ -506,8 +506,14 @@ class EventDetailPage(BasePage):
> >          w = self.get_allocated_width()
> >  
> >          dur = (obj.end.timestamp() - obj.start.timestamp())
> > -        pos = (now.timestamp() - obj.start.timestamp())
> > -        prc = min(max(pos/dur, 0.0), 1.0)
> > +        if dur > 0:
> > +            pos = (now.timestamp() - obj.start.timestamp())
> 
> If you move pos inside this branch...

D'oh! That probably goes to show you shouldn't "quickly" code something
up half asleep. 
> 
> 
> > +            prc = min(max(pos/dur, 0.0), 1.0)
> > +        else:
> > +            # not a valid event duration
> > +            # set prc to 0 if the event is in the future or 1 if
> > in
> > the past
> > +            prc = int(pos >= 0)
> 
> ... it will not be valid here
> 
> 

Sorry for the noise!


-- 
Evangelos
PGP: B938 6554 B7DD 266B CB8E 29A9 90F0 C9B1 8A6B 4A19
Reply to thread Export thread (mbox)