Skip to content

Commit

Permalink
Merge a55b8b5 into 86f2eb8
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Mar 11, 2019
2 parents 86f2eb8 + a55b8b5 commit 2dc1c5a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
7 changes: 7 additions & 0 deletions spinn_utilities/logger_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ def warn_once(logger, msg):
logger.warning(msg)


def error_once(logger, msg):
if msg in _already_issued:
return
_already_issued.add(msg)
logger.error(msg)


def reset():
global _already_issued
_already_issued = set()
11 changes: 10 additions & 1 deletion spinn_utilities/progress_bar.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
from __future__ import print_function, division
import logging
import sys
import math
import os
from spinn_utilities.overrides import overrides
from spinn_utilities import logger_utils

logger = logging.getLogger(__name__)


class ProgressBar(object):
""" Progress bar for telling the user where a task is up to
"""
MAX_LENGTH_IN_CHARS = 60

TOO_MANY_ERROR = "Too many update steps in progress bar! " \
"This may be a sign that something else has gone wrong!"

__slots__ = (
"_number_of_things", "_currently_completed", "_destination",
"_chars_per_thing", "_chars_done", "_string",
Expand Down Expand Up @@ -47,8 +54,10 @@ def update(self, amount_to_add=1):
:param amount_to_add:
:rtype: None
"""

if self._currently_completed + amount_to_add > self._number_of_things:
raise Exception("too many update steps")
logger_utils.error_once(logger, self.TOO_MANY_ERROR)
return
self._currently_completed += amount_to_add
self._check_differences()

Expand Down
10 changes: 10 additions & 0 deletions unittests/test_logger_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@ def test_multiple(self):
logger_utils.warn_once(logger, "a log warning")
log_checker.assert_logs_contains_once("WARNING", lc.records,
"a log warning")

def test_error(self):
with LogCapture() as lc:
logger_utils.error_once(logger, "a log Error")
logger_utils.warn_once(logger, "another log error")
logger_utils.warn_once(logger, "a log warning")
log_checker.assert_logs_contains_once(
"ERROR", lc.records, "a log Error")
log_checker.assert_logs_error_not_contains(
lc.records, "another log error")
13 changes: 11 additions & 2 deletions unittests/test_progress_bar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from testfixtures import LogCapture
from spinn_utilities.progress_bar import ProgressBar, DummyProgressBar
from spinn_utilities.testing import log_checker
from spinn_utilities import logger_utils


@pytest.mark.parametrize("pbclass", [ProgressBar, DummyProgressBar])
Expand Down Expand Up @@ -28,19 +31,25 @@ def test_with_operation(pbclass):

@pytest.mark.parametrize("pbclass", [ProgressBar, DummyProgressBar])
def test_check_length_full(pbclass):
logger_utils.reset()
p = pbclass(2, None)
with pytest.raises(Exception):
with LogCapture() as lc:
p.update(3)
log_checker.assert_logs_contains_once(
"ERROR", lc.records, ProgressBar.TOO_MANY_ERROR)
p.end()


@pytest.mark.parametrize("pbclass", [ProgressBar, DummyProgressBar])
def test_check_length_addition(pbclass):
logger_utils.reset()
p = pbclass(2, None)
p.update()
p.update()
with pytest.raises(Exception):
with LogCapture() as lc:
p.update()
log_checker.assert_logs_contains_once(
"ERROR", lc.records, ProgressBar.TOO_MANY_ERROR)
p.end()


Expand Down

0 comments on commit 2dc1c5a

Please sign in to comment.