From d60874cf16cdec5619cfd41cd0a16d3152fef045 Mon Sep 17 00:00:00 2001 From: Michael Barnathan Date: Sun, 15 Jan 2023 20:37:12 -0500 Subject: [PATCH 1/9] Allow default messages to be set for context managers. Also allow configuration of stream destinations. --- pytictoc.py | 61 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/pytictoc.py b/pytictoc.py index 031ed8d..382c963 100644 --- a/pytictoc.py +++ b/pytictoc.py @@ -10,53 +10,66 @@ __version__ = '1.5.2' __version_date__ = '23 April 2021' - +import sys from timeit import default_timer class TicToc(object): - + """ Replicate the functionality of MATLAB's tic and toc. - + #Methods TicToc.tic() #start or re-start the timer TicToc.toc() #print elapsed time since timer start TicToc.tocvalue() #return floating point value of elapsed time since timer start - + #Attributes TicToc.start #Time from timeit.default_timer() when t.tic() was last called TicToc.end #Time from timeit.default_timer() when t.toc() or t.tocvalue() was last called TicToc.elapsed #t.end - t.start; i.e., time elapsed from t.start when t.toc() or t.tocvalue() was last called """ - - def __init__(self): - """Create instance of TicToc class.""" + + def __init__(self, default_msg='Elapsed time is', stream=sys.stdout): + """ + Create instance of TicToc class. + + Optional arguments: + default_msg - String to replace default message of 'Elapsed time is' + stream - Stream to write the timer message to + """ self.start = float('nan') self.end = float('nan') self.elapsed = float('nan') - + self.default_msg = default_msg + self.stream = stream + assert self.default_msg is not None + assert self.stream is not None + def tic(self): """Start the timer.""" self.start = default_timer() - - def toc(self, msg='Elapsed time is', restart=False): + + def toc(self, msg=None, restart=False): """ Report time elapsed since last call to tic(). - + Optional arguments: - msg - String to replace default message of 'Elapsed time is' + msg - String to replace default message initialized with the object restart - Boolean specifying whether to restart the timer """ + if msg is None: + msg = self.default_msg + self.end = default_timer() self.elapsed = self.end - self.start - print('%s %f seconds.' % (msg, self.elapsed)) + print(self.tocmsg(msg, self.elapsed), file=self.stream) if restart: self.start = default_timer() - + def tocvalue(self, restart=False): """ Return time elapsed since last call to tic(). - + Optional argument: restart - Boolean specifying whether to restart the timer """ @@ -65,13 +78,23 @@ def tocvalue(self, restart=False): if restart: self.start = default_timer() return self.elapsed - + + def tocmsg(self, prefix_msg, elapsed): + """ + Return full message that will be output on toc(). + + Arguments: + prefix_msg: preamble to the timer output + elapsed: time elapsed + """ + return '%s %f seconds.' % (prefix_msg, elapsed) + def __enter__(self): """Start the timer when using TicToc in a context manager.""" self.start = default_timer() - + def __exit__(self, *args): - """On exit, pring time elapsed since entering context manager.""" + """On exit, print time elapsed since entering context manager.""" self.end = default_timer() self.elapsed = self.end - self.start - print('Elapsed time is %f seconds.' % self.elapsed) + print(self.tocmsg(self.default_msg, self.elapsed), file=self.stream) From 282d3c268a60cac011407297ceb5eba2fb464596 Mon Sep 17 00:00:00 2001 From: Michael Barnathan Date: Sun, 15 Jan 2023 22:18:40 -0500 Subject: [PATCH 2/9] Add unit tests and time mocking. --- __init__.py | 0 pytictoc.py | 31 +++++----- testing/__init__.py | 0 testing/pytictoc_test.py | 128 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 14 deletions(-) create mode 100644 __init__.py create mode 100644 testing/__init__.py create mode 100644 testing/pytictoc_test.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pytictoc.py b/pytictoc.py index 382c963..374f6ea 100644 --- a/pytictoc.py +++ b/pytictoc.py @@ -24,30 +24,33 @@ class TicToc(object): TicToc.tocvalue() #return floating point value of elapsed time since timer start #Attributes - TicToc.start #Time from timeit.default_timer() when t.tic() was last called - TicToc.end #Time from timeit.default_timer() when t.toc() or t.tocvalue() was last called + TicToc.start #Time from timeit.self.timer() when t.tic() was last called + TicToc.end #Time from timeit.self.timer() when t.toc() or t.tocvalue() was last called TicToc.elapsed #t.end - t.start; i.e., time elapsed from t.start when t.toc() or t.tocvalue() was last called """ - def __init__(self, default_msg='Elapsed time is', stream=sys.stdout): + def __init__(self, default_msg='Elapsed time is', stream=None, timer=default_timer): """ Create instance of TicToc class. Optional arguments: default_msg - String to replace default message of 'Elapsed time is' stream - Stream to write the timer message to + timer - Function that returns the current time when called """ self.start = float('nan') self.end = float('nan') self.elapsed = float('nan') self.default_msg = default_msg - self.stream = stream + self.stream = stream or sys.stdout + self.timer = timer + assert self.timer is not None assert self.default_msg is not None assert self.stream is not None def tic(self): """Start the timer.""" - self.start = default_timer() + self.start = self.timer() def toc(self, msg=None, restart=False): """ @@ -60,11 +63,11 @@ def toc(self, msg=None, restart=False): if msg is None: msg = self.default_msg - self.end = default_timer() + self.end = self.timer() self.elapsed = self.end - self.start - print(self.tocmsg(msg, self.elapsed), file=self.stream) + print(self._tocmsg(msg, self.elapsed), file=self.stream) if restart: - self.start = default_timer() + self.start = self.timer() def tocvalue(self, restart=False): """ @@ -73,13 +76,13 @@ def tocvalue(self, restart=False): Optional argument: restart - Boolean specifying whether to restart the timer """ - self.end = default_timer() + self.end = self.timer() self.elapsed = self.end - self.start if restart: - self.start = default_timer() + self.start = self.timer() return self.elapsed - def tocmsg(self, prefix_msg, elapsed): + def _tocmsg(self, prefix_msg, elapsed): """ Return full message that will be output on toc(). @@ -91,10 +94,10 @@ def tocmsg(self, prefix_msg, elapsed): def __enter__(self): """Start the timer when using TicToc in a context manager.""" - self.start = default_timer() + self.start = self.timer() def __exit__(self, *args): """On exit, print time elapsed since entering context manager.""" - self.end = default_timer() + self.end = self.timer() self.elapsed = self.end - self.start - print(self.tocmsg(self.default_msg, self.elapsed), file=self.stream) + print(self._tocmsg(self.default_msg, self.elapsed), file=self.stream) diff --git a/testing/__init__.py b/testing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/pytictoc_test.py b/testing/pytictoc_test.py new file mode 100644 index 0000000..1ff854e --- /dev/null +++ b/testing/pytictoc_test.py @@ -0,0 +1,128 @@ +import contextlib +import re +import time +import unittest +from abc import ABCMeta +from io import StringIO + +from .. import pytictoc as ptt + + +class BaseTicTocTest(unittest.TestCase, metaclass=ABCMeta): + + def setUp(self): + self.mock_time = 0 + self.stream = StringIO() + self.tt = ptt.TicToc(default_msg="Time:", stream=self.stream, timer=lambda: self.mock_time) + + def output(self): + return self.stream.getvalue() + + def time_passed(self): + return re.findall(r'([-\d\.,]+) seconds.$', self.output(), re.MULTILINE) + + def err_msg(self): + raw_output = self.output().splitlines() or [""] + return "Last output: %s" % raw_output[-1] + + def _run_tictoc(self, start_time, end_time, work_function=None, **toc_args): + self.mock_time = start_time + self.tt.tic() + if work_function: + work_function() + self.mock_time = end_time + self.tt.toc(**toc_args) + + def tictoc(self, start_time=0, end_time=0, **toc_args): + self._run_tictoc(start_time, end_time, **toc_args) + # Measure equality to 6 decimal places to avoid float precision errors + self.assertAlmostEqual(start_time, self.tt.start, 6, self.err_msg()) + self.assertAlmostEqual(end_time, self.tt.end, 6, self.err_msg()) + time_passed = self.time_passed() + self.assertGreater(len(time_passed), 0, self.err_msg()) + self.assertAlmostEqual(end_time - start_time, float(time_passed[-1]), 6, self.err_msg()) + + def test_increment(self): + self.tictoc(0, 1) + self.tictoc(0, 2) + self.tictoc(5, 9) + self.tictoc(-9, 20) + + def test_no_time_passed(self): + self.tictoc(0, 0) + self.tictoc(1, 1) + self.tictoc(-5, -5) + + def test_decrement(self): + self.tictoc(1, 0) + self.tictoc(5, -5) + self.tictoc(-100, -300) + + def test_decimal(self): + self.tictoc(0.3, 3.6) + self.tictoc(0.3, -3.6) + self.tictoc(0.34, 0) + self.tictoc(0, -0.25) + self.tictoc(-7.9, -20) + self.tictoc(4, 25.89) + + def test_default_msg(self): + self.tt = ptt.TicToc(stream=self.stream, timer=lambda: self.mock_time) + self.tictoc(4, 6) + self.assertIn("Elapsed time is 2.0", self.output()) + + def test_default_stream_is_stdout(self): + with contextlib.redirect_stdout(self.stream): + self.tt = ptt.TicToc(default_msg="Time:", timer=lambda: self.mock_time) + self.tictoc(-4, 8.2) + self.tictoc(8, 16) + + def test_default_invocation_works(self): + with contextlib.redirect_stdout(self.stream): + self.tt = ptt.TicToc() + + # I suggest keeping a negative interval to confirm the actual time is used + self._run_tictoc(0, -1, lambda: time.sleep(0.01)) + + # NB: Prior to Python 3.5 the sleep could end early. Will still work with zero. + self.assertGreaterEqual(float(self.time_passed()[-1]), 0) + + +class TicTocTest(BaseTicTocTest): + def test_msg(self): + self.tictoc(1, 6, msg="Tic Toc") + self.assertIn("Tic Toc 5.0", self.output()) + + def test_msg_in_stdout(self): + with contextlib.redirect_stdout(self.stream): + self.tt = ptt.TicToc(default_msg="Time:", timer=lambda: self.mock_time) + self.tictoc(1, -7, msg="Tic Toc") + self.assertIn("Tic Toc -8.0", self.output()) + + def test_restart(self): + self._run_tictoc(4, 6, restart=True) + self.assertAlmostEqual(6, self.tt.start, 6) + + +class TicTocTestWithContextManager(BaseTicTocTest): + def _run_tictoc(self, start_time, end_time, **toc_args): + if toc_args: + raise unittest.SkipTest("Toc arguments aren't supported with context managers") + + self.mock_time = start_time + with self.tt: + self.mock_time = end_time + + def test_default_invocation_works(self): + with contextlib.redirect_stdout(self.stream): + self.tt = ptt.TicToc() + with self.tt: + time.sleep(0.01) + # NB: Prior to Python 3.5 the sleep could end early. Will still work with zero. + self.assertGreaterEqual(float(self.time_passed()[-1]), 0) + + +del BaseTicTocTest # Don't run the tests in the abstract class. + +if __name__ == '__main__': + unittest.main() From 4795b5cc7a725335d3a5ed2f1d9271321e78aa75 Mon Sep 17 00:00:00 2001 From: Michael Barnathan Date: Sun, 15 Jan 2023 22:30:49 -0500 Subject: [PATCH 3/9] Replace hardcoded directory with parent dir of test. --- testing/test_pytictoc.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/testing/test_pytictoc.py b/testing/test_pytictoc.py index 0e0798b..36b1049 100644 --- a/testing/test_pytictoc.py +++ b/testing/test_pytictoc.py @@ -8,45 +8,48 @@ if sys.version_info.major == 3: from importlib import reload - -os.chdir(r'C:\Users\ecfne\Documents\Eric\Coding\Python\pytictoc') + +os.chdir(os.path.dirname(os.getcwd())) try: reload(pytictoc) except (NameError, ImportError): import pytictoc - + + def waste_time(num_reps=1000000): for i in range(num_reps): spam = i+1 eggs = spam**2 pancakes = eggs % 3 - + + def main(): print('\npytictoc file:') print(pytictoc.__file__ + '\n') - + t = pytictoc.TicToc() - + t.tic() waste_time() t.toc() - + with pytictoc.TicToc(): waste_time() - + t.toc('It has been', restart=True) t.toc() - + spam = t.tocvalue() print(spam) - + waste_time() - + spam = t.tocvalue(restart=True) print(spam) t.toc() - + + if __name__ == '__main__': main() From 53afc7455f04e11b136e209902f6222d88d64eb7 Mon Sep 17 00:00:00 2001 From: Michael Barnathan Date: Sun, 15 Jan 2023 22:35:48 -0500 Subject: [PATCH 4/9] Use tocvalue within toc to eliminate some duplicate code. --- pytictoc.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pytictoc.py b/pytictoc.py index 374f6ea..19a1f08 100644 --- a/pytictoc.py +++ b/pytictoc.py @@ -63,11 +63,8 @@ def toc(self, msg=None, restart=False): if msg is None: msg = self.default_msg - self.end = self.timer() - self.elapsed = self.end - self.start + self.tocvalue(restart=restart) print(self._tocmsg(msg, self.elapsed), file=self.stream) - if restart: - self.start = self.timer() def tocvalue(self, restart=False): """ From 18fa02b76cc6890a181273061a894ea196ec3954 Mon Sep 17 00:00:00 2001 From: Michael Barnathan Date: Sun, 15 Jan 2023 22:36:04 -0500 Subject: [PATCH 5/9] Clean up floating point assertions. --- testing/pytictoc_test.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/testing/pytictoc_test.py b/testing/pytictoc_test.py index 1ff854e..a91c03c 100644 --- a/testing/pytictoc_test.py +++ b/testing/pytictoc_test.py @@ -35,12 +35,16 @@ def _run_tictoc(self, start_time, end_time, work_function=None, **toc_args): def tictoc(self, start_time=0, end_time=0, **toc_args): self._run_tictoc(start_time, end_time, **toc_args) - # Measure equality to 6 decimal places to avoid float precision errors - self.assertAlmostEqual(start_time, self.tt.start, 6, self.err_msg()) - self.assertAlmostEqual(end_time, self.tt.end, 6, self.err_msg()) + self.assertTime(start_time, self.tt.start) + self.assertTime(end_time, self.tt.end) time_passed = self.time_passed() self.assertGreater(len(time_passed), 0, self.err_msg()) - self.assertAlmostEqual(end_time - start_time, float(time_passed[-1]), 6, self.err_msg()) + self.assertTime(end_time - start_time, float(time_passed[-1])) + self.assertTime(end_time - start_time, self.tt.tocvalue()) + + def assertTime(self, expected, actual, *args, **kwargs): + # Measure equality to 6 decimal places to avoid float precision errors + self.assertAlmostEqual(expected, actual, 6, self.err_msg(), *args, **kwargs) def test_increment(self): self.tictoc(0, 1) @@ -101,7 +105,7 @@ def test_msg_in_stdout(self): def test_restart(self): self._run_tictoc(4, 6, restart=True) - self.assertAlmostEqual(6, self.tt.start, 6) + self.assertTime(6, self.tt.start) class TicTocTestWithContextManager(BaseTicTocTest): From 25a8fa371887c7995be9bf5fc0fd8fea4d1d7588 Mon Sep 17 00:00:00 2001 From: Michael Barnathan Date: Sun, 15 Jan 2023 22:54:51 -0500 Subject: [PATCH 6/9] Preserve doc behavior allowing default_timer to be set --- pytictoc.py | 8 ++++++-- testing/pytictoc_test.py | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/pytictoc.py b/pytictoc.py index 19a1f08..c80e0ab 100644 --- a/pytictoc.py +++ b/pytictoc.py @@ -13,6 +13,7 @@ import sys from timeit import default_timer + class TicToc(object): """ @@ -29,7 +30,7 @@ class TicToc(object): TicToc.elapsed #t.end - t.start; i.e., time elapsed from t.start when t.toc() or t.tocvalue() was last called """ - def __init__(self, default_msg='Elapsed time is', stream=None, timer=default_timer): + def __init__(self, default_msg='Elapsed time is', stream=None, timer=None): """ Create instance of TicToc class. @@ -38,12 +39,15 @@ def __init__(self, default_msg='Elapsed time is', stream=None, timer=default_tim stream - Stream to write the timer message to timer - Function that returns the current time when called """ + self.start = float('nan') self.end = float('nan') self.elapsed = float('nan') self.default_msg = default_msg self.stream = stream or sys.stdout - self.timer = timer + + # Lazily accessing default timer allows the user to override it later per the docs. + self.timer = timer or (lambda: default_timer()) assert self.timer is not None assert self.default_msg is not None assert self.stream is not None diff --git a/testing/pytictoc_test.py b/testing/pytictoc_test.py index a91c03c..9841175 100644 --- a/testing/pytictoc_test.py +++ b/testing/pytictoc_test.py @@ -13,14 +13,20 @@ class BaseTicTocTest(unittest.TestCase, metaclass=ABCMeta): def setUp(self): self.mock_time = 0 self.stream = StringIO() - self.tt = ptt.TicToc(default_msg="Time:", stream=self.stream, timer=lambda: self.mock_time) + self.mock_timer = lambda: self.mock_time + self.tt = ptt.TicToc(default_msg="Time:", stream=self.stream, timer=self.mock_timer) def output(self): return self.stream.getvalue() - def time_passed(self): + def times_passed(self): return re.findall(r'([-\d\.,]+) seconds.$', self.output(), re.MULTILINE) + def last_time_passed(self): + times_passed = self.times_passed() + self.assertGreater(len(times_passed), 0, self.err_msg()) + return float(times_passed[-1]) + def err_msg(self): raw_output = self.output().splitlines() or [""] return "Last output: %s" % raw_output[-1] @@ -37,9 +43,7 @@ def tictoc(self, start_time=0, end_time=0, **toc_args): self._run_tictoc(start_time, end_time, **toc_args) self.assertTime(start_time, self.tt.start) self.assertTime(end_time, self.tt.end) - time_passed = self.time_passed() - self.assertGreater(len(time_passed), 0, self.err_msg()) - self.assertTime(end_time - start_time, float(time_passed[-1])) + self.assertTime(end_time - start_time, self.last_time_passed()) self.assertTime(end_time - start_time, self.tt.tocvalue()) def assertTime(self, expected, actual, *args, **kwargs): @@ -71,13 +75,13 @@ def test_decimal(self): self.tictoc(4, 25.89) def test_default_msg(self): - self.tt = ptt.TicToc(stream=self.stream, timer=lambda: self.mock_time) + self.tt = ptt.TicToc(stream=self.stream, timer=self.mock_timer) self.tictoc(4, 6) self.assertIn("Elapsed time is 2.0", self.output()) def test_default_stream_is_stdout(self): with contextlib.redirect_stdout(self.stream): - self.tt = ptt.TicToc(default_msg="Time:", timer=lambda: self.mock_time) + self.tt = ptt.TicToc(default_msg="Time:", timer=self.mock_timer) self.tictoc(-4, 8.2) self.tictoc(8, 16) @@ -89,7 +93,22 @@ def test_default_invocation_works(self): self._run_tictoc(0, -1, lambda: time.sleep(0.01)) # NB: Prior to Python 3.5 the sleep could end early. Will still work with zero. - self.assertGreaterEqual(float(self.time_passed()[-1]), 0) + self.assertGreaterEqual(self.last_time_passed(), 0) + + def test_setting_default_timer(self): + self.tt = ptt.TicToc(stream=self.stream) + old_timer = ptt.default_timer + try: + ptt.default_timer = self.mock_timer + self.tictoc(4, 60) + self.tictoc(5, -2) + finally: + ptt.default_timer = old_timer + + def test_not_setting_default_timer(self): + self.tt = ptt.TicToc(stream=self.stream) + self._run_tictoc(5, -2) + self.assertGreaterEqual(self.last_time_passed(), 0) class TicTocTest(BaseTicTocTest): @@ -99,7 +118,7 @@ def test_msg(self): def test_msg_in_stdout(self): with contextlib.redirect_stdout(self.stream): - self.tt = ptt.TicToc(default_msg="Time:", timer=lambda: self.mock_time) + self.tt = ptt.TicToc(default_msg="Time:", timer=self.mock_timer) self.tictoc(1, -7, msg="Tic Toc") self.assertIn("Tic Toc -8.0", self.output()) @@ -123,7 +142,7 @@ def test_default_invocation_works(self): with self.tt: time.sleep(0.01) # NB: Prior to Python 3.5 the sleep could end early. Will still work with zero. - self.assertGreaterEqual(float(self.time_passed()[-1]), 0) + self.assertGreaterEqual(self.last_time_passed(), 0) del BaseTicTocTest # Don't run the tests in the abstract class. From d482cc7bcbe378db0c426a4ced1dd9596075fff5 Mon Sep 17 00:00:00 2001 From: Michael Barnathan Date: Sun, 15 Jan 2023 23:00:50 -0500 Subject: [PATCH 7/9] Update documentation with new options. --- README.rst | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 26e7919..ca5dfe7 100644 --- a/README.rst +++ b/README.rst @@ -7,7 +7,7 @@ INSTALLATION pytictoc can be installed and updated via conda or pip. **pip** :: - + pip install pytictoc pip install pytictoc --upgrade @@ -19,7 +19,7 @@ pytictoc can be installed and updated via conda or pip. ============= USAGE -============= +============= Basic usage: :: @@ -35,6 +35,14 @@ A string passed to the toc method changes the printed message. This can be usefu >> t.toc('Section 1 took') Section 1 took 16.494467 seconds. +The default message can also be passed in the constructor, for easier usage with context managers: + + >> with TicToc(default_msg='Section 1 took'): + >> ... + Section 1 took 16.494467 seconds. + +The string passed to toc will override the string in the constructor if both are present. + An optional keyword argument restarts the timer (equivalent to t.tic()) after reporting the time elapsed. :: >> t.toc(restart=True) @@ -48,8 +56,12 @@ If you want to return the time elapsed to a variable rather than printing it, us >>spam 20.156261717544602 +You can also pass in an alternative stream to print to: + + >> t = TicToc(stream=mystream) + The TicToc class can be used within a context manager as an alternative way to time a section of code. The time taken to run the code inside the with statement will be reported on exit. :: - + >>with TicToc(): >> spam = [x+1 for x in range(10000)] Elapsed time is 0.002343 seconds. @@ -58,7 +70,7 @@ The TicToc class can be used within a context manager as an alternative way to t Determining and setting the timer ------------------------------------ -pytictoc uses timeit.default_timer to time code. On Python 3.3 and later, this is an alias for time.perf_counter. On earlier versions of Python it is an alias for the most precise timer for a given system. +pytictoc uses timeit.default_timer to time code. On Python 3.3 and later, this is an alias for time.perf_counter. On earlier versions of Python it is an alias for the most precise timer for a given system. To see which function is being used: :: @@ -66,9 +78,12 @@ To see which function is being used: :: >>pytictoc.default_timer -You can change the timer by simple assignment. :: - +You can change the timer by simple assignment, or by passing a different timer function into an object's constructor. :: + >>import time >>pytictoc.default_timer = time.clock >>pytictoc.default_timer + + >>import time + >>pytictoc.TicToc(timer=time.clock) From da04a4a1460812f783fcbed7ff8816baa34ba49e Mon Sep 17 00:00:00 2001 From: Michael Barnathan Date: Sun, 15 Jan 2023 23:01:17 -0500 Subject: [PATCH 8/9] Explicitly check toc message override. --- testing/pytictoc_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/testing/pytictoc_test.py b/testing/pytictoc_test.py index 9841175..9d5f94b 100644 --- a/testing/pytictoc_test.py +++ b/testing/pytictoc_test.py @@ -121,6 +121,7 @@ def test_msg_in_stdout(self): self.tt = ptt.TicToc(default_msg="Time:", timer=self.mock_timer) self.tictoc(1, -7, msg="Tic Toc") self.assertIn("Tic Toc -8.0", self.output()) + self.assertNotIn("Time:", self.output()) def test_restart(self): self._run_tictoc(4, 6, restart=True) From 2dd2974d7c9a1d8f7356dc3958208253118eaf84 Mon Sep 17 00:00:00 2001 From: Michael Barnathan Date: Mon, 16 Jan 2023 19:40:35 -0800 Subject: [PATCH 9/9] Update README.rst Fix code blocks in readme --- README.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index ca5dfe7..277fa7e 100644 --- a/README.rst +++ b/README.rst @@ -35,7 +35,7 @@ A string passed to the toc method changes the printed message. This can be usefu >> t.toc('Section 1 took') Section 1 took 16.494467 seconds. -The default message can also be passed in the constructor, for easier usage with context managers: +The default message can also be passed in the constructor, for easier usage with context managers: :: >> with TicToc(default_msg='Section 1 took'): >> ... @@ -56,7 +56,7 @@ If you want to return the time elapsed to a variable rather than printing it, us >>spam 20.156261717544602 -You can also pass in an alternative stream to print to: +You can also pass in an alternative stream to print to: :: >> t = TicToc(stream=mystream) @@ -78,12 +78,14 @@ To see which function is being used: :: >>pytictoc.default_timer -You can change the timer by simple assignment, or by passing a different timer function into an object's constructor. :: +You can change the timer by simple assignment: :: >>import time >>pytictoc.default_timer = time.clock >>pytictoc.default_timer +Or by passing a different timer function into an object's constructor: :: + >>import time >>pytictoc.TicToc(timer=time.clock)