When you have to do something... later.
Project description
DoItLater
A simple python library to schedule work in the future with ability to loop infinitely. Does not depend on any 3rd party libraries.
Installation
The package can be simply installed using on of these methods:
pip
:pip install doitlater
poetry
:poetry add doitlater
poetry
:poetry add git+https://github.com/evalkaz/doitlater.git
Usage
Simplest example:
from datetime import datetime, timedelta
from doitlater import Later
later = Later()
@later.on(datetime(2021, 1, 1))
def say_hello():
print("Happy new years in 2021!")
if __name__ == "__main__":
later.do()
To call the same function every 10 seconds with 30 seconds cold start:
@later.on(
datetime.now() + timedelta(seconds=30),
timedelta(seconds=10),
loop=True
)
def repeatable_work():
print("Hello every 10 seconds!")
The later.on()
is stackable so this will work too (will be executed every 5 and every 7 days):
@later.on(datetime(2021, 1, 1), timedelta(days=5))
@later.on(datetime(2021, 1, 1), timedelta(weeks=1))
def say_hello():
print("Happy new years in 2021!")
You can also pass a list of datetime
or timedelta
(or a mixed one) when to execute the function:
@later.on(datetime(2021, 1, 1), [
datetime(2021, 2, 1),
datetime(2021, 3, 1),
timedelta(days=31)], loop=False)
def say_hello():
print("Happy new years in 2021!")
If you want to stop executing the same function just return False
:
@later.on(datetime(2021, 1, 1), timedelta(seconds=5), loop=True)
def only_one_hello():
print("This will appear only once!")
return False
If you have multiple functions and one of them throws an exception but you don't want to stop the work, pass ignore_errors=True
to Later()
object:
later = Later(ignore_errors=True)
By default, library will output logs on errors. To change logging, use dictConfig
:
from datetime import datetime, timedelta
from doitlater import Later
from logging.config import dictConfig
dictConfig(
{
"version": 1,
"formatters": {
"default": {
"format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
}
},
"handlers": {
"default": {"class": "logging.StreamHandler", "formatter": "default",}
},
"root": {"level": "INFO", "handlers": ["default"]},
}
)
later = Later()
@later.on(datetime(2021, 1, 1))
def say_hello():
print("Happy new years in 2021!")
if __name__ == "__main__":
later.do()
API
Later(workers, ignore_errors)
takes these parameters:
workers
- number of threads to use, defaults to maximum number of threads supported by CPU.ignore_errors
-False
will exit when one of the function throws an error,True
- ignores exceptions and will resume the work.
later.on(exactly, repeat, loop)
function takes these parameters:
exactly
- on which time perform the first function call.repeat
- a single value or a list ofdatetime
,timedelta
or both on when to repeat the call. IfNone
is passed the function will not be called again. Defaults toNone
.loop
- whether repeat calling times fromrepeat
. Defaults toTrue
.
later.do()
does not take any parameters.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file DoItLater-0.1.2.tar.gz
.
File metadata
- Download URL: DoItLater-0.1.2.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.6.9 Linux/4.4.0-148-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92d4bb4c9c397d9baceeb441f676ca60bbf704a15f3130fa27d38d17ce5fec02 |
|
MD5 | 15415184f842e54707dcae6b125436ed |
|
BLAKE2b-256 | 0d825415e9adeee36ea3be33158c0004a2e67e26eedf78f3de9b5a24c41df802 |
File details
Details for the file DoItLater-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: DoItLater-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.6.9 Linux/4.4.0-148-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fbdf144a26164c3e3583e26916075acb46283a89382ba747c5b4844f7d522e1 |
|
MD5 | f60fc20b6eeade6430fa20b2715ea317 |
|
BLAKE2b-256 | 8ad43e89222c17369c188638dbf2b941d951dbafb9184995fcde7dc69ceb07cc |