Skip to main content

Pychrono is a Python package for managing delays, scheduling tasks, and timing functions. It supports both synchronous and async operations, making time management simple and efficient.

Project description

Pychrono

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.

Features

  • Delay execution for a specific amount of time.
  • Get the current system time and format it.
  • Run tasks on a delay asynchronously.
  • Repeat functions multiple times.
  • Measure function execution time.
  • Use a robust Timer class with start, pause, resume, and elapsed time tracking.
  • Recurring task scheduling.
  • Countdown timers.

Changlogs

v0.1.2

  • Added a @recurring decorator : this decorator allows you to always execute a function every interval seconds.
  • Added a .countdown(seconds, callback) method : this method will execute the function callback in seconds seconds.

v0.1.1

  • .elapsed and Timer (__str__) now output a non-rounded string without "seconds" that led to 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_timer(5, times_up)

2. Decorators

Repeat Function Execution

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

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

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()

Always execute a Function on an interval

@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)

Asynchronous Scheduling with Delay

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

say_hello()  # Will print "Hello" after 2 seconds without blocking

Run a Function Asynchronously

@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(2)

# 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(1)
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-0.1.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

pychrono-0.1.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pychrono-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f367ac7930426768e0afaaf78ed4f8495a4ba797e79aa733f873431978f440c3
MD5 5ffeef1d41e1a7c994eeee140c85af10
BLAKE2b-256 8b3e603684d3352210eab669ee8abfc8c535bd68bfba80b83ae44fc3f24ec0a0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pychrono-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1d2a9fc86db4cb7f44b40fbf04eec08885ac386646fff4ef34d1512e0a8bd895
MD5 0933f549aef267e9978b2bdef120ec75
BLAKE2b-256 4b82c313c5fb5f7f40ee06088cd5f78c5c965578d96b56983ab9ccc7a76500ef

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