Skip to main content

A simple timer class for tracking elapsed time across multiple sessions, with detailed statistics and formatted output.

Project description

stopstart

A simple and intuitive Python class for tracking elapsed time across multiple sessions. The Timer class offers a clean, human-readable interface to start and stop time, while providing detailed session statistics and beautifully formatted elapsed time. Whether you're measuring short intervals or long-running processes, the Timer class ensures you have accurate data with minimal effort.

Features

  • Easy to Use: No complex setup—just call start() to begin and stop() to end your time tracking. Perfect for quick and efficient use.
  • Time Formatting: Effortlessly convert elapsed time into a human-friendly format, including days, hours, minutes, seconds, and even fractions like microseconds and nanoseconds.
  • Session Statistics: Track total time, the number of sessions, and average session durations with just one method call.
  • Formatted Output: Receive time statistics in an easy-to-read format that includes precise microseconds and nanoseconds.

Methods:

  • start(reset=True): Starts the timer. If reset=True, it resets the timer and clears any past session history. If reset=False, the timer continues from where it left off.
  • stop(): Stops the timer, records the elapsed time for the current session, and appends it to the history.
  • print_snapshot(prefix=""): Prints a snapshot of the total elapsed time up to the current moment, appending a given prefix (optional).
  • print(prefix=""): Prints the formatted total elapsed time with an optional prefix.
  • get_stats(): Returns a dictionary with statistics about the timer, including total time, number of sessions, and average session time.

Properties:

  • days: The total elapsed time in days.
  • hours: The total elapsed time in hours.
  • minutes: The total elapsed time in minutes.
  • seconds: The total elapsed time in seconds.
  • milli: The total elapsed time in milliseconds (total duration in seconds * 1e3).
  • nano: The total elapsed time in nanoseconds (total duration in seconds * 1e9).
  • total_duration: The total accumulated duration of all actions in seconds, calculated by summing the durations of all recorded sessions.

Installation

Install the package via pip:

pip install timer

Usage

Example: Typical Usecase

In this example, the timer is used to measure a single session. Starting and stopping the timer around an activity (like a 2-second wait) will give you the total duration for that period, along with a detailed human-readable output and session statistics.

import time
from stopstart import Timer

# Initialize the Timer
timer = Timer()

# Start the timer (reset to 0)
timer.start()

# Wait for 2 seconds
time.sleep(2)

# Stop the timer
timer.stop()

# Get total duration in seconds
print(f"Total duration in seconds: {timer.seconds}")

# Get a nice clean formatted total duration
print(timer)

# Get stats
stats = timer.get_stats()
print(f"Stats: {stats}")

Output:

Total duration in seconds: 2.000082492828369  
2 seconds, 82 microseconds, and 492 nanoseconds  
Stats: {'total_time': 2.000082492828369, 'num_sessions': 1, 'avg_time': 2.000082492828369}

Example: Session Resuming

The Timer class can resume timing without resetting the session history, allowing you to track multiple periods of time over the course of your work. This makes it easy to start new sessions without losing track of the total time across sessions.

In this example, we start a second session without resetting the timer, simulate another 2-second activity, and then stop the timer to get the updated total duration.

# Start the second session (without reset)
timer.start(reset=False)

# Wait for another 2 seconds
time.sleep(2)

# Stop the timer
timer.stop()

# Get updated total duration
print(f"Total duration in seconds: {timer.seconds}")
print(timer)

Output:

Total duration in seconds: 4.0001797676086426
4 seconds, 179 microseconds, and 767 nanoseconds
Stats: {'total_time': 4.0001797676086426, 'num_sessions': 2, 'avg_time': 2.0000898838043213}

Print snapshots

You can also print time snapshots without affecting the ongoing time tracking. This is useful for logging intermediate results or showing progress without interrupting the current session. The snapshot feature is non-intrusive and does not reset or stop the timer.

time.sleep(30)
timer.print_snapshot(prefix='Snapshot Example:')
timer.print('Actual recorded time:')

Output:

Snapshot Example: 34 seconds, 132 microseconds, and 165 nanoseconds
Actual recorded time: 4 seconds, 179 microseconds, and 767 nanoseconds

Example: Accessing Timer History

The Timer automatically preserves the history of previous sessions in the actions attribute. This allows you to access a complete log of all sessions, including their start and stop times, without losing any context. You can easily retrieve this history to analyze past sessions or track progress over time.

import time
from stopstart import Timer

# Initialize the Timer object
timer = Timer()

# Start the first session
timer.start()
time.sleep(2)
timer.stop()
print(f"Session 1 Duration: {timer}")

# Start a second session without resetting the timer
timer.start(reset=False)
time.sleep(3)
timer.stop()
print(f"Session 2 Duration: {timer}")

# Accessing the history of all actions
print(f"Actions History: {timer.actions}")

Output:

Session 1 Duration: 2 seconds, 0 microseconds, and 0 nanoseconds  
Session 2 Duration: 5 seconds, 0 microseconds, and 0 nanoseconds  
Actions History: [
    {'start': 1699392400.0, 'stop': 1699392402.0, 'duration': 2.0}, 
    {'start': 1699392402.0, 'stop': 1699392407.0, 'duration': 5.0}
]

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

stopstart-0.1.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

stopstart-0.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stopstart-0.1.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for stopstart-0.1.0.tar.gz
Algorithm Hash digest
SHA256 237fcd67d80dc743cc826616a9986c25e44b1e1f7ccd03da68bef7a57d2cd692
MD5 c4de23e5daff63fab9605ce716a880b3
BLAKE2b-256 31df080dce08352b812eaf544f8815d60640664b23a8043ccd1a14e9da8f4e1f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stopstart-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for stopstart-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54349f56acd7820756a1259e9a8c6cb547742edc279b2d9173f30c8fa6e312f1
MD5 a5f15d66c6e8a03938b51965739563d1
BLAKE2b-256 24af0748f5b388bb2a7aa608290ce7b04ed5e9389d8f6ed703b4d4d41573eae1

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