-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
234 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
fail_fast: true | ||
|
||
repos: | ||
- repo: https://github.com/ambv/black | ||
rev: 22.10.0 | ||
hooks: | ||
- id: black | ||
- repo: https://gitlab.com/pycqa/flake8 | ||
rev: 3.9.2 | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.3.0 | ||
hooks: | ||
- id: flake8 | ||
- repo: https://github.com/timothycrosley/isort | ||
rev: 5.10.1 | ||
- id: check-yaml | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace | ||
- repo: https://github.com/psf/black | ||
rev: 22.10.0 | ||
hooks: | ||
- id: isort | ||
- id: black |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"python.analysis.typeCheckingMode": "basic", | ||
"[python]": { | ||
"editor.defaultFormatter": "ms-python.black-formatter" | ||
}, | ||
"python.formatting.provider": "none", | ||
"python.testing.pytestArgs": [ | ||
"tests" | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) [year] [author] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,49 @@ | ||
# Project Name | ||
# Execy | ||
|
||
Short project description | ||
The ececy exec_time decorator is a utility that measures the execution time of a function. It can be used to easily track and log the time taken by a function to execute, both for synchronous and asynchronous functions. | ||
|
||
## Table of Contents | ||
|
||
- [Introduction](#introduction) | ||
- [Features](#features) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Contributing](#contributing) | ||
- [License](#license) | ||
- [Execy](#execy) | ||
- [Table of Contents](#table-of-contents) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
|
||
## Introduction | ||
## Installation | ||
|
||
Provide an introduction to your project here. Explain what it does and why it's useful. | ||
```bash | ||
pip install execy | ||
``` | ||
|
||
## Features | ||
## Usage | ||
|
||
List the main features and functionalities of your project. | ||
To use the exec_time decorator, follow these steps: | ||
```python | ||
from execy import exec_time | ||
import asyncio | ||
import time | ||
|
||
## Installation | ||
@exec_time | ||
def sync_function(): | ||
# Simulate a time-consuming synchronous task | ||
time.sleep(2) | ||
|
||
Provide instructions on how to install and set up your project. Include any dependencies and installation steps required. | ||
@exec_time | ||
async def async_function(): | ||
# Simulate a time-consuming asynchronous task | ||
await asyncio.sleep(2) | ||
|
||
## Usage | ||
# Call the decorated synchronous function | ||
sync_function() | ||
|
||
# Call the decorated asynchronous function | ||
asyncio.run(async_function()) | ||
``` | ||
|
||
The result will be | ||
|
||
Provide examples and explanations on how to use your project. Include code snippets or detailed instructions to help users get started. | ||
```bash | ||
Function 'sync_function' executed in 2.0001089572906494 seconds. | ||
Function 'async_function' executed in 2.001432180404663 seconds. | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,24 @@ | ||
[project] | ||
name = "exectime" | ||
name = "execy" | ||
version = "0.1.0" | ||
description = "Add a short description here" | ||
authors = [ | ||
{ name = "hglong16", email = "[email protected]" } | ||
] | ||
dependencies = [] | ||
dependencies = ["pytest-asyncio~=0.21.0", "setuptools~=67.8.0", "wheel~=0.40.0", "twine~=4.0.2"] | ||
readme = "README.md" | ||
requires-python = ">= 3.8" | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[tool.rye.scripts] | ||
build = "python setup.py sdist bdist_wheel" | ||
upload = "twine upload dist/*" | ||
|
||
[tool.rye] | ||
managed = true | ||
dev-dependencies = ["pytest~=7.3.1", "black~=23.3.0", "isort~=5.12.0"] | ||
[tool.hatch.metadata] | ||
allow-direct-references = true | ||
|
||
[tool.flake8] | ||
max-line-length = 75 | ||
extend-ignore = ["D203", "E203", "E251", "E266", "E302", "E305", "E401", "E402", "E501", "F401", "F403", "W503"] | ||
exclude = [".git", "__pycache__", "dist"] | ||
max-complexity = 10 | ||
|
||
[tool.isort] | ||
atomic = true | ||
profile = "black" | ||
line_length = 75 | ||
skip_gitignore = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# setup.py | ||
|
||
from setuptools import setup, find_packages | ||
|
||
setup( | ||
name="execy", | ||
version="1.0.0", | ||
author="hglong16", | ||
author_email="[email protected]", | ||
description="A package for measuring execution time", | ||
packages=find_packages("src"), | ||
package_dir={"": "src"}, | ||
python_requires=">=3.6", | ||
license="MIT", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +0,0 @@ | ||
def hello(): | ||
return "Hello from exectime!" | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import time | ||
import asyncio | ||
from functools import wraps | ||
|
||
|
||
def exec_time(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
if asyncio.iscoroutinefunction(func): | ||
return measure_execution_time_async(func, *args, **kwargs) | ||
else: | ||
return measure_execution_time_sync(func, *args, **kwargs) | ||
|
||
def measure_execution_time_sync(func, *args, **kwargs): | ||
start_time = time.time() | ||
result = func(*args, **kwargs) | ||
end_time = time.time() | ||
execution_time = end_time - start_time | ||
wrapper.execution_time = execution_time # type: ignore | ||
print(f"Function '{func.__name__}' executed in {execution_time} seconds.") | ||
return result | ||
|
||
async def measure_execution_time_async(func, *args, **kwargs): | ||
start_time = time.time() | ||
result = await func(*args, **kwargs) | ||
end_time = time.time() | ||
execution_time = end_time - start_time | ||
wrapper.execution_time = execution_time # type: ignore | ||
print(f"Function '{func.__name__}' executed in {execution_time} seconds.") | ||
return result | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import time | ||
import asyncio | ||
import pytest | ||
from exectime.exec import exec_time | ||
|
||
|
||
@exec_time | ||
async def async_function(): | ||
# Simulate a time-consuming task asynchronously | ||
await asyncio.sleep(2) | ||
|
||
|
||
def test_execution_time(): | ||
@exec_time | ||
def time_consuming_function(): | ||
# Simulate a time-consuming task | ||
time.sleep(2) | ||
|
||
# Call the decorated function | ||
time_consuming_function() | ||
|
||
# Assert that the execution time is within an acceptable range | ||
assert time_consuming_function.execution_time >= 1.9 | ||
assert time_consuming_function.execution_time <= 2.1 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_execution_time(): | ||
# Call the decorated async function | ||
await async_function() | ||
|
||
# Assert that the execution time is within an acceptable range | ||
assert async_function.execution_time >= 1.9 | ||
assert async_function.execution_time <= 2.1 |