Skip to main content

Raccoon Simple StopWatch

Project description

Simple Stopwatch

Quality Gate Status Maintainability Rating Reliability Rating Security Rating Coverage

Bugs Code Smells Duplicated Lines (%) Lines of Code Technical Debt Vulnerabilities

This package is a simple stopwatch implementation I use on several projects and got tired of copying and pasting this code all around.

The default elapsed time format is DAYS.HOURS:MINUTES:SECONDS.MS

Examples

01. Creating new stopwatch

from raccoon_simple_stopwatch.stopwatch import StopWatch

sw = StopWatch()

The snippet above shows how to instantiate a StopWatch. Creating an instance like this will not start the stopwatch.

02. Creating new stopwatch

from raccoon_simple_stopwatch.stopwatch import StopWatch

sw = StopWatch(True)

# or
sw2 = StopWatch(auto_start=True)

The snippet above shows how to instantiate a StopWatch. Creating an instance like this WILL autostart the stopwatch.

03. Getting the final elapsed time

import time
from raccoon_simple_stopwatch.stopwatch import StopWatch

sw = StopWatch(True)
time.sleep(10)
elapsed_time = sw.end()
print(elapsed_time)

Output:

0:00:10.001899

This snippet auto starts the stopwatch, waits for 10 seconds, stops it and then gets the total elapsed time.

04. Getting current/partial elapsed time

import time
from raccoon_simple_stopwatch.stopwatch import StopWatch

sw = StopWatch(True)
for i in range(10):
    print(f"[{i:02d}] Partial: {sw.elapsed()}")
    time.sleep(1)
elapsed_time = sw.end()
print(f"Total Elapsed time: {elapsed_time}")

Output:

[00] Partial: 0:00:00.000010
[01] Partial: 0:00:01.008056
[02] Partial: 0:00:02.020088
[03] Partial: 0:00:03.030782
[04] Partial: 0:00:04.040916
[05] Partial: 0:00:05.054144
[06] Partial: 0:00:06.062206
[07] Partial: 0:00:07.075621
[08] Partial: 0:00:08.088999
[09] Partial: 0:00:09.100672
Total Elapsed time: 0:00:10.115585

This snippet auto starts the stopwatch, gets the partial elapsed time 10 times during 10 seconds, stops it and then gets the total elapsed time.

05. Getting current/partial elapsed time in timedelta format

import time
from raccoon_simple_stopwatch.stopwatch import StopWatch

sw = StopWatch(True)
for i in range(10):
    print(f"[{i:02d}] Partial: {sw.elapsed(True).total_seconds()}")
    time.sleep(1)
elapsed_time = sw.end(True).total_seconds()
print(f"Total Elapsed time: {elapsed_time}")

Output:

[00] Partial: 1.5e-05
[01] Partial: 1.009903
[02] Partial: 2.022595
[03] Partial: 3.033897
[04] Partial: 4.046012
[05] Partial: 5.056192
[06] Partial: 6.068632
[07] Partial: 7.068943
[08] Partial: 8.084072
[09] Partial: 9.097145
Total Elapsed time: 10.111292

This works just like the previous snippet, but the elapsed time is retrieved in timedelta format, and then we call total_seconds() functions to get the elapsed time in seconds.

06. Getting the moment when the stopwatch started (UTC and local)

from raccoon_simple_stopwatch.stopwatch import StopWatch

sw = StopWatch()  # Local datetime
print(sw.start_datetime)
sw.start()
print(f"{sw.start_datetime} | {sw.start_datetime.tzinfo}")
sw2 = StopWatch(use_utc=True)  # UTC datetime
print(sw2.start_datetime)
sw2.start()
print(f"{sw2.start_datetime} | {sw2.start_datetime.tzinfo}")

Output:

None
2021-12-23 20:43:50.585377 | None
None
2021-12-23 23:43:50.585377 | None

Note: All examples involving start_datetime can also be applied to stop_datetime.

07. Getting the moment when the stopwatch started, making the datetime timezone aware.

from raccoon_simple_stopwatch.stopwatch import StopWatch

sw = StopWatch(use_utc=True, make_tz_aware=True)
print(sw.start_datetime)
sw.start()
print(f"{sw.start_datetime} | {sw.start_datetime.tzinfo}")
sw2 = StopWatch(make_tz_aware=True)
print(sw2.start_datetime)
sw2.start()
print(f"{sw2.start_datetime} | {sw2.start_datetime.tzinfo}")

Output:

None
2021-12-23 23:41:56.415212+00:00 | UTC
None
2021-12-23 20:41:56.415212-03:00 | E. South America Standard Time

Useful links

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

raccoon_simple_stopwatch-0.0.3.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

raccoon_simple_stopwatch-0.0.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file raccoon_simple_stopwatch-0.0.3.tar.gz.

File metadata

  • Download URL: raccoon_simple_stopwatch-0.0.3.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.1

File hashes

Hashes for raccoon_simple_stopwatch-0.0.3.tar.gz
Algorithm Hash digest
SHA256 5b7aa6d2a76e2b8b26d4005c3d1e839e922b6aaa8dee88a2dddcd7d37e69fdf9
MD5 3a223912afb25e8de7343ad2f3fb9504
BLAKE2b-256 b938508551514dd5b5489cb4d05df5ee1283ecc9c3b304bbe9077ce4fe9f6e5c

See more details on using hashes here.

File details

Details for the file raccoon_simple_stopwatch-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: raccoon_simple_stopwatch-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.1

File hashes

Hashes for raccoon_simple_stopwatch-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 401b021dc92dad4daf25623088f3011e17261f22a058af17ad4b1bced3063bef
MD5 73e8b394a346a865da8349d0cd70412c
BLAKE2b-256 8affbec83d4835ec4b1d5d3627f42ce3216f8ebe311cbde9bbd77a77e166eca2

See more details on using hashes here.

Supported by

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