~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
1

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

Details
Message ID
<20210613003806.543514-1-devrtz@fortysixandtwo.eu>
DKIM signature
missing
Download raw message
Patch: +1 -1
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
---
Forgot to reference the raised issue (and I've wanted to try sending a v2 patch :)

 src/widgets.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/widgets.py b/src/widgets.py
index 528244d..03b4458 100644
--- a/src/widgets.py
+++ b/src/widgets.py
@@ -127,7 +127,7 @@ class EventActionRow(Handy.ActionRow):

        dur = (obj.end.timestamp() - obj.start.timestamp())
        pos = (now.timestamp() - obj.start.timestamp())
        prc = min(max(pos/dur, 0.0), 1.0)
        prc = min(max(pos/dur, 0.0), 1.0) if dur > 0 else int(pos >= 0)
        y = h * prc

        cr.set_source_rgb(0, 0, 0)
-- 
2.30.2
Details
Message ID
<f4efec8037aa2ea8bfdd2ef238d3740a134972fb.camel@kirgroup.net>
In-Reply-To
<20210613003806.543514-1-devrtz@fortysixandtwo.eu> (view parent)
DKIM signature
pass
Download raw message
Il giorno dom, 13/06/2021 alle 02.38 +0200, Evangelos Ribeiro Tzaras ha
scritto:
> 
> +        prc = min(max(pos/dur, 0.0), 1.0) if dur > 0 else int(pos >=
> 0)

While it's correct (and more correct than the code I was writting to
fix this), it's a little to difficult to read.
I would prefer an explicit if/else, with some comments, something like

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

And same patch should be added to `src/pages.py:510`
Reply to thread Export thread (mbox)