thisiscam / math-with-slack

Rendered math (MathJax) with Slack's desktop client
MIT License
306 stars 28 forks source link

ZeroDivisionError during install #3

Closed EricPostMaster closed 4 years ago

EricPostMaster commented 4 years ago

It took me three tries to run the script and download MathJax. The first two times I got this error:

Traceback (most recent call last):
  File "math-with-slack.py", line 385, in <module>
    mathjax_tar_name, headers = urllib_request.urlretrieve(args.mathjax_url,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 283, in urlretrieve
    reporthook(blocknum, bs, size)
  File "math-with-slack.py", line 375, in reporthook
    speed = progress_size / (1024 * duration)
ZeroDivisionError: float division by zero

For some reason, it just worked on the third attempt even though I didn't do anything differently. I don't know what caused the error, but until it's solved I think it would be good to add a note to the README.md to help new users troubleshoot.

EricPostMaster commented 4 years ago

It seems like the problem might be

        if count == 0:
            progress_size = 0
            start_time = time.time()  # Time starts counting from zero
            return
        duration = time.time() - start_time  # If duration is first calculated in the same second as start_time, speed (below) will divide by zero and return an error
        progress_size += block_size
        if progress_size >= total_size:
            progress_size = total_size
        speed = progress_size / (1024 * duration)  # Error if duration is calculated in same second as start_time

I'm looking at datetime.time() right now to see if we can use microseconds instead...

thisiscam commented 4 years ago

@EricPostMaster I have just fixed this on master https://github.com/thisiscam/math-with-slack/blob/master/math-with-slack.py#L369 and https://github.com/thisiscam/math-with-slack/blob/master/math-with-slack.py#L375

Try catch might not be necessary here but it avoids problems :) Let me know if this is working for you!

EricPostMaster commented 4 years ago

@thisiscam - Looks great! I have never used try/except, so I'm glad to see your solution.

I am new to this, so is there a way for me to test the fix now that I have math-with-slack installed on my computer? Or do I need to uninstall it and reinstall with the new master?

EricPostMaster commented 4 years ago

Working like a charm. Closing issue.