Skip to main content

A framework to keep track of dependencies in non-linear workflows

Project description

A framework to keep track of dependencies in nonlinear workflows. Source code

About

  • Create event-driven apps in Python with ease, without writing callback functions

  • Set up the functional relationship between your public (visible to user) as well as internal (hidden) variables, and let autocalc take care of keeping them in sync.

Example

In this example we will implement a quadratic equation solver. User can enter the a, b and c parameters of the

a*x*x + b*x +c = 0

quadratic equation and we will calculate the two solutions: x1 and x2 by the following formulas:

x1 = (-b-D)/(2a)

x2 = (-b+D)/(2a)

where D is defined as

D=sqrt(b*b - 4*a*c)

In this example we assume a Jupyter notebook environment and the use of ipywidgets. The library was designed with this setup in mind, but the core functionality is independent of any interactive environment.

First we declare and display our input variables

import ipywidgets as widgets
from autocalc.autocalc import Var
import math

a = Var('a', initial_value = 1, widget = widgets.FloatText())
b = Var('b', initial_value = -3, widget = widgets.FloatText())
c = Var('c', initial_value = 1, widget = widgets.FloatText())

display(a); display(b); display(c)

Then we implement the code which calculates the solution

def Dfun(a, b, c):
    try:
        return math.sqrt(b*b-4*a*c)
    except ValueError:
        return math.nan

def x1fun(a,b,D):
    return (-b-D)/2/a
def x2fun(a,b,D):
    return (-b+D)/2/a

We are now ready to define and display our internal variable: D=sqrt(b*b - 4*a*c) and output variables: x1 and x2

D = Var('D', fun=Dfun, inputs=[a, b, c])
x1 = Var('X1', fun=x1fun, inputs=[a, b, D], widget =     widgets.FloatText(), read_only=True)
x2 = Var('X2', fun=x2fun, inputs=[a, b, D], widget = widgets.FloatText(), read_only=True)
display(x1)
display(x2)

That’s it. With just a few lines we set up the dependency graph from our input variables to our output ones. Any time the user upgrades any of the input variables, the output will be updated automatically.

Features

  • Ipywidgets integration + visual framework independent mode of operation.

  • Read-only variables

  • Lightweight, minimal library. Learn the basics in 10 minutes.

  • “Lazy” variables, which only get invalidated when their input(s) change, but are not actually recalculated until explicitly requested. Useful for functions which take a long time to evaluate.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autocalc-0.2.1.tar.gz (24.2 kB view hashes)

Uploaded Source

Built Distribution

autocalc-0.2.1-py3-none-any.whl (8.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page