Skip to main content

A simple, yet useful stopwatch library.

Project description

swpy: A simple, yet useful stopwatch library for python

PyPI - Python Version PyPI CircleCI codecov PyPI - License

Requirements

  • Python 3.6+

Install

Just use pip to install.

pip install swpy

Usage

Basic Usage

Import Timer class from swpy, and use with statement to capsule the block where you want timing.

from swpy import Timer
from time import sleep

with Timer():
    sleep(1)

Or use start() and stop() function to controll the timer.

t = Timer()
t.start()
sleep(1)
t.stop()

And the output will look like below:

[timer-1557406243.3309178] started.
[timer-1557406243.3309178] finish time: 1.00 sec.

Features

Name the timer

You can name the timer to make it easy to recognize.

with Timer(name='test timer'):
    sleep(1)

Now the timer is renamed to test timer !

[test timer] started.
[test timer] finish time: 1.00 sec.

Lap time and Split time

There are two types to measuring time without stoping: lap time and split time. The figure below may help you to understand the differences.

split_lap_time

We prepared split and lap functions for this kind of usage. The examples are below.

# measure split time
with Timer('timer') as t:
    sleep(1)
    t.split()
    sleep(1)
    t.split()
    sleep(1)
    t.split()
# outptus
[timer] started.
[timer] split time:  1.00 sec.
[timer] split time:  2.01 sec.
[timer] split time:  3.01 sec.
[timer] finish time: 3.01 sec.
# measure lap time
with Timer('timer') as t:
    sleep(1)
    t.lap()
    sleep(1)
    t.lap()
    sleep(1)
    t.lap()
# outputs
[timer] started.
[timer] lap time:    1.00 sec.
[timer] lap time:    1.01 sec.
[timer] lap time:    1.00 sec.
[timer] finish time: 3.01 sec.

And you can name your lap/split time in the case of measuring several tasks in a single run as below.

with Timer('task timer') as t:
    task1()
    t.lap('task1')
    task2()
    t.lap('task2')
# outputs
[task timer] started.
[task timer] [task1] lap time:    3.69 sec.
[task timer] [task2] lap time:    4.21 sec.
[task timer] finish time: 7.91 sec.

Use your own logger

You can use your own logger instead of the default print.

from logzero import logger
import logging

with Timer(name='test timer', logger=logger, level=logging.DEBUG):
    sleep(1)

It will output using logger.

[D 190510 14:41:59 swpy:15] [test timer] started.
[D 190510 14:42:00 swpy:15] [test timer] finish time: 1.01 sec.

Define your own callback

Sometimes, we want to do something after the job has done like notifying the result to slack, executing the next process and so on. Callback feature will help you to do those.

# define a slack notification function
import requests, json
def send_slack(msg):
    requests.post(SLACK_URL, json.dumps({'text': msg}))

# just specify the callback argument
with Timer(name='experiment-1', callback=send_slack):
    sleep(1)

License

MIT

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

swpy-0.1.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

swpy-0.1.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file swpy-0.1.2.tar.gz.

File metadata

  • Download URL: swpy-0.1.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.6.8 Linux/4.15.0-1035-aws

File hashes

Hashes for swpy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e9c62941386e826247c384a95bf6a38f9a95449e742cb5357ffe1c2afe5d51aa
MD5 8b9649228528ceb93c5cc019d82b3e8e
BLAKE2b-256 fef5e88b8dfb5fd5f126634339bde1af74f397b26f1826063f0576a52010e5d7

See more details on using hashes here.

File details

Details for the file swpy-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: swpy-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.6.8 Linux/4.15.0-1035-aws

File hashes

Hashes for swpy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 72a6262a7f6853f17791351cb9e772a9cfb298253e1586f68f26922c9f08fdf4
MD5 ef182cb527d597d828716cf573318533
BLAKE2b-256 92f3ef4cbc730d84c9fd1b5a6dca411cf2493675dbe201af93455f75b00461cc

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