# Buggy code
def __init__(self, job_type):
    self.stats_weighing_until = settings().getFloat(
        ["estimation", "printTime", "statsWeighingUntil"]
    )
    self.validity_range = settings().getFloat(
        ["estimation", "printTime", "validityRange"]
    )
    self.force_dumb_from_percent = settings().getFloat(
        ["estimation", "printTime", "forceDumbFromPercent"]
    )
    self.force_dumb_after_min = settings().getFloat(
        ["estimation", "printTime", "forceDumbAfterMin"]
    )

    threshold = None
    rolling_window = None
    countdown = None

    if job_type == "local" or job_type == "sdcard":
        # we are happy if the average of the estimates stays within 60s of the prior one
        threshold = settings().getFloat(["estimation", "printTime", "stableThreshold"])

        if job_type == "sdcard":
            # we are interesting in a rolling window of roughly the last 15s, so the number of entries has to be derived
            # by that divided by the sd status polling interval
            interval = settings().getFloat(["serial", "timeout", "sdStatus"])
            if interval <= 0:
                interval = 1.0
            rolling_window = 15 / interval

            # we are happy when one rolling window has been stable
            countdown = rolling_window

    self._data = TimeEstimationHelper(
        rolling_window=rolling_window, countdown=countdown, threshold=threshold
    )


# Traceback (ZeroDivisionError)
2019-04-08 17:22:53,104 - octoprint.printer.standard - ERROR - Looks like something crashed inside the state update worker. Please report this on the OctoPrint issue tracker (make sure to include logs!)
Traceback (most recent call last):
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/printer/standard.py", line 1346, in _work
data = self.get_current_data()
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/printer/standard.py", line 1356, in get_current_data
self._progress = self._get_current_progress()
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/printer/standard.py", line 1282, in _get_current_progress
return self._on_get_progress()
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/printer/standard.py", line 793, in _updateProgressDataCallback
statisticalTotalPrintTimeType)
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/printer/estimation.py", line 106, in estimate
estimatedTotalPrintTime = self.estimate_total(progress, cleanedPrintTime)
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/printer/estimation.py", line 166, in estimate_total
return self._data.update(printTime / progress)
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/printer/estimation.py", line 207, in update
if -1.0 * self._threshold < self.average_distance < self._threshold:
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/printer/estimation.py", line 239, in average_distance
return sum(self._distances) / len(self._distances)
ZeroDivisionError: division by zero

# Fixed code
def __init__(self, job_type):
    self.stats_weighing_until = settings().getFloat(
        ["estimation", "printTime", "statsWeighingUntil"]
    )
    self.validity_range = settings().getFloat(
        ["estimation", "printTime", "validityRange"]
    )
    self.force_dumb_from_percent = settings().getFloat(
        ["estimation", "printTime", "forceDumbFromPercent"]
    )
    self.force_dumb_after_min = settings().getFloat(
        ["estimation", "printTime", "forceDumbAfterMin"]
    )

    threshold = None
    rolling_window = None
    countdown = None

    if job_type == "local" or job_type == "sdcard":
        # we are happy if the average of the estimates stays within 60s of the prior one
        threshold = settings().getFloat(["estimation", "printTime", "stableThreshold"])

        if job_type == "sdcard":
            # we are interesting in a rolling window of roughly the last 15s, so the number of entries has to be derived
            # by that divided by the sd status polling interval
            interval = settings().getFloat(["serial", "timeout", "sdStatus"])
            if interval <= 0:
                interval = 1.0
            rolling_window = 15 / interval
            if rolling_window < 1:
                rolling_window = 1

            # we are happy when one rolling window has been stable
            countdown = rolling_window

    self._data = TimeEstimationHelper(
        rolling_window=rolling_window, countdown=countdown, threshold=threshold
    )