From 0e451d3b3bf899b3d9ac0c38e3c3cd9d9be170ba Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Fri, 16 Jul 2021 21:43:11 -0700 Subject: Rename timer; add a test for OutputContext. --- stopwatch.py | 28 ++++++++++++++++++++++++++++ tests/logging_utils_test.py | 42 ++++++++++++++++++++++++++++++++++++++++++ timer.py | 28 ---------------------------- 3 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 stopwatch.py create mode 100755 tests/logging_utils_test.py delete mode 100644 timer.py diff --git a/stopwatch.py b/stopwatch.py new file mode 100644 index 0000000..752c7ed --- /dev/null +++ b/stopwatch.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import time +from typing import Callable + + +class Timer(object): + """ + with timer.Timer() as t: + do_the_thing() + + walltime = t() + print(f'That took {walltime}s.') + """ + + def __init__(self) -> None: + self.start = None + self.end = None + pass + + def __enter__(self) -> Callable[[], float]: + self.start = time.perf_counter() + self.end = 0.0 + return lambda: self.end - self.start + + def __exit__(self, *args) -> bool: + self.end = time.perf_counter() + return True diff --git a/tests/logging_utils_test.py b/tests/logging_utils_test.py new file mode 100755 index 0000000..48e5015 --- /dev/null +++ b/tests/logging_utils_test.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import contextlib +import tempfile +import unittest + +import bootstrap +import logging_utils as lutils +import string_utils as sutils + + +class TestLoggingUtils(unittest.TestCase): + + def test_output_context(self): + unique_suffix = sutils.generate_uuid(True) + filename = f'/tmp/logging_utils_test.{unique_suffix}' + secret_message = f'This is a test, {unique_suffix}.' + + with tempfile.SpooledTemporaryFile(mode='r+') as tmpfile1: + with tempfile.SpooledTemporaryFile(mode='r+') as tmpfile2: + with contextlib.redirect_stdout(tmpfile1): + with lutils.OutputContext( + lutils.OutputMultiplexer.Destination.FILENAME | + lutils.OutputMultiplexer.Destination.FILEHANDLE | + lutils.OutputMultiplexer.Destination.STDOUT, + filename = filename, + handle = tmpfile2, + ) as mplex: + mplex.print(secret_message, end='') + with open(filename, 'r') as rf: + self.assertEqual(rf.readline(), secret_message) + tmpfile2.seek(0) + tmp = tmpfile2.readline() + self.assertEqual(tmp, secret_message) + tmpfile1.seek(0) + tmp = tmpfile1.readline() + self.assertEqual(tmp, secret_message) + + +if __name__ == '__main__': + unittest.main = bootstrap.initialize(unittest.main) + unittest.main() diff --git a/timer.py b/timer.py deleted file mode 100644 index 752c7ed..0000000 --- a/timer.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python3 - -import time -from typing import Callable - - -class Timer(object): - """ - with timer.Timer() as t: - do_the_thing() - - walltime = t() - print(f'That took {walltime}s.') - """ - - def __init__(self) -> None: - self.start = None - self.end = None - pass - - def __enter__(self) -> Callable[[], float]: - self.start = time.perf_counter() - self.end = 0.0 - return lambda: self.end - self.start - - def __exit__(self, *args) -> bool: - self.end = time.perf_counter() - return True -- cgit v1.3