Notebook-based gsmo module that computes the Hailstone sequence:
This module also demonstrates several features of gsmo modules:
- state-propagation across runs
- early-stopping
- custom commit-messages
The file value
will hold an integer; initialize it to 6
if it doesn't exist:
INITIAL_STATE = 6
from pathlib import Path
value_path = Path('value')
if value_path.exists():
with value_path.open('r') as f:
value = int(f.read().strip())
print('Loaded previous value: %d' % value)
else:
value = INITIAL_STATE
print('Set default value: %d' % value)
if value == 1:
raise Exception('OK: value is 1; exiting early')
prev_value = value
if value % 2 == 0:
value = value / 2
else:
value = 3*value + 1
print('New value: %d' % value)
The _STATE
file in this module is read by the gsmo runner, and contains contents:
value
This tells the runner machinery that any changes to the path value
should be included in the Git commit representing a run (which is committed in a clone of the module that lives under runs/
in the module directory).
Additionally, the runner will merge changes to the value
file upstream into the outer repo (which subsequent runs are cloned from; note this is not true of other files generated by runs and committed in the runs/
clone, like the logs/{out,err}
and SUCCESS
/FAILURE
files).
with value_path.open('w') as f:
f.write('%d\n' % value)
Finally, by writing to the _MSG
, we can tell gsmo what the commit message for this run should be:
commit_msg_path = Path('_MSG')
with commit_msg_path.open('w') as f:
f.write('%d โ %d\n' % (prev_value, value))