Skip to content

Commit 5b3ef41

Browse files
authored
Add support for using isort to order imports (#19)
1 parent 51369dc commit 5b3ef41

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

codetiming/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def stuff():
2121
# Do something
2222
"""
2323

24-
# Import Timer for cleaner namespace
24+
# Codetiming imports
2525
from codetiming._timer import Timer, TimerError # noqa
2626

2727
# Versioning is handled by bump2version

codetiming/_timer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"""
66

77
# Standard library imports
8+
import math
89
import time
910
from contextlib import ContextDecorator
1011
from dataclasses import dataclass, field
11-
from math import nan
1212
from typing import Any, Callable, ClassVar, Dict, Optional
1313

1414

@@ -21,11 +21,11 @@ class Timer(ContextDecorator):
2121
"""Time your code using a class, context manager, or decorator"""
2222

2323
timers: ClassVar[Dict[str, float]] = dict()
24+
_start_time: Optional[float] = field(default=None, init=False, repr=False)
2425
name: Optional[str] = None
2526
text: str = "Elapsed time: {:0.4f} seconds"
2627
logger: Optional[Callable[[str], None]] = print
27-
_start_time: Optional[float] = field(default=None, init=False, repr=False)
28-
last: float = field(default=nan, init=False, repr=False)
28+
last: float = field(default=math.nan, init=False, repr=False)
2929

3030
def __post_init__(self) -> None:
3131
"""Initialization: add timer to dict of timers"""

pyproject.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ requires = [
3939
"Tutorial" = "https://realpython.com/python-timer"
4040

4141
[tool.flit.metadata.requires-extra]
42-
dev = ["black", "bump2version", "flake8", "flit", "mypy"]
42+
dev = ["black", "bump2version", "flake8", "flit", "isort", "mypy"]
4343
test = ["pytest", "pytest-cov", "tox"]
4444

45+
[tool.isort]
46+
multi_line_output = 3
47+
include_trailing_comma = true
48+
force_grid_wrap = 0
49+
use_parentheses = true
50+
line_length = 88
51+
import_heading_stdlib = "Standard library imports"
52+
import_heading_thirdparty = "Third party imports"
53+
import_heading_firstparty = "Codetiming imports"

tests/test_codetiming.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
Based on the Pytest test runner
44
"""
55
# Standard library imports
6+
import math
67
import re
78
import time
8-
from math import isnan
99

1010
# Third party imports
1111
import pytest
@@ -167,11 +167,15 @@ def test_error_if_restarting_running_timer():
167167
t.start()
168168

169169

170-
def test_timer_sets_last():
170+
def test_last_starts_as_nan():
171+
"""Test that .last attribute is initialized as nan"""
171172
t = Timer()
172-
assert isnan(t.last)
173-
t.start()
174-
time.sleep(0.1)
175-
t.stop()
173+
assert math.isnan(t.last)
174+
175+
176+
def test_timer_sets_last():
177+
"""Test that .last attribute is properly set"""
178+
with Timer() as t:
179+
time.sleep(0.02)
176180

177-
assert 0.1 <= t.last
181+
assert t.last >= 0.02

0 commit comments

Comments
 (0)