Skip to content

Instantly share code, notes, and snippets.

@lmmx
Created July 16, 2024 10:47
Show Gist options
  • Save lmmx/6c08d0dac4721075ed681b56c428cdf3 to your computer and use it in GitHub Desktop.
Save lmmx/6c08d0dac4721075ed681b56c428cdf3 to your computer and use it in GitHub Desktop.
Example of running an idempotent pipeline using Pydantic validation for control flow (using `OnErrorOmit`)
from pydantic import BaseModel, FilePath, NewPath, OnErrorOmit
class Step(BaseModel):
source: FilePath
dest: NewPath
class Pipeline(BaseModel):
steps: list[OnErrorOmit[Step]]
def run_pipeline():
steps = [dict(source=f"{letter}.in", dest=f"{letter}.out") for letter in "ab"]
for step in steps:
print(f"Prepared step: {step}")
pipeline = Pipeline(steps=steps)
for step in pipeline.steps:
print(f"Running {step=}")
@lmmx
Copy link
Author

lmmx commented Jul 16, 2024

louis 🌟 ~/lab/pydantic/idem $ ls
run.py

louis 🌟 ~/lab/pydantic/idem $ qp -c "import run; run.run_pipeline()"
Prepared step: {'source': 'a.in', 'dest': 'a.out'}
Prepared step: {'source': 'b.in', 'dest': 'b.out'}

louis 🌟 ~/lab/pydantic/idem $ touch a.in

louis 🌟 ~/lab/pydantic/idem $ qp -c "import run; run.run_pipeline()"
Prepared step: {'source': 'a.in', 'dest': 'a.out'}
Prepared step: {'source': 'b.in', 'dest': 'b.out'}
Running step=Step(source=PosixPath('a.in'), dest=PosixPath('a.out'))

louis 🌟 ~/lab/pydantic/idem $ touch b.in

louis 🌟 ~/lab/pydantic/idem $ qp -c "import run; run.run_pipeline()"
Prepared step: {'source': 'a.in', 'dest': 'a.out'}
Prepared step: {'source': 'b.in', 'dest': 'b.out'}
Running step=Step(source=PosixPath('a.in'), dest=PosixPath('a.out'))
Running step=Step(source=PosixPath('b.in'), dest=PosixPath('b.out'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment