Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
v0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhan Li committed Feb 7, 2021
1 parent 9c0677c commit c4b07a2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 46 deletions.
42 changes: 14 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ pip install timework
import timework as tw
```

### timework.timer
### Statement `with`

***A decorator measuring the execution time.***
#### timework.Stopwatch

**`timework.TimeError`** contains three parts:
**Todo**

- `TimeError.message` ***string,*** \<*decorated function name*\>: \<*time used*\> seconds used
- `TimeError.result` return values of the function being decorated
- `TimeError.detail` ***float,*** time used
### Decorator `@`

***Notice:*** In **`@timer`** decorator, `timeout` is used to raise a `Error` **after** the decorated function finishes, but fails to finish within `timeout` seconds. If you want to **terminate** the function with a `timeout` limit, please use **`@limit`**.
**Todo:** read docstrings and tests for more details

#### timework.timer

> measuring the execution time.
```python
import logging
Expand All @@ -54,38 +56,24 @@ def timer_demo_b():
while i < 2 ** 24:
i += 1
return i

@tw.timer(timeout=1)
def timer_demo_c():
i = 0
while i < 2 ** 25:
i += 1
return i
```
```python
a = timer_demo_a()
b = timer_demo_b()

try:
c = timer_demo_c()
except tw.TimeError as e:
print('error:', e.message)
c = e.result

print(a, b, c)
print(a, b)
```
```
WARNING:root:timer_demo_a: 0.496672 seconds used
START: Tue Feb 18 15:06:45 2020
FINISH: Tue Feb 18 15:06:46 2020
timer_demo_b: 0.989352 seconds used
error: timer_demo_c: 1.9817 seconds used
8388608 16777216 33554432
8388608 16777216
```

### timework.limit
#### timework.limit

***A decorator limiting the execution time.***
> limiting the execution time.
**`timework.TimeError`** only contains:

Expand Down Expand Up @@ -121,8 +109,6 @@ result: 16
limit_demo: 3 seconds exceeded
```

*You can read docstrings for more details.*

## License

MIT License &copy; <a href="https://github.com/bugstop" style="color: black !important;text-decoration: none !important;">bugstop</a>
MIT License &copy; <a href="https://github.com/bugstop" style="color: black !important; text-decoration: none !important;">bugstop</a>
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="timework",
version="0.3.1",
version="0.3.2",
author="bugstop",
author_email="pypi@isaacx.com",
description="measure / limit the function execution time, cross-platform",
Expand Down
4 changes: 2 additions & 2 deletions timework/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
timework 0.3.1
timework 0.3.2
MIT License © bugstop
Expand All @@ -18,5 +18,5 @@
from .timework import *

__name__ = 'timework'
__version__ = '0.3.1'
__version__ = '0.3.2'
__all__ = ['timer', 'limit', 'Stopwatch']
1 change: 0 additions & 1 deletion timework/test_with-statement.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from . import timework as tw

import time
from random import randint

def test_no_split():
"""
Expand Down
18 changes: 4 additions & 14 deletions timework/timework.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, message: str, result=None, detail=None):
Args:
message: timeout error message
result: return values of the decorated function (if have)
result: return values of the function being decorated
detail: more information (if have)
"""
self.message = message
Expand All @@ -57,21 +57,11 @@ def your_function():
Args:
output: A function object that specifies where to log messages.
For example: print.
For example: print. timework.nil does not log messages.
detail: A boolean value, whether to print start and end time.
Returns:
Exactly the return values of the inner function that is being decorated.
In this case, the process finishes within [timeout] seconds.
Raises:
TimeError: This error does not occur until the inner function
terminates, but fails to finish within [timeout] seconds.
A TimeError object contains:
TimeError.message: timeout message
TimeError.result: return values
TimeError.detail: used run time
Exactly the return values of the inner function just as normal.
"""

def decorator(func):
Expand Down Expand Up @@ -121,7 +111,7 @@ def your_function():
not yet finishes, then raise TimeError and stop the inner function.
Returns:
Exactly the return values of the inner function that is being decorated.
Exactly the return values of the inner function just as normal.
In this case, the process finishes within [timeout] seconds.
Raises:
Expand Down

0 comments on commit c4b07a2

Please sign in to comment.