Skip to content

Commit

Permalink
ref: update make_checkpoint example
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeorpinel committed Feb 17, 2021
1 parent 22af4cf commit 597161c
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions content/docs/api-reference/make_checkpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@ by the code in stages executes by `dvc exp run` (see `cmd` field of `dvc.yaml`).
> Note that for non-Python code, the alternative is to write a
> `.dvc/tmp/DVC_CHECKPOINT` signal file.
## Exceptions

None

## Example: Mark a checkpoint every 100 iterations
## Example: Every 100th iteration

Let's consider the following `dvc.yaml` file:

```yaml
stages:
foo:
cmd: python src/mystage.py
cmd: python iterate.py
params:
- start
outs:
Expand All @@ -49,35 +45,31 @@ stages:
> See `dvc params` for information on stage parameters.

Here's the example code that the stage above will execute. It continuously
increments an integer value in the `int.txt` file, starting at the `start`
param. It makes a checkpoint with `dvc exp` every time the value adds 100:
increments an integer value in `int.txt`, starting at the `start` param. It
makes a checkpoint with `dvc exp` every time the value adds 100:

```py
import os
from ruamel.yaml import YAML
from dvc.api import make_checkpoint
output_file = "int.txt"
with open("params.yaml") as fobj:
params = yaml.load(fobj)
start = params.get("start", 0)
start = _load_param('start') # Load start from params.yaml
while True:
try:
if os.path.exists(output_file):
with open(output_file, "r") as fobj:
with open(output_file, "r") as fd:
try:
data = fobj.read()
iter_ = int(data) + 1
iter_ = int(fd.read()) + 1
except ValueError:
iter_ = start
else:
iter_ = start
with open(output_file, "w") as fobj:
fobj.write(f"{iter_}")
with open(output_file, "w") as fd:
fd.write(f"{iter_}")
if iter_ % 100 == 0:
make_checkpoint()
# ...
```

0 comments on commit 597161c

Please sign in to comment.