Project Overview

Project Purpose and Objectives

This project delivers a lightweight, precise timing and scheduling utility designed primarily for developers and testers who need to measure elapsed time and compute next scheduled run times based on cron-like expressions. Its core objectives are:

Major Functionalities:

Example Workflows and Use Cases

Measuring Elapsed Time of a Code Block

  1. Create a TaskTimer instance.

  2. Call .start() before the code block.

  3. Execute the code to be timed.

  4. Call .stop() after execution.

  5. Access .elapsed_ms to retrieve elapsed time in milliseconds.

from tasktimer.timer import TaskTimer

timer = TaskTimer()
timer.start()
# ... code to measure ...
timer.stop()
print(f"Elapsed time: {timer.elapsed_ms} ms")

Calculating Next Scheduled Run Time

Given a cron expression such as "0 9 * * 1-5" (9 AM on weekdays), compute the next occurrence after the current moment:

from tasktimer.schedule import next_run

cron_expr = "0 9 * * 1-5"
next_run_time = next_run(cron_expr)
print(f"Next run at: {next_run_time}")

The next_run function parses the expression, expands the fields, and iterates forward up to one year from now to find the next matching datetime.

Stack and Technologies

These choices prioritize minimal dependencies, ease of understanding, and portability, making the project suitable as a utility in different Python environments.

High-Level Architecture

The project consists of three main logical components:

The package initialization (__init__.py) exposes the timer API cleanly.

Interactions

graph TB
Demo --> Timer[Timer Module]
Demo --> Schedule[Schedule Module]
Test --> Timer
Timer --> TimeLib[time Module]
Schedule --> DateTimeLib[datetime Module]
__init__ --> Timer

Developer Navigation

Visual Diagrams

Component Diagram: High-Level Architecture

graph TB
Demo --> Timer
Demo --> Schedule
Test --> Timer
Timer --> Time
Schedule --> DateTime
__init__ --> Timer

Flowchart: Timer Usage Workflow

flowchart TD
Start["Create TaskTimer instance"]
Start --> S1["Call start()"]
S1 --> Work["Execute code to time"]
Work --> S2["Call stop()"]
S2 --> Result["Retrieve elapsed_ms"]
Result --> End["Use elapsed time"]

Flowchart: Schedule Next Run Calculation

flowchart TD
Input["Input cron expression"]
Input --> Parse["Parse expression into fields"]
Parse --> Expand["Expand fields to integer lists"]
Expand --> Search["Iterate dates from 'now'"]
Search --> Match{"Date matches schedule?"}
Match -- Yes --> Output["Return next run datetime"]
Match -- No --> Search
Search -->|1 year limit exceeded| Error["Raise error: no match found"]