Dialga: call function in the future
If you ever have the need to run a Python at a specific time, or periodically. This might come handy.
import asyncio
from datetime import datetime
from dialga import call_at, call_after
loop = asyncio.get_event_loop()
# This function will be call at 11:05 everyday as long that the loop is still running
@call_at(loop=loop, repeated=True, hour=11, minute=5)
def show_actual_runtime(name: str):
print(f"Actual time of {name}:", datetime.strftime(datetime.now(), "%y-%m-%d %H:%M:%S"))
# This function will be call after every 10 minutes as long as the loop is still running
@call_after(loop=loop, repeated=True, minutes=10)
def show_current_runtime(name: str):
print(f"Current time of {name}:", datetime.strftime(datetime.now(), "%y-%m-%d %H:%M:%S"))
show_actual_runtime(name="call_now")
show_current_runtime(name="call_now")
show_actual_runtime.promise(name="call_at")
show_current_runtime.promis(name="call_after")
loop.run_forever()
Another way to run an application:
from typing import *
from datetime import datetime
from dialga import Scheduler, call_at, call_after
scheduler = Scheduler()
now = datetime.now()
@scheduler.schedule(call_at, hour=now.hour, minute=now.minute + 5)
def five_minute_from_now():
print("Five minute from start")
print("Start time:", datetime.strftime(now, "%y-%m-%d %H:%M:%S"))
print("End time:", datetime.strftime(datetime.now(), "%y-%m-%d %H:%M:%S"))
@scheduler.schedule(call_after, minutes=1)
def one_minute_from_now():
print("One minute from start")
print("Start time:", datetime.strftime(now, "%y-%m-%d %H:%M:%S"))
print("End time:", datetime.strftime(datetime.now(), "%y-%m-%d %H:%M:%S"))
scheduler.run()
Or another way:
- Call at:
from datetime import datetime
from dialga import schedulable
@schedulable
def print_message(msg: str):
now = datetime.now()
print(now, msg)
return now
print(datetime.now())
task = print_message.at(hour=17, minute=32).call('17:32')
print_message('now')
print(task.get_result())
- Call after:
from datetime import datetime
from dialga import schedulable
@schedulable
def print_message(msg: str):
now = datetime.now()
print(now, msg)
return now
print(datetime.now())
task = print_message.after(seconds=1).call('1 second later')
print_message('now')
print(task.get_result())
The dialga module is built upon two main function.
call_after: call the function after some time
def call_after(
loop: asyncio.AbstractEventLoop = None,
repeated: bool = False, # Is the function called repeatly
**timedetail # Is the parameters of datetime.timedelta
):
...
call_at: call the function at specified time match the configuration
def call_at(
loop: asyncio.AbstractEventLoop = None,
repeated: bool = False, # Is the function called repeatly
**timedetail # Will be explained below
):
...
timedetail includes these parameters: year, month, week, weekday, day, hour, minute, second, all with same format.
- None: ignore
- int: specific value
- (start, end, [step]): value in range(start, end, [step])
- [int, ...]: value in list
Note:
- a
tuplein list is still considered as range and will be expanded. weekhas value from 1 -> 5weekdayhas value from 1 -> 7, monday -> sunday, mon -> sun, starts from mondayhourhas value from 0 -> 23minuteandsecondhav value from 0 -> 59- If no suitable time found, the function won't be called
Metadata
Release files for dialga 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dialga-0.0.1.tar.gz | 7.2 kB | Without provenance | Details for dialga-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dialga-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for dialga-0.0.1-py3-none-any.whl |
Total release size: 13.7 kB
Release files / dialga-0.0.1.tar.gz
| Download URL | dialga-0.0.1.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ae2b35f3492889a704ced17e826f7a79661ecc6d6293c903e8a1cbfdac8b5d67
|
|
BLAKE2b-256 checksum How to use checksums |
27e76caae297f70fba05ea889e96ce0398c6917445193b16466087fbceeb1622
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.13
|
Release files / dialga-0.0.1-py3-none-any.whl
| Download URL | dialga-0.0.1-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1397235faf87a81c90e948daf51874bda4c944251fbee46a3dfa37fb7daa74b0
|
|
BLAKE2b-256 checksum How to use checksums |
0b6df97f6af4ac1f68b6838f2018c4849c844e8c9eded167970353454ab5dfd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.13
|
Security for dialga 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.