# Timer
Python code timer, support block wise and function wise
## Installation
```shell
pip install timer
```
## Usage
1. import
```py
from timer import timer
```
2. decorate without brackets
```py
@timer
def func(): ...
```
3. decorate with brackets
```py
@timer()
def func(): ...
```
4. decorate with name and time unit
```py
@timer('function name', 's')
def func(): ...
```
5. decorate with key word arguments
```py
@timer(name='function name', unit='s')
def func(): ...
```
6. block wise without object
```py
with timer():
...
```
7. block wise with object
```py
with timer() as t:
...
print(t.elapse)
```
## Sample Code
```python
import logging
import time
from timer import timer, get_timer
# default timer's logging level is logging.DEBUG
# so timer would print nothing if logging level is logging.INFO or higher
logging.basicConfig(level=logging.DEBUG)
# or you can change default timer's logging level
timer.set_level(logging.DEBUG)
# also you can get a timer with custom logging level with get_timer(level)
warning_timer = get_timer(logging.WARNING)
# explicit the timer's name and it's time unit
@timer('function:add', unit='s')
def add(a, b):
time.sleep(.1)
return a + b
# function name is timer's name for default
@timer
def sub(a, b):
time.sleep(.1)
return a - b
if __name__ == '__main__':
# 'timer' would be timer's name by default
with timer('time.sleep(2)') as t:
print(3)
time.sleep(1)
print(f'after time.sleep(1) once, t.elapse = {t.elapse}')
time.sleep(1)
print(f'after time.sleep(1) twice, t.elapse = {t.elapse}')
print(f'after with, t.elapse = {t.elapse}')
with warning_timer('test'):
pass
print(add(1, 1))
print(sub(2, 1))
```
### Outputs
```plain
3
after time.sleep(1) once, t.elapse = 1.003798776
after time.sleep(1) twice, t.elapse = 2.0052743459999998
DEBUG:timer.time.sleep(2): 2.006 s
after with, t.elapse = 2.005628447
WARNING:timer.test:start
WARNING:timer.test:cost 0 ms
DEBUG:timer.function:add: 0.105 s
2
DEBUG:timer.sub: 102 ms
1
```
## Special Thanks
[@Krzysztof S](https://github.com/papierukartka)
Raw data
{
"_id": null,
"home_page": "https://github.com/LucienShui/timer",
"name": "timer",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Lucien Shui",
"author_email": "lucien@lucien.ink",
"download_url": "https://files.pythonhosted.org/packages/19/6a/a702531c26a8c480dbb8b1c1b5381d74e0a8deb3d77516b8c5580342c46b/timer-0.2.3.tar.gz",
"platform": null,
"description": "# Timer\n\nPython code timer, support block wise and function wise\n\n## Installation\n\n```shell\npip install timer\n```\n\n## Usage\n\n1. import\n ```py\n from timer import timer\n ```\n\n2. decorate without brackets\n ```py\n @timer\n def func(): ...\n ```\n\n3. decorate with brackets\n ```py\n @timer()\n def func(): ...\n ```\n\n4. decorate with name and time unit\n ```py\n @timer('function name', 's')\n def func(): ...\n ```\n\n5. decorate with key word arguments\n ```py\n @timer(name='function name', unit='s')\n def func(): ...\n ```\n \n6. block wise without object\n\n ```py\n with timer():\n ...\n ```\n \n7. block wise with object\n \n ```py\n with timer() as t:\n ...\n print(t.elapse)\n ```\n\n## Sample Code\n\n```python\nimport logging\nimport time\n\nfrom timer import timer, get_timer\n\n# default timer's logging level is logging.DEBUG\n# so timer would print nothing if logging level is logging.INFO or higher\nlogging.basicConfig(level=logging.DEBUG)\n\n# or you can change default timer's logging level\ntimer.set_level(logging.DEBUG)\n\n# also you can get a timer with custom logging level with get_timer(level)\nwarning_timer = get_timer(logging.WARNING)\n\n\n# explicit the timer's name and it's time unit\n@timer('function:add', unit='s')\ndef add(a, b):\n time.sleep(.1)\n return a + b\n\n\n# function name is timer's name for default\n@timer\ndef sub(a, b):\n time.sleep(.1)\n return a - b\n\n\nif __name__ == '__main__':\n # 'timer' would be timer's name by default\n with timer('time.sleep(2)') as t:\n print(3)\n time.sleep(1)\n print(f'after time.sleep(1) once, t.elapse = {t.elapse}')\n time.sleep(1)\n print(f'after time.sleep(1) twice, t.elapse = {t.elapse}')\n print(f'after with, t.elapse = {t.elapse}')\n \n with warning_timer('test'):\n pass\n\n print(add(1, 1))\n print(sub(2, 1))\n```\n\n### Outputs\n\n```plain\n3\nafter time.sleep(1) once, t.elapse = 1.003798776\nafter time.sleep(1) twice, t.elapse = 2.0052743459999998\nDEBUG:timer.time.sleep(2): 2.006 s\nafter with, t.elapse = 2.005628447\nWARNING:timer.test:start\nWARNING:timer.test:cost 0 ms\nDEBUG:timer.function:add: 0.105 s\n2\nDEBUG:timer.sub: 102 ms\n1\n```\n\n## Special Thanks\n\n[@Krzysztof S](https://github.com/papierukartka)\n",
"bugtrack_url": null,
"license": null,
"summary": "Python Code Timer",
"version": "0.2.3",
"project_urls": {
"Homepage": "https://github.com/LucienShui/timer"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "76578ddce65925245465c6bb6901e99a372c5df6ad907ed7cc90ed8c601f861f",
"md5": "909cf52ae985aa788822c6eddd43ac17",
"sha256": "85a8d82fc217caf9ebf9f03326b14601e784000cbba1b6bac74701660fac60b2"
},
"downloads": -1,
"filename": "timer-0.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "909cf52ae985aa788822c6eddd43ac17",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7968,
"upload_time": "2024-05-21T02:51:25",
"upload_time_iso_8601": "2024-05-21T02:51:25.952776Z",
"url": "https://files.pythonhosted.org/packages/76/57/8ddce65925245465c6bb6901e99a372c5df6ad907ed7cc90ed8c601f861f/timer-0.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "196aa702531c26a8c480dbb8b1c1b5381d74e0a8deb3d77516b8c5580342c46b",
"md5": "83b2a64032f77902b038819b93c0da87",
"sha256": "59b307e0d0dcddd70ba69849f0e500e4740be87d30f0775ae353dc7c35ce4260"
},
"downloads": -1,
"filename": "timer-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "83b2a64032f77902b038819b93c0da87",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7436,
"upload_time": "2024-05-21T02:51:27",
"upload_time_iso_8601": "2024-05-21T02:51:27.323045Z",
"url": "https://files.pythonhosted.org/packages/19/6a/a702531c26a8c480dbb8b1c1b5381d74e0a8deb3d77516b8c5580342c46b/timer-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-21 02:51:27",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "LucienShui",
"github_project": "timer",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "timer"
}