Skip to main content

py-tictoc-timer

PyPI version Python
Released Unit Testing Publish Package codecov
Vulnerabilities License Downloads Code style: black

Time the execution of Python code using syntax similar to MATLAB's tic and toc functions.

Contents

Installation

  • Using pip:

    pip install py-tictoc-timer
    
  • Using pipenv:

    pipenv install py-tictoc-timer
    
  • Using poetry:

    1. In your pyproject.toml file, add:
      [tool.poetry.dependencies]
      py-tictoc-timer = "*"
      
      Then in the terminal, run:
      poetry install
      
    2. Or run:
      poetry add py-tictoc-timer
      
  • Using conda:

    conda install py-tictoc-timer
    

Usage

  • Basic usage:

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> tt = TicToc()
    >>> tt.tic()
    >>> sleep(1.1)
    >>> tt.toc()
    Elapsed time: 1secs
    
  • Within context manager:

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> with TicToc():
    ...     sleep(1.1)
    Elapsed time: 1secs
    
  • Within context manager using custom messages:

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> with TicToc(begin_message="start", end_message="end"):
    ...     sleep(1.1)
    start
    end: 1secs
    
  • Particularly helpful when running loops:

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> with TicToc(begin_message="Start loop", end_message="Time to run loop")
    ...     for value in ["first", "second", "Third"]:
    ...         with TicToc(f"- Time for {value}"):
    ...             sleep(1.1)
    Start loop
    - Time for first: 1secs
    - Time for second: 1secs
    - Time for Third: 1secs
    Time to run loop: 3secs
    
  • Custom message:

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> with TicToc("Total Time"):
    ...     sleep(1.1)
    Total time: 1secs
    
  • With restart during .tic():

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> tt = TicToc()
    >>> tt.tic(restart=True)
    >>> sleep(1.1)
    >>> toc()
    Elapsed time: 1secs
    >>> toc()
    Elapsed time: 1secs
    
  • With restart during .toc():

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> tt = TicToc()
    >>> tt.tic()
    >>> sleep(1.1)
    >>> tt.toc(restart=True)
    Elapsed time: 1secs
    >>> tt.toc()
    Elapsed time: 1secs
    
  • With restart using .rtoc():

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> tt = TicToc()
    >>> tt.tic()
    >>> sleep(1.1)
    >>> tt.rtoc()
    Elapsed time: 1secs
    >>> tt.toc()
    Elapsed time: 1secs
    
  • With time returned as value:

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> tt = TicToc()
    >>> tt.tic()
    >>> sleep(1.1)
    >>> value = tt.toc_value()
    >>> print(round(value, 1))
    1.1
    
  • With time returned as string:

    >>> from py_tictoc_timer.tictoc import TicToc
    >>> from time import sleep
    >>> tt = TicToc()
    >>> tt.tic()
    >>> sleep(1.1)
    >>> value = tt.toc_string()
    >>> print(value)
    1secs
    

Contribution

Contribution is always welcome!

  1. First, either fork or branch the main repo.
  2. Clone your forked/branched repo.
  3. Build your environment with any of the below options:
    1. With pipenv:
      if (-not (Test-Path .venv)) {mkdir .venv}
      python -m pipenv install --requirements requirements.txt --ignore-pipfile --skip-lock --no-site-packages
      python -m pipenv install --requirements requirements-dev.txt --dev --ignore-pipfile --skip-lock --no-site-packages
      python -m pipenv run pre-commit install
      
    2. With poetry on Windows:
      (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -
      python -m poetry run pre-commit install
      
    3. With poetry on Linux:
      curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -
      python -m poetry run pre-commit install
      
  4. Start contributing.
  5. Ensure you add additional Unit Test's to the test library for each new feature/functionality.
  6. Ensure that all the tests are passing successfully.
  7. When you're happy with the changes, raise a Pull Request to merge with the main branch again.

Tests

  • Run Black:

    pipenv run python -m black --safe py_tictoc_timer tests
    
  • Run PyTests:

    pipenv run python -m pytest --verbose --cov=py_tictoc_timer --cov-report=term --cov-report=html:cov-report/html --cov-report=xml:cov-report/xml/cov-report.xml
    
  • Run MyPy Tests:

    pipenv run mypy py_tictoc_timer --ignore-missing-imports --pretty --install-types --non-interactive
    

Credit

This package was inspired by a few other packages:

Why you should use py-tictoc-timer and not any of the others is because this package has:

  1. Better & more flexible restart to the timer
  2. Better custom messages during starting & ending the timer
  3. Enhanced usage within a context manager

Maintainers

Release files for py-tictoc-timer 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-tictoc-timer 2.1.1
File Size Uploaded
py_tictoc_timer-2.1.1.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-tictoc-timer 2.1.1
File Interpreter ABI Platform
py_tictoc_timer-2.1.1-py3-none-any.whl Python 3 none any Details

Total release size:13.8 kB

Release files / py_tictoc_timer-2.1.1.tar.gz

Download URL py_tictoc_timer-2.1.1.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
32d16c53bdc46711a4a523b4130d1d3e5d4c53cbff69b8b750537971e085515f
BLAKE2b-256 checksum
How to use checksums
4333940d56a35ee851d648ed8a1bfba4d31186804f85b140339e7c04fcb2d526
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.11 Linux/5.15.0-1037-azure

Release files / py_tictoc_timer-2.1.1-py3-none-any.whl

Download URL py_tictoc_timer-2.1.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dfa805596c4052f2b1646c8904ac271aab4603fe30460af2ec2a984590b2d257
BLAKE2b-256 checksum
How to use checksums
6efcd2a99587a4dcd6bda2048dd49be896aa5d6b1cbef2a3fd27c035cfc9a1a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.11 Linux/5.15.0-1037-azure

Release history Release notifications | RSS feed

This release

2.1.1 This release

2 release files

1.5.2

2 release files

1.2.1

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page