Skip to main content

Simple customizable Python timer.

Project description

Asdftimer

asdftimer is a simple Python utility class for measuring elapsed time. It provides an easy-to-use interface for timing code execution, with support for logging and context management.

Features

  • Measure elapsed time.
  • Log elapsed time using a custom logger or print().
  • Restart the timer at any point.
  • Use as a context manager for automatic timing.

Installation

pip install asdftimer

Usage

Basic Usage

from asdftimer import Timer
from time import sleep

timer = Timer()
time.sleep (2.4)
elapsed_time = timer.end()
# Output: AsdfTimer took 2.40 seconds

Using a Custom Logger

from asdftimer.timer import Timer
import logging
logger = logging.getLogger(__name__)

timer = Timer(name="LoggedTimer", logger=logger)
# ur code here
timer.end()
# Output: LoggedTimer took X seconds'

Timing a context

from asdftimer.timer import Timer
with Timer(name="ContextTimer") as timer:
    pass # ur code here
# Output: ContextTimer took X seconds

API Reference

Timer

__init__(name="AsdfTimer", logger=None, disable_print=False, print_digits=2)

  • name (str): The name of the timer. Defaults to "AsdfTimer".
  • logger (Logger): A logger instance for logging. Uses print() if None.
  • disable_print (bool): Whether to disable logging/printing the elapsed time. Defaults to False.
  • print_digits (int): Number of decimal places to print for elapsed time. Defaults to 2.

end() -> float

Outputs and returns the elapsed time in seconds.

restart() -> None

Restarts the timer.

__enter__()

Starts the timer when used as a context manager.

__exit__(exc_type, exc_value, traceback)

Stops the timer and outputs the elapsed time when exiting the context.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to improve asdftimer.

Author

Developed by Nibs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asdftimer-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asdftimer-0.1.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file asdftimer-0.1.0.tar.gz.

File metadata

  • Download URL: asdftimer-0.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.16 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for asdftimer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3ebba2af3aeec89860eee16437f2e1ee30ee102b46ee85fdc5c8146997a22ee9
MD5 cf2515998427588bca358c47f399b25e
BLAKE2b-256 e5232c941cf1058014bbd14e5d71c1e1f45b792045664a290c376d54220457aa

See more details on using hashes here.

File details

Details for the file asdftimer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: asdftimer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.16 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for asdftimer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23f6b42e2efb69a91de9787b9396fb8ced90bea212c55187496eb050a8508f83
MD5 c98f48df229fe968e3e78d51e6c030de
BLAKE2b-256 ff1629fe2f58bca3b4ae5c3eba405d371ad8a3e82a7e1f41afdda5f45f66edb1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page