Depcode
From MairasNetWiki
Contents |
Introduction
Depcode is a conceptual test of dependency-based programming. Instead of defining the program actions in a sequential order, you define blocks and their relative dependencies. Makefiles work in the same manner: for every target, you define the dependencies and the command to create the target from the dependencies.
I have experimented with depcode in a few programming projects of my own (notably mapstitch). It works, and you can create programs with it. It makes some things more flexible, but it also adds some extra syntax and makes debugging somewhat more cumbersome due to less obvious code execution paths. On the other hand, the dependency graphs are great in quickly grasping the program structure. I think it would really show its strength in larger GUI projects, but I have yet to try to create such things.
Examples
Code samples
A simple dependency defininition:
input1 = Dep(value=3,id="input1") output1 = Dep((input1,),lambda x: 2*x,id="output1") # output1.value is 6 input1.value = 4 # output1.value is 8
Add a listener:
def p(x): print x output2 = Dep((input1,), p, id="output2",listener=True) # prints the value of input1 whenever it is changed
A syntactic alternative using decorators:
@dep(input1, id="output3", listener=True) def output3(x): print x # equivalent to output2
Dependency graphs
Below is an example of a simple and crude test program (plottest.py in the package), plotting a sum of two sine waves and the respective spectrum. The plotters, shown as diamonds, are listeners, i.e. they are automatically updated whenever any value in their dependency tree changes. The rectangles are blocks depending on other blocks, and the circles are simple values.
An example of a more complicated program is below: the dependency graph of align.py of mapstitch.
Licence
The software is licenced under the X11 licence, which is a free and open-source licence, compatible with the GPL. The licence details can be found in the package.
Download
Note that while depcode.py itself has no dependencies, the example file plottest.py requires SciPy, Matplotlib, and yapgvb. On Ubuntu (and probably other Debian-derivatives), you can install them like this:
sudo apt-get install python-scipy python-matplotlib python-yapgvb
Acknowledgments
Antti Kaihola has given insightful comments on the concept itself and implemented the decorator syntax.