brilliant-learn-python 0.1.3__tar.gz → 0.1.6__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.3
3
+ Version: 0.1.6
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.3"
7
+ version = "0.1.6"
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.3"
10
+ version = "0.1.6"
11
11
 
12
12
  [build-system]
13
13
  build-backend = "uv_build"
@@ -0,0 +1,42 @@
1
+ """Context manager for measuring execution time."""
2
+
3
+ import time
4
+ from contextlib import AbstractContextManager
5
+ from types import TracebackType
6
+ from typing import Self
7
+
8
+
9
+ class Timer(AbstractContextManager["Timer"]):
10
+ """Measure execution time."""
11
+
12
+ def __init__(self) -> None:
13
+ self.start: float | None = None
14
+ self.elapsed = 0.0
15
+ self.done = False
16
+
17
+ def __enter__(self) -> Self:
18
+ self.start = time.perf_counter()
19
+ self.done = False
20
+ return self
21
+
22
+ def __exit__(
23
+ self,
24
+ exc_type: type[BaseException] | None,
25
+ exc_value: BaseException | None,
26
+ traceback: TracebackType | None,
27
+ ) -> None:
28
+ if self.start is None:
29
+ raise RuntimeError("Timer was never started")
30
+
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(timer.get_elapsed())
18
+
19
+ return result
20
+
21
+ return wrapper