Giter Club home page Giter Club logo

hailstone_module's Introduction

Hailstone module

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

Load current value ("state")

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)

Short-circuit if value has reached 1

if value == 1:
    raise Exception('OK: value is 1; exiting early')

Compute the next value

prev_value = value
if value % 2 == 0:
    value = value / 2
else:
    value = 3*value + 1
print('New value: %d' % value)

Write the new value to the "state" path

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)

Customize each run's commit message

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))

hailstone_module's People

Contributors

ryan-williams avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.