performance-decorator 0.1.0__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.
- performance_decorator-0.1.0/.idea/.gitignore +10 -0
- performance_decorator-0.1.0/.idea/inspectionProfiles/profiles_settings.xml +6 -0
- performance_decorator-0.1.0/.idea/modules.xml +8 -0
- performance_decorator-0.1.0/.idea/performance-decorator.iml +14 -0
- performance_decorator-0.1.0/.idea/workspace.xml +102 -0
- performance_decorator-0.1.0/LICENSE +21 -0
- performance_decorator-0.1.0/PKG-INFO +55 -0
- performance_decorator-0.1.0/README.md +35 -0
- performance_decorator-0.1.0/pyproject.toml +31 -0
- performance_decorator-0.1.0/src/performance/__init__.py +37 -0
- performance_decorator-0.1.0/tests/test_performance.py +27 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="ProjectModuleManager">
|
|
4
|
+
<modules>
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/performance-decorator.iml" filepath="$PROJECT_DIR$/.idea/performance-decorator.iml" />
|
|
6
|
+
</modules>
|
|
7
|
+
</component>
|
|
8
|
+
</project>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module external.system.id="pyproject.toml" type="PYTHON_MODULE" version="4">
|
|
3
|
+
<component name="NewModuleRootManager">
|
|
4
|
+
<content url="file://$MODULE_DIR$">
|
|
5
|
+
<sourceFolder url="file://$MODULE_DIR$/src" />
|
|
6
|
+
</content>
|
|
7
|
+
<orderEntry type="jdk" jdkName="/usr/local/bin/python3" jdkType="Python SDK" />
|
|
8
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
9
|
+
</component>
|
|
10
|
+
<component name="PackageRequirementsSettings" />
|
|
11
|
+
<component name="PyDocumentationSettings" />
|
|
12
|
+
<component name="ReSTService" />
|
|
13
|
+
<component name="TestRunnerService" />
|
|
14
|
+
</module>
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="AutoImportSettings">
|
|
4
|
+
<option name="autoReloadType" value="ALL" />
|
|
5
|
+
</component>
|
|
6
|
+
<component name="ChangeListManager">
|
|
7
|
+
<list default="true" id="a9c99927-fb94-4f3f-8e4b-995b99955790" name="Changes" comment="" />
|
|
8
|
+
<option name="SHOW_DIALOG" value="false" />
|
|
9
|
+
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
|
10
|
+
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
|
11
|
+
<option name="LAST_RESOLUTION" value="IGNORE" />
|
|
12
|
+
</component>
|
|
13
|
+
<component name="ProjectColorInfo"><![CDATA[{
|
|
14
|
+
"associatedIndex": 8,
|
|
15
|
+
"fromUser": false
|
|
16
|
+
}]]></component>
|
|
17
|
+
<component name="ProjectId" id="3JjLVvuwa1hUIO8ATBa2u3hImgU" />
|
|
18
|
+
<component name="ProjectViewState">
|
|
19
|
+
<option name="hideEmptyMiddlePackages" value="true" />
|
|
20
|
+
<option name="showLibraryContents" value="true" />
|
|
21
|
+
</component>
|
|
22
|
+
<component name="PropertiesComponent"><![CDATA[{
|
|
23
|
+
"keyToString": {
|
|
24
|
+
"ModuleVcsDetector.initialDetectionPerformed": "true",
|
|
25
|
+
"RunOnceActivity.ShowReadmeOnStart": "true",
|
|
26
|
+
"codeWithMe.voiceChat.enabledByDefault": "false",
|
|
27
|
+
"copilot.chat.legacySessionMetadata.migrated": "true",
|
|
28
|
+
"last_opened_file_path": "/Users/leonardobartolelli/Desktop/Projects/Performance Decorator Library",
|
|
29
|
+
"python.packages.toolwindow.project.migrated.to.right.top.v2": "true"
|
|
30
|
+
}
|
|
31
|
+
}]]></component>
|
|
32
|
+
<component name="RunManager">
|
|
33
|
+
<configuration default="true" type="PythonConfigurationType" factoryName="Python">
|
|
34
|
+
<module name="performance-decorator" />
|
|
35
|
+
<option name="ENV_FILES" value="" />
|
|
36
|
+
<option name="INTERPRETER_OPTIONS" value="" />
|
|
37
|
+
<option name="PARENT_ENVS" value="true" />
|
|
38
|
+
<envs>
|
|
39
|
+
<env name="PYTHONUNBUFFERED" value="1" />
|
|
40
|
+
</envs>
|
|
41
|
+
<option name="SDK_HOME" value="" />
|
|
42
|
+
<option name="WORKING_DIRECTORY" value="" />
|
|
43
|
+
<option name="IS_MODULE_SDK" value="false" />
|
|
44
|
+
<option name="ADD_CONTENT_ROOTS" value="true" />
|
|
45
|
+
<option name="ADD_SOURCE_ROOTS" value="true" />
|
|
46
|
+
<option name="DEBUG_JUST_MY_CODE" value="false" />
|
|
47
|
+
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
|
|
48
|
+
<option name="RUN_TOOL" value="" />
|
|
49
|
+
<option name="SCRIPT_NAME" value="" />
|
|
50
|
+
<option name="PARAMETERS" value="" />
|
|
51
|
+
<option name="SHOW_COMMAND_LINE" value="false" />
|
|
52
|
+
<option name="EMULATE_TERMINAL" value="false" />
|
|
53
|
+
<option name="MODULE_MODE" value="false" />
|
|
54
|
+
<option name="REDIRECT_INPUT" value="false" />
|
|
55
|
+
<option name="INPUT_FILE" value="" />
|
|
56
|
+
<method v="2" />
|
|
57
|
+
</configuration>
|
|
58
|
+
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
|
59
|
+
<module name="performance-decorator" />
|
|
60
|
+
<option name="ENV_FILES" value="" />
|
|
61
|
+
<option name="INTERPRETER_OPTIONS" value="" />
|
|
62
|
+
<option name="PARENT_ENVS" value="true" />
|
|
63
|
+
<envs>
|
|
64
|
+
<env name="PYTHONUNBUFFERED" value="1" />
|
|
65
|
+
</envs>
|
|
66
|
+
<option name="SDK_HOME" value="" />
|
|
67
|
+
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
|
68
|
+
<option name="IS_MODULE_SDK" value="true" />
|
|
69
|
+
<option name="ADD_CONTENT_ROOTS" value="true" />
|
|
70
|
+
<option name="ADD_SOURCE_ROOTS" value="true" />
|
|
71
|
+
<option name="DEBUG_JUST_MY_CODE" value="false" />
|
|
72
|
+
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
|
|
73
|
+
<option name="RUN_TOOL" value="" />
|
|
74
|
+
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
|
|
75
|
+
<option name="PARAMETERS" value="" />
|
|
76
|
+
<option name="SHOW_COMMAND_LINE" value="false" />
|
|
77
|
+
<option name="EMULATE_TERMINAL" value="false" />
|
|
78
|
+
<option name="MODULE_MODE" value="false" />
|
|
79
|
+
<option name="REDIRECT_INPUT" value="false" />
|
|
80
|
+
<option name="INPUT_FILE" value="" />
|
|
81
|
+
<method v="2" />
|
|
82
|
+
</configuration>
|
|
83
|
+
</component>
|
|
84
|
+
<component name="SharedIndexes">
|
|
85
|
+
<attachedChunks>
|
|
86
|
+
<set>
|
|
87
|
+
<option value="bundled-python-sdk-eff1e8def5f5-5bd6d13a2a75-com.jetbrains.pycharm.pro.sharedIndexes.bundled-PY-262.10315.174" />
|
|
88
|
+
</set>
|
|
89
|
+
</attachedChunks>
|
|
90
|
+
</component>
|
|
91
|
+
<component name="TaskManager">
|
|
92
|
+
<task active="true" id="Default" summary="Default task">
|
|
93
|
+
<changelist id="a9c99927-fb94-4f3f-8e4b-995b99955790" name="Changes" comment="" />
|
|
94
|
+
<created>1790169557781</created>
|
|
95
|
+
<option name="number" value="Default" />
|
|
96
|
+
<option name="presentableId" value="Default" />
|
|
97
|
+
<updated>1790169557781</updated>
|
|
98
|
+
<workItem from="1790169558793" duration="326000" />
|
|
99
|
+
</task>
|
|
100
|
+
<servers />
|
|
101
|
+
</component>
|
|
102
|
+
</project>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Leonardo Bartolelli
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: performance-decorator
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small decorator that measures and reports function execution time.
|
|
5
|
+
Project-URL: Homepage, https://pypi.org/project/performance-decorator/
|
|
6
|
+
Project-URL: Repository, https://github.com/leonardobartolelli/performance-decorator
|
|
7
|
+
Author: Leonardo Bartolelli
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: decorator,performance,timing
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Requires-Python: >=3.8
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# performance-decorator
|
|
22
|
+
|
|
23
|
+
Measure function execution time with a simple decorator.
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install performance-decorator
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from performance import performance
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@performance
|
|
38
|
+
def calculate():
|
|
39
|
+
return sum(range(100_000))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
result = calculate()
|
|
43
|
+
print(result)
|
|
44
|
+
print(calculate.last_duration) # elapsed time in seconds
|
|
45
|
+
print(calculate.execution_times) # duration of every completed call
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Each call prints a line such as:
|
|
49
|
+
|
|
50
|
+
```text
|
|
51
|
+
calculate took 0.002341 seconds
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The wrapped function's return value and metadata are preserved. Timing is
|
|
55
|
+
recorded even when the wrapped function raises an exception.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# performance-decorator
|
|
2
|
+
|
|
3
|
+
Measure function execution time with a simple decorator.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install performance-decorator
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from performance import performance
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@performance
|
|
18
|
+
def calculate():
|
|
19
|
+
return sum(range(100_000))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
result = calculate()
|
|
23
|
+
print(result)
|
|
24
|
+
print(calculate.last_duration) # elapsed time in seconds
|
|
25
|
+
print(calculate.execution_times) # duration of every completed call
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Each call prints a line such as:
|
|
29
|
+
|
|
30
|
+
```text
|
|
31
|
+
calculate took 0.002341 seconds
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The wrapped function's return value and metadata are preserved. Timing is
|
|
35
|
+
recorded even when the wrapped function raises an exception.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "performance-decorator"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A small decorator that measures and reports function execution time."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Leonardo Bartolelli" },
|
|
14
|
+
]
|
|
15
|
+
keywords = ["decorator", "performance", "timing"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
22
|
+
"Programming Language :: Python :: 3.8",
|
|
23
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Homepage = "https://pypi.org/project/performance-decorator/"
|
|
28
|
+
Repository = "https://github.com/leonardobartolelli/performance-decorator"
|
|
29
|
+
|
|
30
|
+
[tool.hatch.build.targets.wheel]
|
|
31
|
+
packages = ["src/performance"]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Measure and report function execution time."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from functools import wraps
|
|
6
|
+
from time import perf_counter
|
|
7
|
+
from typing import Any, Callable, TypeVar, cast
|
|
8
|
+
|
|
9
|
+
F = TypeVar("F", bound=Callable[..., Any])
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def performance(function: F) -> F:
|
|
13
|
+
"""Decorate a function to print and expose its execution time.
|
|
14
|
+
|
|
15
|
+
The wrapped function keeps its original return value. Its most recent
|
|
16
|
+
execution time, in seconds, is available as ``function.last_duration``.
|
|
17
|
+
A history of all completed calls is available as
|
|
18
|
+
``function.execution_times``.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
@wraps(function)
|
|
22
|
+
def timed(*args: Any, **kwargs: Any) -> Any:
|
|
23
|
+
start = perf_counter()
|
|
24
|
+
try:
|
|
25
|
+
return function(*args, **kwargs)
|
|
26
|
+
finally:
|
|
27
|
+
duration = perf_counter() - start
|
|
28
|
+
timed.last_duration = duration # type: ignore[attr-defined]
|
|
29
|
+
timed.execution_times.append(duration) # type: ignore[attr-defined]
|
|
30
|
+
print(f"{function.__name__} took {duration:.6f} seconds")
|
|
31
|
+
|
|
32
|
+
timed.last_duration = None # type: ignore[attr-defined]
|
|
33
|
+
timed.execution_times = [] # type: ignore[attr-defined]
|
|
34
|
+
return cast(F, timed)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
__all__ = ["performance"]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from performance import performance
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_performance_preserves_result_and_records_duration(capsys):
|
|
5
|
+
@performance
|
|
6
|
+
def add(left, right):
|
|
7
|
+
return left + right
|
|
8
|
+
|
|
9
|
+
assert add(2, 3) == 5
|
|
10
|
+
assert add.last_duration is not None
|
|
11
|
+
assert add.last_duration >= 0
|
|
12
|
+
assert len(add.execution_times) == 1
|
|
13
|
+
assert "add took " in capsys.readouterr().out
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_performance_records_duration_when_function_raises():
|
|
17
|
+
@performance
|
|
18
|
+
def fail():
|
|
19
|
+
raise ValueError("failed")
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
fail()
|
|
23
|
+
except ValueError:
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
assert fail.last_duration is not None
|
|
27
|
+
assert len(fail.execution_times) == 1
|