Skip to main content

Lightweight Python library for scheduling functions at specified times.

Project description

TickTask

TickTask is a lightweight and intuitive Python library designed to execute functions at specified times. Whether you're looking to automate tasks, schedule reminders, or perform time-based operations, TickTask provides a simple interface to schedule function calls with ease.

Table of Contents

Installation

To install TickTask, make sure you have Python version 3.12 or higher installed on your system.

Unix/macOS

pip3 install ticktask

Windows

pip install ticktask

Usage

Scheduler Class

The Scheduler class is a core component of the TickTask library, responsible for managing and executing scheduled tasks. It can operate with or without a database, allowing for flexible task management.

Parameters

  • database ISchedulerDatabase, optional: An instance of a database object that allows the scheduler to store and retrieve tasks.

Methods

Starts the scheduler. If a database is provided, it will connect to the database and retrieve all existing tasks:

start(self) -> None

Stops the scheduler. If a database is provided, it will disconnect from the database:

stop(self) -> None

Adds a new task to the scheduler. If a task with the same name already exists, a ValueError will be raised:

add_task(self, task: Task) -> None
# task (Task): The task to be added to the scheduler.

Removes a task from the scheduler by its name. If the task does not exist, a ValueError will be raised:

remove_task(self, task_name: str) -> None
# task_name (str): The name of the task to be removed.

Example Usage

Here's an example of how to create a scheduler, add tasks, and start it:

from ticktask import Scheduler, Task, TaskType

# Create a scheduler
scheduler = Scheduler()

# Define a task
def my_task():
    print("Task executed!")

task = Task(
    name="My Task",
    task=my_task
)

# Add the task to the scheduler
scheduler.add_task(task)

# Start the scheduler
scheduler.start()

# Stop the scheduler when done
scheduler.stop()

Task Class

The Task class represents a scheduled task. It encapsulates the task's details, including its name, the function to execute, and any arguments required for execution.

Parameters

  • name str: The name of the task.
  • task typing.Callable: The function to be executed as the task.
  • task_type TaskType, optional: The type of the task, either ONE_TIME or REPEAT. Defaults to ONE_TIME.
  • task_args typing.Dict[str, typing.Any], optional: A dictionary of arguments to pass to the task function. Defaults to an empty dictionary.
  • start_date datetime.datetime, optional: The date and time when the task should start. Defaults to the current date and time.
  • time_interval datetime.timedelta, optional: The interval at which the task should repeat (if applicable).
  • callback typing.Callable, optional: A callback function to be executed after the task completes.
  • callback_args typing.Dict[str, typing.Any], optional: A dictionary of arguments to pass to the callback function.

Methods

exec()

Executes the task immediately, ignoring any scheduled time. If a callback is provided, it will be executed after the task completes, and the output of the task can be passed to the callback if specified.

Example Usage

Here’s an example of how to create and execute a task using the Task class:

import datetime
from ticktask import Task, TaskType

def my_function(arg1, arg2):
    print(f"Task executed with arguments: {arg1}, {arg2}")
    return arg1 + arg2

def my_callback(output):
    print(f"Callback executed with output: {output}")

# Create a task
task = Task(
    name="My First Task",
    task=my_function,
    task_type=TaskType.ONE_TIME,
    task_args={"arg1": 5, "arg2": 10},
    start_date=datetime.datetime.now(),
    callback=my_callback,
    callback_args={"output": None}
)

# Execute the task
task.exec()

Callable Serialization

This module provides functionality to serialize and deserialize callable objects in Python. The serialization process converts a callable into a dictionary format that can be stored and later reconstructed, while deserialization restores the callable from its dictionary representation.

Warning: These functions are in the experimental phase. They may not handle all edge cases and should be used with caution in production environments.

Functions

serialize_callable(func: typing.Callable) -> dict
# func (typing.Callable): The function or callable object to serialize.
# dict: A dictionary containing the serialized representation of the callable.

Serializes a callable object into a dictionary format. This allows you to store essential information required to recreate the function later. Note that the body of the function is not saved, only metadata.

deserialize_callable(data: dict) -> typing.Callable
# data (dict): The dictionary containing the serialized callable information.
# typing.Callable: The deserialized callable function.

Deserializes a dictionary back into a callable function. The function must exist in the code and be located in the same module and path as when it was serialized.

Usage Notes

Ensure that the functions you serialize are available in the same module and path when deserializing. These functions do not serialize the function's body or state, only its location and name. Use with caution in production environments due to their experimental nature.

ChangeLog

[0.2.0] - 2025-06-30

Add

  • Adding in serialize_callable and deserialize_callable the ability to process methods from objects. Serialization saves attributes and their values. Object initialization should include initialization of all object attributes

[0.1.2] - 2025-06-18

Fix

  • In __start_task method remove calls target.

[0.1.1] - 2025-06-17

Fix

  • Use remove_task method to delete ONE_TIME task after executed.

[0.1.0] - 2025-06-16

Initial Release

  • Introduced basic library features.

TODO

  • Create a more intuitive method of passing shuffle output to a callback.
  • Add Status Codes. Adding more exceptions for more control.
  • To create a method of storing functions together with the body, maintaining security.

License

This project is licensed under the terms of the MIT license. For more details, please refer to the LICENSE file.

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

ticktask-0.2.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

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

ticktask-0.2.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file ticktask-0.2.0.tar.gz.

File metadata

  • Download URL: ticktask-0.2.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ticktask-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e14b88d15242602ebefaeaf30d137b83d5ea9571cb90a174ec2a6ae5b27ec062
MD5 5e771d0e4f08370625b7bd36354c475f
BLAKE2b-256 76a83b79205177df3fd42f66880b024d7e918e5d8a5a53c25e617cdc3301c87b

See more details on using hashes here.

File details

Details for the file ticktask-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ticktask-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ticktask-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6bec8d851360365e9545acbf4a7d7d51586936c961344728da0ad95bbf0437ae
MD5 0b5caee9c4631ffa015fa629fdc10db2
BLAKE2b-256 b91d6a778efafb0aa34f1316c364494f36417bdf90c643e12f91360681a73a7b

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