timevery


Nametimevery JSON
Version 0.1.3 PyPI version JSON
download
home_pageNone
SummaryMonitor the time cost of your code easily.
upload_time2024-06-18 11:33:52
maintainerNone
docs_urlNone
authorNone
requires_pythonNone
licenseMIT License Copyright (c) 2024 ShiChangshan 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.
keywords timer
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # timevery

Python timer for measuring execution time.

[![PyPI License](https://img.shields.io/pypi/l/timevery.svg)](https://pypi.org/project/timevery)
[![PyPI Version](https://img.shields.io/pypi/v/timevery.svg)](https://pypi.org/project/timevery)

## Quick Start

- Install `timevery`:

    ```bash
    pip install timevery
    ```

You can use `timevery.Timer` in several different ways:

1. As a **class**:

    ```python
    a = Timer("Detect", show_freq=True, logger=print)

    for i in range(5):
        a.start()

        time.sleep(0.1)
        a.lap("detect")

        if i % 2 == 0:
            time.sleep(0.1)
            a.lap("segment")

        time.sleep(0.2)
        a.lap("plot")
        a.stop()
    a.report()
    ```

    <details>
    <summary>Click to see the output</summary>

    ```bash
    >>>
        Detect started.
        Elapsed time of detect: 0.1002 seconds.
        Elapsed time of segment: 0.1003 seconds.
        Elapsed time of plot: 0.2003 seconds.
        Elapsed time of Detect: 0.4009 seconds.  Frequency: 2.49 Hz
        Detect started.
        Elapsed time of detect: 0.1001 seconds.
        Elapsed time of plot: 0.2004 seconds.
        Elapsed time of Detect: 0.3006 seconds.  Frequency: 3.33 Hz
        Detect started.
        Elapsed time of detect: 0.1001 seconds.
        Elapsed time of segment: 0.1002 seconds.
        Elapsed time of plot: 0.2004 seconds.
        Elapsed time of Detect: 0.4008 seconds.  Frequency: 2.49 Hz
        Detect started.
        Elapsed time of detect: 0.1001 seconds.
        Elapsed time of plot: 0.2004 seconds.
        Elapsed time of Detect: 0.3006 seconds.  Frequency: 3.33 Hz
        Detect started.
        Elapsed time of detect: 0.1002 seconds.
        Elapsed time of segment: 0.1003 seconds.
        Elapsed time of plot: 0.2004 seconds.
        Elapsed time of Detect: 0.4010 seconds.  Frequency: 2.49 Hz

        |  Name   | Total(s) | Average(s) | Freq(Hz) | Percent(%) | Count |  Min   |  Max   |
        |---------|----------|------------|----------|------------|-------|--------|--------|
        | Detect  |  1.8040  |   0.3608   |  2.7716  |  100.0000  |   5   | 0.3006 | 0.4010 |
        | detect  |  0.5008  |   0.1002   |  9.9831  |  27.7627   |   5   | 0.1001 | 0.1002 |
        | segment |  0.3008  |   0.1003   |  9.9739  |  16.6730   |   3   | 0.1002 | 0.1003 |
        |  plot   |  1.0018  |   0.2004   |  4.9909  |  55.5327   |   5   | 0.2003 | 0.2004 |
    ```

    </details>

2. As a **context manager**:

    ```python
    with Timer("MakeRobot", show_report=True) as t:
        time.sleep(1)
        t.lap("foot")
        time.sleep(1)
        t.lap("hand")
        time.sleep(1)
        t.lap("head")
        time.sleep(2)
        t.lap("body")
        time.sleep(1)
        t.lap("combine")
    ```

    <details>
    <summary>Click to see the output</summary>

    ```bash
    >>>
        MakeRobot started.
        Elapsed time of foot: 1.0011 seconds.
        Elapsed time of hand: 1.0012 seconds.
        Elapsed time of head: 1.0010 seconds.
        Elapsed time of body: 2.0021 seconds.
        Elapsed time of combine: 1.0012 seconds.
        Elapsed time of MakeRobot: 6.0068 seconds.
        |   Name    | Total(s) | Average(s) | Freq(Hz) | Percent(%) | Count |  Min   |  Max   |
        |-----------|----------|------------|----------|------------|-------|--------|--------|
        | MakeRobot |  6.0068  |   6.0068   |  0.1665  |  100.0000  |   1   | 6.0068 | 6.0068 |
        |   foot    |  1.0011  |   1.0011   |  0.9989  |  16.6663   |   1   | 1.0011 | 1.0011 |
        |   hand    |  1.0012  |   1.0012   |  0.9988  |  16.6679   |   1   | 1.0012 | 1.0012 |
        |   head    |  1.0010  |   1.0010   |  0.9990  |  16.6640   |   1   | 1.0010 | 1.0010 |
        |   body    |  2.0021  |   2.0021   |  0.4995  |  33.3309   |   1   | 2.0021 | 2.0021 |
        |  combine  |  1.0012  |   1.0012   |  0.9988  |  16.6674   |   1   | 1.0012 | 1.0012 |
    ```

    </details>

3. As a **decorator**:

    ```python
    @Timer("Locate")
    def locate():
        time.sleep(1)
        print("located")

    locate()
    ```

    <details>
    <summary>Click to see the output</summary>

    ```bash
    >>>
        Locate started.
        located
        Elapsed time of Locate: 1.0011 seconds.
    ```

    </details>

Some showcases are available in the [showcase.py](showcase.py).

## API

### `timevery.Timer()`

- `Timer()`

    ```python
    class Timer(ContextDecorator):
        def __init__(
            self,
            name: Optional[str] = "Timer",
            text: Optional[str] = "Elapsed time of {name}: {seconds:0.4f} seconds. ",
            initial_text: Union[bool, str] = False,
            period: Optional[float] = None,
            show_freq: Optional[bool] = False,
            show_report: Optional[bool] = False,
            auto_restart: Optional[bool] = False,
            logger: Optional[Callable] = print,
            time_function: Optional[Callable] = time.perf_counter,
        ):
            """Create a Timer.

            Args:
                name (Optional[str], optional): Timer's name. Defaults to "Timer".
                text (Optional[str]): Then text shown when `stop()` or `lap()` is called.
                    Defaults to "Elapsed time of {name}: {seconds:0.4f} seconds. ".
                    Available substitutions: {name}, {milliseconds}, {seconds}, {minutes}.
                initial_text (Union[bool, str], optional): The text shown when `start()` is called. Defaults to False.
                period (Optional[float]): Period of the timer. Defaults to None. Use with `sleep_until_next_period()`, `stop_and_sleep_until_next_period()`, `sleep_until_next_period_and_stop()`.
                show_freq (Optional[str]): Show frequency when `stop()` is called if is True. Defaults to False.
                show_report (Optional[str]): Show report when `stop()` is called if is True. Defaults to False.
                auto_restart: Optional[bool]: Restart the timer when `start()` is called if is True. Defaults to False.
                logger (Optional[Callable], optional): Callable to show logs. Defaults to `print`.
                time_function (Optional[Callable], optional): The function can return a number to indicate the time it be called.
                    Defaults to `time.perf_counter()` in seconds. `time.time()`, `time.monotonic()`, `time.process_time()` are also available.
            """
    ```

- `Timer.start()`
- `Timer.stop()`
- `Timer.lap(name: Optional[str] = None)`
- `Timer.report()`

## Acknowledgement

Thanks to [this tutorial](https://realpython.com/python-timer/) and the [`codetiming`](https://github.com/realpython/codetiming) for the inspiration.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "timevery",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "timer",
    "author": null,
    "author_email": "Shi Changshan <changshanshi@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/e2/f4/d6689a05cef259fbf0d71b11fa071979f459b5bb49a99aa27d6957e7de8e/timevery-0.1.3.tar.gz",
    "platform": null,
    "description": "# timevery\n\nPython timer for measuring execution time.\n\n[![PyPI License](https://img.shields.io/pypi/l/timevery.svg)](https://pypi.org/project/timevery)\n[![PyPI Version](https://img.shields.io/pypi/v/timevery.svg)](https://pypi.org/project/timevery)\n\n## Quick Start\n\n- Install `timevery`:\n\n    ```bash\n    pip install timevery\n    ```\n\nYou can use `timevery.Timer` in several different ways:\n\n1. As a **class**:\n\n    ```python\n    a = Timer(\"Detect\", show_freq=True, logger=print)\n\n    for i in range(5):\n        a.start()\n\n        time.sleep(0.1)\n        a.lap(\"detect\")\n\n        if i % 2 == 0:\n            time.sleep(0.1)\n            a.lap(\"segment\")\n\n        time.sleep(0.2)\n        a.lap(\"plot\")\n        a.stop()\n    a.report()\n    ```\n\n    <details>\n    <summary>Click to see the output</summary>\n\n    ```bash\n    >>>\n        Detect started.\n        Elapsed time of detect: 0.1002 seconds.\n        Elapsed time of segment: 0.1003 seconds.\n        Elapsed time of plot: 0.2003 seconds.\n        Elapsed time of Detect: 0.4009 seconds.  Frequency: 2.49 Hz\n        Detect started.\n        Elapsed time of detect: 0.1001 seconds.\n        Elapsed time of plot: 0.2004 seconds.\n        Elapsed time of Detect: 0.3006 seconds.  Frequency: 3.33 Hz\n        Detect started.\n        Elapsed time of detect: 0.1001 seconds.\n        Elapsed time of segment: 0.1002 seconds.\n        Elapsed time of plot: 0.2004 seconds.\n        Elapsed time of Detect: 0.4008 seconds.  Frequency: 2.49 Hz\n        Detect started.\n        Elapsed time of detect: 0.1001 seconds.\n        Elapsed time of plot: 0.2004 seconds.\n        Elapsed time of Detect: 0.3006 seconds.  Frequency: 3.33 Hz\n        Detect started.\n        Elapsed time of detect: 0.1002 seconds.\n        Elapsed time of segment: 0.1003 seconds.\n        Elapsed time of plot: 0.2004 seconds.\n        Elapsed time of Detect: 0.4010 seconds.  Frequency: 2.49 Hz\n\n        |  Name   | Total(s) | Average(s) | Freq(Hz) | Percent(%) | Count |  Min   |  Max   |\n        |---------|----------|------------|----------|------------|-------|--------|--------|\n        | Detect  |  1.8040  |   0.3608   |  2.7716  |  100.0000  |   5   | 0.3006 | 0.4010 |\n        | detect  |  0.5008  |   0.1002   |  9.9831  |  27.7627   |   5   | 0.1001 | 0.1002 |\n        | segment |  0.3008  |   0.1003   |  9.9739  |  16.6730   |   3   | 0.1002 | 0.1003 |\n        |  plot   |  1.0018  |   0.2004   |  4.9909  |  55.5327   |   5   | 0.2003 | 0.2004 |\n    ```\n\n    </details>\n\n2. As a **context manager**:\n\n    ```python\n    with Timer(\"MakeRobot\", show_report=True) as t:\n        time.sleep(1)\n        t.lap(\"foot\")\n        time.sleep(1)\n        t.lap(\"hand\")\n        time.sleep(1)\n        t.lap(\"head\")\n        time.sleep(2)\n        t.lap(\"body\")\n        time.sleep(1)\n        t.lap(\"combine\")\n    ```\n\n    <details>\n    <summary>Click to see the output</summary>\n\n    ```bash\n    >>>\n        MakeRobot started.\n        Elapsed time of foot: 1.0011 seconds.\n        Elapsed time of hand: 1.0012 seconds.\n        Elapsed time of head: 1.0010 seconds.\n        Elapsed time of body: 2.0021 seconds.\n        Elapsed time of combine: 1.0012 seconds.\n        Elapsed time of MakeRobot: 6.0068 seconds.\n        |   Name    | Total(s) | Average(s) | Freq(Hz) | Percent(%) | Count |  Min   |  Max   |\n        |-----------|----------|------------|----------|------------|-------|--------|--------|\n        | MakeRobot |  6.0068  |   6.0068   |  0.1665  |  100.0000  |   1   | 6.0068 | 6.0068 |\n        |   foot    |  1.0011  |   1.0011   |  0.9989  |  16.6663   |   1   | 1.0011 | 1.0011 |\n        |   hand    |  1.0012  |   1.0012   |  0.9988  |  16.6679   |   1   | 1.0012 | 1.0012 |\n        |   head    |  1.0010  |   1.0010   |  0.9990  |  16.6640   |   1   | 1.0010 | 1.0010 |\n        |   body    |  2.0021  |   2.0021   |  0.4995  |  33.3309   |   1   | 2.0021 | 2.0021 |\n        |  combine  |  1.0012  |   1.0012   |  0.9988  |  16.6674   |   1   | 1.0012 | 1.0012 |\n    ```\n\n    </details>\n\n3. As a **decorator**:\n\n    ```python\n    @Timer(\"Locate\")\n    def locate():\n        time.sleep(1)\n        print(\"located\")\n\n    locate()\n    ```\n\n    <details>\n    <summary>Click to see the output</summary>\n\n    ```bash\n    >>>\n        Locate started.\n        located\n        Elapsed time of Locate: 1.0011 seconds.\n    ```\n\n    </details>\n\nSome showcases are available in the [showcase.py](showcase.py).\n\n## API\n\n### `timevery.Timer()`\n\n- `Timer()`\n\n    ```python\n    class Timer(ContextDecorator):\n        def __init__(\n            self,\n            name: Optional[str] = \"Timer\",\n            text: Optional[str] = \"Elapsed time of {name}: {seconds:0.4f} seconds. \",\n            initial_text: Union[bool, str] = False,\n            period: Optional[float] = None,\n            show_freq: Optional[bool] = False,\n            show_report: Optional[bool] = False,\n            auto_restart: Optional[bool] = False,\n            logger: Optional[Callable] = print,\n            time_function: Optional[Callable] = time.perf_counter,\n        ):\n            \"\"\"Create a Timer.\n\n            Args:\n                name (Optional[str], optional): Timer's name. Defaults to \"Timer\".\n                text (Optional[str]): Then text shown when `stop()` or `lap()` is called.\n                    Defaults to \"Elapsed time of {name}: {seconds:0.4f} seconds. \".\n                    Available substitutions: {name}, {milliseconds}, {seconds}, {minutes}.\n                initial_text (Union[bool, str], optional): The text shown when `start()` is called. Defaults to False.\n                period (Optional[float]): Period of the timer. Defaults to None. Use with `sleep_until_next_period()`, `stop_and_sleep_until_next_period()`, `sleep_until_next_period_and_stop()`.\n                show_freq (Optional[str]): Show frequency when `stop()` is called if is True. Defaults to False.\n                show_report (Optional[str]): Show report when `stop()` is called if is True. Defaults to False.\n                auto_restart: Optional[bool]: Restart the timer when `start()` is called if is True. Defaults to False.\n                logger (Optional[Callable], optional): Callable to show logs. Defaults to `print`.\n                time_function (Optional[Callable], optional): The function can return a number to indicate the time it be called.\n                    Defaults to `time.perf_counter()` in seconds. `time.time()`, `time.monotonic()`, `time.process_time()` are also available.\n            \"\"\"\n    ```\n\n- `Timer.start()`\n- `Timer.stop()`\n- `Timer.lap(name: Optional[str] = None)`\n- `Timer.report()`\n\n## Acknowledgement\n\nThanks to [this tutorial](https://realpython.com/python-timer/) and the [`codetiming`](https://github.com/realpython/codetiming) for the inspiration.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 ShiChangshan  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. ",
    "summary": "Monitor the time cost of your code easily.",
    "version": "0.1.3",
    "project_urls": {
        "Homepage": "https://github.com/Bardreamaster/timevery"
    },
    "split_keywords": [
        "timer"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "42a77a3294683a38d5b5462805631f4fff0e613ee16c09b3d5a8dedebb5f0bd4",
                "md5": "ed6117ca608b27a3a6b1bc6abffd7c1d",
                "sha256": "a7f84d444c91dee00e840b996ec97a7ff8e9f204b7beee831c9fdb5bc0eab92b"
            },
            "downloads": -1,
            "filename": "timevery-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ed6117ca608b27a3a6b1bc6abffd7c1d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7323,
            "upload_time": "2024-06-18T11:33:48",
            "upload_time_iso_8601": "2024-06-18T11:33:48.803245Z",
            "url": "https://files.pythonhosted.org/packages/42/a7/7a3294683a38d5b5462805631f4fff0e613ee16c09b3d5a8dedebb5f0bd4/timevery-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e2f4d6689a05cef259fbf0d71b11fa071979f459b5bb49a99aa27d6957e7de8e",
                "md5": "6f616914d8380dfe05fa6f8d6310aab0",
                "sha256": "2e832a6f735d510fda7ebf16fba70e6b61d859a86da1519fb783cc11fc9be473"
            },
            "downloads": -1,
            "filename": "timevery-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "6f616914d8380dfe05fa6f8d6310aab0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7536,
            "upload_time": "2024-06-18T11:33:52",
            "upload_time_iso_8601": "2024-06-18T11:33:52.235972Z",
            "url": "https://files.pythonhosted.org/packages/e2/f4/d6689a05cef259fbf0d71b11fa071979f459b5bb49a99aa27d6957e7de8e/timevery-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-18 11:33:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Bardreamaster",
    "github_project": "timevery",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "timevery"
}
        
Elapsed time: 0.27287s