Skip to content

Commit 08efcc0

Browse files
Copilotmmcky
andcommitted
Remove silent parameter, keep only verbose for output control
- Remove silent parameter from Timer class entirely - Remove silent parameter from timeit function - Update all logic to use only verbose parameter - Update all tests to use verbose=False instead of silent=True - Remove backward compatibility tests for silent parameter - Simplify API to have only one parameter controlling output behavior Co-authored-by: mmcky <[email protected]>
1 parent c59645a commit 08efcc0

File tree

2 files changed

+31
-49
lines changed

2 files changed

+31
-49
lines changed

quantecon/util/tests/test_timing.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def setup_method(self):
6464

6565
def test_basic_timer(self):
6666
"""Test basic Timer context manager functionality."""
67-
timer = Timer(silent=True)
67+
timer = Timer(verbose=False)
6868

6969
with timer:
7070
time.sleep(self.sleep_time)
@@ -75,7 +75,7 @@ def test_basic_timer(self):
7575

7676
def test_timer_return_value(self):
7777
"""Test that Timer returns self for variable assignment."""
78-
with Timer(silent=True) as timer:
78+
with Timer(verbose=False) as timer:
7979
time.sleep(self.sleep_time)
8080

8181
assert timer.elapsed is not None
@@ -84,17 +84,17 @@ def test_timer_return_value(self):
8484
def test_timer_units(self):
8585
"""Test different time units."""
8686
# Test seconds (default)
87-
with Timer(silent=True) as timer_sec:
87+
with Timer(verbose=False) as timer_sec:
8888
time.sleep(self.sleep_time)
8989
expected_sec = self.sleep_time
9090
assert_allclose(timer_sec.elapsed, expected_sec, atol=0.05, rtol=2)
9191

9292
# Timer always stores elapsed time in seconds regardless of display unit
93-
with Timer(unit="milliseconds", silent=True) as timer_ms:
93+
with Timer(unit="milliseconds", verbose=False) as timer_ms:
9494
time.sleep(self.sleep_time)
9595
assert_allclose(timer_ms.elapsed, expected_sec, atol=0.05, rtol=2)
9696

97-
with Timer(unit="microseconds", silent=True) as timer_us:
97+
with Timer(unit="microseconds", verbose=False) as timer_us:
9898
time.sleep(self.sleep_time)
9999
assert_allclose(timer_us.elapsed, expected_sec, atol=0.05, rtol=2)
100100

@@ -109,33 +109,33 @@ def test_invalid_unit(self):
109109
def test_timer_precision(self):
110110
"""Test that precision parameter is accepted (output format tested manually)."""
111111
# Just verify it doesn't crash with different precision values
112-
with Timer(precision=0, silent=True) as timer0:
112+
with Timer(precision=0, verbose=False) as timer0:
113113
time.sleep(self.sleep_time)
114-
with Timer(precision=6, silent=True) as timer6:
114+
with Timer(precision=6, verbose=False) as timer6:
115115
time.sleep(self.sleep_time)
116116

117117
assert timer0.elapsed is not None
118118
assert timer6.elapsed is not None
119119

120120
def test_timer_message(self):
121121
"""Test custom message parameter (output format tested manually)."""
122-
with Timer(message="Test operation", silent=True) as timer:
122+
with Timer(message="Test operation", verbose=False) as timer:
123123
time.sleep(self.sleep_time)
124124

125125
assert timer.elapsed is not None
126126

127-
def test_timer_silent_mode(self):
128-
"""Test silent mode suppresses output."""
129-
# This mainly tests that silent=True doesn't crash
127+
def test_timer_verbose_mode(self):
128+
"""Test verbose mode controls output."""
129+
# This mainly tests that verbose=False doesn't crash
130130
# Output suppression is hard to test automatically
131-
with Timer(silent=True) as timer:
131+
with Timer(verbose=False) as timer:
132132
time.sleep(self.sleep_time)
133133

134134
assert timer.elapsed is not None
135135

136136
def test_timer_exception_handling(self):
137137
"""Test that Timer works correctly even when exceptions occur."""
138-
timer = Timer(silent=True)
138+
timer = Timer(verbose=False)
139139

140140
try:
141141
with timer:
@@ -153,7 +153,7 @@ def test_timeit_basic(self):
153153
def test_func():
154154
time.sleep(self.sleep_time)
155155

156-
result = timeit(test_func, runs=3, silent=True, results=True)
156+
result = timeit(test_func, runs=3, verbose=False, results=True)
157157

158158
# Check that we have results
159159
assert 'elapsed' in result
@@ -177,7 +177,7 @@ def test_func_with_args(sleep_time, multiplier=1):
177177

178178
# Use lambda to bind arguments
179179
func_with_args = lambda: test_func_with_args(self.sleep_time, 0.5)
180-
result = timeit(func_with_args, runs=2, silent=True, results=True)
180+
result = timeit(func_with_args, runs=2, verbose=False, results=True)
181181

182182
# Check results
183183
assert len(result['elapsed']) == 2
@@ -214,7 +214,7 @@ def test_timeit_single_run(self):
214214
def test_func():
215215
time.sleep(self.sleep_time)
216216

217-
result = timeit(test_func, runs=1, silent=True, results=True)
217+
result = timeit(test_func, runs=1, verbose=False, results=True)
218218

219219
assert len(result['elapsed']) == 1
220220
assert result['average'] == result['elapsed'][0]
@@ -226,12 +226,12 @@ def test_timeit_different_units(self):
226226
def test_func():
227227
time.sleep(self.sleep_time)
228228

229-
# Test milliseconds (silent mode to avoid output during tests)
230-
result_ms = timeit(test_func, runs=2, unit="milliseconds", silent=True, results=True)
229+
# Test milliseconds (verbose=False to avoid output during tests)
230+
result_ms = timeit(test_func, runs=2, unit="milliseconds", verbose=False, results=True)
231231
assert len(result_ms['elapsed']) == 2
232232

233233
# Test microseconds
234-
result_us = timeit(test_func, runs=2, unit="microseconds", silent=True, results=True)
234+
result_us = timeit(test_func, runs=2, unit="microseconds", verbose=False, results=True)
235235
assert len(result_us['elapsed']) == 2
236236

237237
# All results should be in seconds regardless of display unit
@@ -246,7 +246,7 @@ def test_func():
246246
time.sleep(self.sleep_time)
247247

248248
# This test is mainly to ensure stats_only doesn't crash
249-
result = timeit(test_func, runs=2, stats_only=True, silent=True, results=True)
249+
result = timeit(test_func, runs=2, stats_only=True, verbose=False, results=True)
250250
assert len(result['elapsed']) == 2
251251

252252
def test_timeit_invalid_timer_kwargs(self):
@@ -291,19 +291,3 @@ def test_func():
291291
assert 'elapsed' in result2
292292
assert len(result2['elapsed']) == 2
293293

294-
def test_timeit_backward_compatibility(self):
295-
"""Test that existing silent parameter still works."""
296-
def test_func():
297-
time.sleep(self.sleep_time)
298-
299-
# Test that silent=True still suppresses output
300-
result = timeit(test_func, runs=2, silent=True, results=True)
301-
assert result is not None
302-
assert len(result['elapsed']) == 2
303-
304-
# Test interaction between verbose and silent
305-
# silent=True should override verbose=True
306-
result2 = timeit(test_func, runs=2, verbose=True, silent=True, results=True)
307-
assert result2 is not None
308-
assert len(result2['elapsed']) == 2
309-

quantecon/util/timing.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ class Timer:
194194
Number of decimal places to display for seconds.
195195
unit : str, optional(default="seconds")
196196
Unit to display timing in. Options: "seconds", "milliseconds", "microseconds"
197-
silent : bool, optional(default=False)
198-
If True, suppress printing of timing results.
197+
verbose : bool, optional(default=True)
198+
If True, print timing results. If False, suppress printing of timing results.
199199
200200
Attributes
201201
----------
@@ -217,19 +217,19 @@ class Timer:
217217
Computing results: 0.0001 seconds elapsed
218218
219219
Store elapsed time for comparison:
220-
>>> timer = Timer(silent=True)
220+
>>> timer = Timer(verbose=False)
221221
>>> with timer:
222222
... # some code
223223
... pass
224224
>>> print(f"Method took {timer.elapsed:.6f} seconds")
225225
Method took 0.000123 seconds
226226
"""
227227

228-
def __init__(self, message="", precision=2, unit="seconds", silent=False):
228+
def __init__(self, message="", precision=2, unit="seconds", verbose=True):
229229
self.message = message
230230
self.precision = precision
231231
self.unit = unit.lower()
232-
self.silent = silent
232+
self.verbose = verbose
233233
self.elapsed = None
234234
self._start_time = None
235235

@@ -246,7 +246,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
246246
end_time = time.time()
247247
self.elapsed = end_time - self._start_time
248248

249-
if not self.silent:
249+
if self.verbose:
250250
self._print_elapsed()
251251

252252
def _print_elapsed(self):
@@ -290,12 +290,11 @@ def timeit(func, runs=1, stats_only=False, verbose=True, results=False, **timer_
290290
individual run times followed by summary statistics.
291291
verbose : bool, optional(default=True)
292292
If True, print nicely formatted timing output all at once at the end.
293-
If False, suppress all output (overrides stats_only and silent).
293+
If False, suppress all output.
294294
results : bool, optional(default=False)
295295
If True, return dictionary with timing results. If False, return None.
296296
**timer_kwargs
297-
Keyword arguments to pass to Timer (message, precision, unit, silent).
298-
Note: silent parameter is deprecated in favor of verbose parameter.
297+
Keyword arguments to pass to Timer (message, precision, unit, verbose).
299298
300299
Returns
301300
-------
@@ -350,16 +349,15 @@ def timeit(func, runs=1, stats_only=False, verbose=True, results=False, **timer_
350349
'message': timer_kwargs.pop('message', ''),
351350
'precision': timer_kwargs.pop('precision', 2),
352351
'unit': timer_kwargs.pop('unit', 'seconds'),
353-
'silent': timer_kwargs.pop('silent', False) # Explicit silent parameter
352+
'verbose': timer_kwargs.pop('verbose', True) # Timer verbose parameter
354353
}
355354

356355
# Warn about unused kwargs
357356
if timer_kwargs:
358357
raise ValueError(f"Unknown timer parameters: {list(timer_kwargs.keys())}")
359358

360359
# Determine if we should show output
361-
# verbose=False overrides everything, silent=True overrides for backward compatibility
362-
show_output = verbose and not timer_params['silent']
360+
show_output = verbose
363361

364362
run_times = []
365363
output_lines = [] # Collect output lines for printing all at once
@@ -368,7 +366,7 @@ def timeit(func, runs=1, stats_only=False, verbose=True, results=False, **timer_
368366
for i in range(runs):
369367
# Always silence individual Timer output to avoid duplication with our run display
370368
individual_timer_params = timer_params.copy()
371-
individual_timer_params['silent'] = True
369+
individual_timer_params['verbose'] = False
372370

373371
with Timer(**individual_timer_params) as timer:
374372
func()

0 commit comments

Comments
 (0)