Miksus / rocketry

Modern scheduling library for Python
https://rocketry.readthedocs.io
MIT License
3.27k stars 108 forks source link

Expose conditions #44

Closed Miksus closed 2 years ago

Miksus commented 2 years ago

This is quite a massive update. Nothing already documented will change but quite a lot under the hood.

Most notable changes:

Introduction of easy to use conditions

These are convenient alternatives for those who prefer Python objects instead of the string syntax. These are exact equivalents:

from rocketry.conds import daily, time_of_week

@app.task(daily)
def do_first():
    ...

@app.task(daily.between("12:00", "15:00") & time_of_week.on("Monday"))
def do_second():
    ...

and

from rocketry.conds import daily, time_of_week

@app.task("daily")
def do_first():
    ...

@app.task("daily between 12:00 and 15:00 & time of week on Monday")
def do_second():
    ...

Pick which style suits you.

Parametrizable conditions

Previously the conditions used sessions and tasks in quite an ugly way. They relied on that they are mutable and

Now instead of using bool(condition), this should be used instead: condition.observe(session=session). The context is given when evaluating instead of embedding the context inside.

In addition, this allows easier custom conditions and conditions support for passing the context out of the box similarly as parametrization in tasks work. Actually, they now share the mechanism.

Easier reference

Now these are also possible:

from rocketry.conds import daily, time_of_week

@app.cond()
def is_foo():
    ...

@app.task(daily & is_foo)
def do_things():
    ...

app.cond() now returns the condition itself. There could be problems with pickling but conditions are not passed to processes.

Also, reference in Return is also simpler:

from rocketry.args import Return

@app.task()
def do_first():
    ...

@app.task()
def do_second(arg=Return(do_first)):
    ...

Note how we did not pass the task name as a string but just the function. The task name is stored in the function as do_first.__rocketry__['name'].

codecov-commenter commented 2 years ago

Codecov Report

Merging #44 (b61468a) into master (766fd57) will decrease coverage by 0.25%. The diff coverage is 98.23%.

@@            Coverage Diff             @@
##           master      #44      +/-   ##
==========================================
- Coverage   90.08%   89.83%   -0.26%     
==========================================
  Files          81       83       +2     
  Lines        3652     3747      +95     
==========================================
+ Hits         3290     3366      +76     
- Misses        362      381      +19     
Impacted Files Coverage Δ
rocketry/core/condition/__init__.py 100.00% <ø> (ø)
rocketry/conditions/task/task.py 92.12% <94.82%> (-3.45%) :arrow_down:
rocketry/conditions/task/utils.py 90.47% <96.42%> (+1.28%) :arrow_up:
rocketry/conditions/api.py 97.22% <97.22%> (ø)
rocketry/core/condition/base.py 92.26% <98.73%> (+1.94%) :arrow_up:
rocketry/_setup.py 100.00% <100.00%> (ø)
rocketry/args/builtin.py 94.91% <100.00%> (+0.27%) :arrow_up:
rocketry/conditions/__init__.py 100.00% <100.00%> (ø)
rocketry/conditions/func.py 87.80% <100.00%> (+1.69%) :arrow_up:
rocketry/conditions/meta.py 95.74% <100.00%> (+0.50%) :arrow_up:
... and 20 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 766fd57...b61468a. Read the comment docs.

Miksus commented 2 years ago

If session.task_exists is tested the code coverage goal is probably reached. The cov decreased due to removal of unneeded code (which was tested).