brilliant-learn-python 0.1.5__tar.gz → 0.1.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: brilliant-learn-python
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: Add your description here
5
5
  Author: Oliver Tautz
6
6
  Author-email: Oliver Tautz <oliver_tautz@yahoo.de>
@@ -4,7 +4,7 @@ description = "Add your description here"
4
4
  name = "brilliant-learn-python"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.14"
7
- version = "0.1.5"
7
+ version = "0.1.7"
8
8
 
9
9
  [[project.authors]]
10
10
  name = "Oliver Tautz"
@@ -7,7 +7,7 @@ description = "Add your description here"
7
7
  name = "brilliant-learn-python"
8
8
  readme = "README.md"
9
9
  requires-python = ">=3.14"
10
- version = "0.1.5"
10
+ version = "0.1.7"
11
11
 
12
12
  [build-system]
13
13
  build-backend = "uv_build"
@@ -10,11 +10,13 @@ class Timer(AbstractContextManager["Timer"]):
10
10
  """Measure execution time."""
11
11
 
12
12
  def __init__(self) -> None:
13
- self.start = 0.0
13
+ self.start: float | None = None
14
14
  self.elapsed = 0.0
15
+ self.done = False
15
16
 
16
17
  def __enter__(self) -> Self:
17
18
  self.start = time.perf_counter()
19
+ self.done = False
18
20
  return self
19
21
 
20
22
  def __exit__(
@@ -23,4 +25,18 @@ class Timer(AbstractContextManager["Timer"]):
23
25
  exc_value: BaseException | None,
24
26
  traceback: TracebackType | None,
25
27
  ) -> None:
28
+ if self.start is None:
29
+ raise RuntimeError("Timer was never started")
30
+
26
31
  self.elapsed = time.perf_counter() - self.start
32
+ self.done = True
33
+
34
+ def get_elapsed(self) -> float:
35
+ """Return the current elapsed time in seconds."""
36
+ if self.start is None:
37
+ return 0.0
38
+
39
+ if not self.done:
40
+ return time.perf_counter() - self.start
41
+
42
+ return self.elapsed
@@ -0,0 +1,21 @@
1
+ """Decorator for measuring function execution time."""
2
+
3
+ from collections.abc import Callable
4
+ from typing import Any
5
+
6
+ from brilliant_learn_python.context_managers.timer import Timer
7
+
8
+
9
+ def timer(func: Callable[..., Any]) -> Callable[..., Any]:
10
+ """Measure the execution time of a function."""
11
+
12
+ def wrapper(*args: Any, **kwargs: Any) -> Any:
13
+ """Execute the wrapped function and measure its duration."""
14
+
15
+ with Timer() as timer:
16
+ result = func(*args, **kwargs)
17
+ print(f"func {func.__name__} ran for {timer.get_elapsed()}")
18
+
19
+ return result
20
+
21
+ return wrapper