Skip to main content

A package for managing delays, scheduling tasks, timing functions, caching results, and enhancing time-based operations with robust decorators for asynchronous execution, throttling, retries, and more.

Project description

Pychrono

Downloads PyPI version Build Status License: MIT

Pychrono is a Python package designed for managing delays, scheduling tasks, timing functions, and more. It provides decorators for repeating tasks, scheduling actions, and running tasks asynchronously using threading. Pychrono simplifies time-related operations for both synchronous and asynchronous contexts. View on PyPi

Features

  • Delay execution for a specific amount of time.
  • Get and format the current system time.
  • Run tasks on a delay asynchronously.
  • Repeat functions multiple times.
  • Measure function execution time.
  • Recurring task scheduling.
  • Countdown timers.
  • Cache function results with expiration.
  • Retry functions if they fail.
  • Limit function execution rate.
  • Impose an execution timeout.
  • Validate function arguments.
  • Throttle function calls to avoid frequent executions.

Changelog

For the next update, we are aiming to rework and enhance our existing functions and add new ones.

v1.0.0: Public Release

This update focuses on enhancing and expanding the decorators.

Added Decorators:

  • @cache: A decorator that caches the results of a function.
  • @throttle: A decorator that limits how often a function is executed.
  • @retry: A decorator that retries a function if it fails.
  • @timeout: A decorator that imposes an execution time limit on a function.
  • @validate: A decorator to validate the types of a function's arguments.
  • @timed_cache: A decorator that caches results with an expiration period.

Fixed Decorators:

  • @schedule: Improved for consistent execution timing.

Documentation:

  • Updated the docstrings and examples of all decorators.

v0.1.2

  • Added a @recurring decorator to always execute a function at specified intervals.
  • Added a .countdown(seconds, callback) method to execute a function after a countdown.

v0.1.1

  • .elapsed and Timer (__str__) now output a non-rounded string without "seconds" to avoid type casting issues.

Installation

pip install pychrono

Usage

1. Delays and Time Functions

Delay Execution

import pychrono

# Delay execution for 1000 milliseconds (1 second)
pychrono.delay(1000)

Get Current Time

# Get the current time in seconds since the epoch
current_time = pychrono.current()
print(f"Current time: {current_time}")

Convert Time to Local String

# Convert time to a readable local time string
seconds = pychrono.current()
formatted_time = pychrono.local(seconds)
print(f"Local time: {formatted_time}")

Start a Countdown on a Function

def times_up():
    print("Time's up!")

# Start a countdown from 5 seconds
pychrono.countdown(5, times_up)

2. Decorators

Repeat Function Execution

@pychrono.repeat(3)
def greet():
    print("Hello!")

greet()  # This will print "Hello!" three times

Cache Function Results

@pychrono.cache
def heavy_computation(x):
    print(f"Computing for {x}")
    return x * x

print(heavy_computation(2))  # Outputs: 4 and caches the result
print(heavy_computation(2))  # Uses cached result

Time a Function's Execution

@pychrono.timer
def long_task():
    for _ in range(1000000):
        pass

# Print the time taken to run the function
long_task()

Limit Function Execution Rate (@throttle)

@pychrono.throttle(2)  # Allow execution only every 2 seconds
def greet_throttled():
    print("Throttled Hello!")

greet_throttled()  # Prints immediately
greet_throttled()  # Throttled, won't print if called within 2 seconds

Retry Function Execution (@retry)

@pychrono.retry(max_attempts=3, wait=2)
def unstable_task():
    import random
    if random.random() < 0.7:
        raise ValueError("Random failure!")
    print("Success!")

unstable_task()  # Retries up to 3 times with a 2-second wait between attempts

Validate Function Arguments (@validate)

@pychrono.validate(int, float)
def add(a, b):
    return a + b

print(add(3, 4.5))  # Valid input, prints: 7.5
# print(add(3, 'four'))  # Raises TypeError

Cache Results with Expiration (@timed_cache)

@pychrono.timed_cache(5)  # Cache results for 5 seconds
def expensive_function(x):
    print(f"Expensive calculation for {x}")
    return x * 2

print(expensive_function(3))  # Performs calculation
print(expensive_function(3))  # Uses cached result if called within 5 seconds

Execute a Function Repeatedly (@recurring)

@pychrono.recurring(2)  # Run every 2 seconds
def print_message():
    print("This message will print every 2 seconds.")

# Start the recurring task
print_message()

# Prevent the main thread from exiting immediately
while True:
    time.sleep(1)

Schedule a Task with Delay (@schedule)

@pychrono.schedule(2000)  # Delay for 2000 milliseconds (2 seconds)
def say_hello():
    print("Hello after 2 seconds!")

say_hello()  # Prints "Hello" after 2 seconds without blocking

Run a Function Asynchronously (@asynchronous)

@pychrono.asynchronous
def task():
    print("Running asynchronously!")

task()  # Runs in a separate thread

3. Timer Class

The Timer class allows you to start, pause, resume, and get the elapsed time. Printing the timer object directly will output the seconds elapsed.

Start, Pause, and Resume Timer

# Create a timer instance
timer = pychrono.Timer()

# Start the timer
timer.start()

# Perform some task
pychrono.delay(2000)  # Delay for 2 seconds

# Get the elapsed time
print(f"Elapsed: {timer}")  # Prints elapsed time in seconds (e.g., 2.0)

# Pause the timer
timer.pause()

# Resume the timer
timer.resume()

# Get updated elapsed time
pychrono.delay(1000)  # Delay for 1 more second
print(f"Updated Elapsed: {timer}")  # Prints updated elapsed time (e.g., 3.0)

More Features Coming Soon!

Stay tuned for more functionalities such as:

  • Enhanced threading control and task management.
  • Time zone support for easier global time handling.
  • And much more!

Feel free to contribute to the project, raise issues, or suggest features by visiting our GitHub repository.

License

Pychrono is licensed under the MIT License.


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

pychrono-1.0.5.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

pychrono-1.0.5-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file pychrono-1.0.5.tar.gz.

File metadata

  • Download URL: pychrono-1.0.5.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for pychrono-1.0.5.tar.gz
Algorithm Hash digest
SHA256 234ef1354c8206b740ba60c01e697b28b418ccf33504135eab09339462a16e11
MD5 357cc552b5cc6466e38ada21f949c041
BLAKE2b-256 93aa998c2ef6ae59709ac28a24cabe58512b7c6ea15c897862d3c268ddeb2269

See more details on using hashes here.

File details

Details for the file pychrono-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: pychrono-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for pychrono-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 eb42ccfbee690863c099f8ef065b72b0cdafeccf1f7ffab1f6e9dc6d716720bc
MD5 6f9c98daed0bb2701e9d3f87a62bb891
BLAKE2b-256 70256d4025f138d3955f55d3865aea9a6161eb4473678c3fd5103c4009c95df8

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