Mercurial > pylearn
annotate doc/v2_planning/arch_src/plugin_JB.py @ 1251:70ca63c05672
comment on OD's reply
author | Razvan Pascanu <r.pascanu@gmail.com> |
---|---|
date | Thu, 23 Sep 2010 13:44:50 -0400 |
parents | 9fac28d80fb7 |
children |
rev | line source |
---|---|
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
1 """plugin_JB - draft of potential library architecture using iterators |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
2 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
3 This strategy makes use of a simple imperative language whose statements are python function |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
4 calls to create learning algorithms that can be manipulated and executed in several desirable |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
5 ways. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
6 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
7 The training procedure for a PCA module is easy to express: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
8 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
9 # allocate the relevant modules |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
10 dataset = Dataset(numpy.random.RandomState(123).randn(13,1)) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
11 pca = PCA_Analysis() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
12 pca_batchsize=1000 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
13 |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
14 reg = Registers() |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
15 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
16 # define the control-flow of the algorithm |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
17 train_pca = SEQ([ |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
18 REPEAT(pca_batchsize, CALL(dataset.next, store_to=reg('x'))), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
19 CALL(pca.analyze, reg('x'))]) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
20 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
21 # run the program |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
22 train_pca.run() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
23 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
24 The CALL, SEQ, and REPEAT are control-flow elements. The control-flow elements I |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
25 defined so far are: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
26 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
27 - CALL - a basic statement, just calls a python function |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
28 - SEQ - a sequence of elements to run in order |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
29 - REPEAT - do something N times (and return None or maybe the last CALL?) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
30 - LOOP - do something an infinite number of times |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
31 - CHOOSE - like a switch statement (should rename to SWITCH) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
32 - WEAVE - interleave execution of multiple control-flow elements |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
33 - POPEN - launch a process and return its status when it's complete |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
34 - PRINT - a shortcut for CALL(print_obj) |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
35 - SPAWN - run a program fragment asynchronously in another process |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
36 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
37 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
38 We don't have many requirements per-se for the architecture, but I think this design respects |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
39 and realizes all of them. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
40 The advantages of this approach are: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
41 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
42 - algorithms (including partially run ones) are COPYABLE, and SERIALIZABLE |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
43 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
44 - algorithms can be executed without seizing control of the python process (the run() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
45 method does this, but if you look inside it you'll see it's a simple for loop) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
46 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
47 - it is easy to execute an algorithm step by step in a main loop that also checks for |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
48 network or filesystem events related to e.g. job management. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
49 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
50 - the library can provide learning algorithms via control-flow templates, and the user can |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
51 edit them (with search/replace calls) to include HOOKS, and DIAGNOSTIC plug-in |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
52 functionality |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
53 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
54 e.g. prog.find(CALL(cd1_update, layer=layer1)).replace_with( |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
55 SEQ([CALL(cd1_update, layer=layer1), CALL(my_debugfn)])) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
56 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
57 - user can print the 'program code' of an algorithm built from library pieces |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
58 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
59 - program can be optimized automatically. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
60 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
61 - e.g. BUFFER(N, CALL(dataset.next)) could be replaced if dataset.next implements the |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
62 right attribute/protocol for 'bufferable' or something. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
63 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
64 - e.g. SEQ([a,b,c,d]) could be compiled to a single CALL to a Theano-compiled function |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
65 if a, b, c, and d are calls to callable objects that export something like a |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
66 'theano_SEQ' interface |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
67 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
68 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
69 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
70 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
71 __license__ = 'TODO' |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
72 __copyright__ = 'TODO' |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
73 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
74 import cPickle, copy, os, subprocess, sys, time |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
75 import numpy |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
76 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
77 #################################################### |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
78 # CONTROL-FLOW CONSTRUCTS |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
79 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
80 class INCOMPLETE: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
81 """Return value for Element.step""" |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
82 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
83 class ELEMENT(object): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
84 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
85 Base class for control flow elements (e.g. CALL, REPEAT, etc.) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
86 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
87 The design is that every element has a driver, that is another element, or the iterator |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
88 implementation in the ELEMENT class. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
89 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
90 the driver calls start when entering a new control element |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
91 - this would be called once per e.g. outer loop iteration |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
92 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
93 the driver calls step to advance the control element |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
94 - which returns INCOMPLETE |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
95 - which returns any other object to indicate completion |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
96 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
97 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
98 # subclasses should override these methods: |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
99 def start(self): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
100 pass |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
101 def step(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
102 pass |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
103 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
104 # subclasses should typically not override these: |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
105 def run(self, n_steps=float('inf')): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
106 self.start() |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
107 i = 0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
108 r = self.step() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
109 while r is INCOMPLETE: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
110 i += 1 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
111 #TODO make sure there is not an off-by-one error |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
112 if i > n_steps: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
113 break |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
114 r = self.step() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
115 return r |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
116 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
117 class BUFFER_REPEAT(ELEMENT): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
118 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
119 Accumulate a number of return values into one list / array. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
120 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
121 The source of return values `src` is a control element that will be restarted repeatedly in |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
122 order to fulfil the requiement of gathering N samples. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
123 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
124 TODO: support accumulating of tuples of arrays |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
125 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
126 def __init__(self, N, src, storage=None): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
127 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
128 TODO: use preallocated `storage` |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
129 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
130 self.N = N |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
131 self.n = 0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
132 self.src = src |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
133 self.storage = storage |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
134 self.src.start(None) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
135 if self.storage != None: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
136 raise NotImplementedError() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
137 def start(self, arg): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
138 self.buf = [None] * self.N |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
139 self.n = 0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
140 self.finished = False |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
141 def step(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
142 assert not self.finished |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
143 r = self.src.step() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
144 if r is INCOMPLETE: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
145 return r |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
146 self.src.start(None) # restart our stream |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
147 self.buf[self.n] = r |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
148 self.n += 1 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
149 if self.n == self.N: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
150 self.finished = True |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
151 return self.buf |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
152 else: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
153 return INCOMPLETE |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
154 assert 0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
155 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
156 class CALL(ELEMENT): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
157 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
158 Control flow terminal - call a python function or method. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
159 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
160 Returns the return value of the call. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
161 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
162 def __init__(self, fn, *args, **kwargs): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
163 self.fn = fn |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
164 self.args = args |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
165 self.kwargs=kwargs |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
166 def start(self): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
167 self.finished = False |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
168 return self |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
169 def step(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
170 assert not self.finished |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
171 self.finished = True |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
172 fn_rval = self.fn(*self.lookup_args(), **self.lookup_kwargs()) |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
173 if '_set' in self.kwargs: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
174 self.kwargs['_set'].set(fn_rval) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
175 def __getstate__(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
176 rval = dict(self.__dict__) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
177 if type(self.fn) is type(self.step): #instancemethod |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
178 fn = rval.pop('fn') |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
179 rval['i fn'] = fn.im_func, fn.im_self, fn.im_class |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
180 return rval |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
181 def __setstate__(self, dct): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
182 if 'i fn' in dct: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
183 dct['fn'] = type(self.step)(*dct.pop('i fn')) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
184 self.__dict__.update(dct) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
185 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
186 def lookup_args(self): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
187 rval = [] |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
188 for a in self.args: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
189 if isinstance(a, Register): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
190 rval.append(a.get()) |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
191 else: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
192 rval.append(a) |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
193 return rval |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
194 def lookup_kwargs(self): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
195 rval = {} |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
196 for k,v in self.kwargs.iteritems(): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
197 if k == '_set': |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
198 continue |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
199 if isinstance(v, Register): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
200 rval[k] = v.get() |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
201 else: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
202 rval[k] = v |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
203 return rval |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
204 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
205 def CHOOSE(which, options): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
206 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
207 Execute one out of a number of optional control flow paths |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
208 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
209 raise NotImplementedError() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
210 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
211 def LOOP(element): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
212 #TODO: implement a true infinite loop |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
213 return REPEAT(sys.maxint, element) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
214 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
215 class REPEAT(ELEMENT): |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
216 def __init__(self, N, element, counter=None): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
217 self.N = N |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
218 if not isinstance(element, ELEMENT): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
219 raise TypeError(element) |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
220 self.element = element |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
221 self.counter = counter |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
222 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
223 #TODO: check for N being callable |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
224 def start(self): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
225 self.n = 0 #loop iteration |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
226 self.finished = False |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
227 self.element.start() |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
228 if self.counter: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
229 self.counter.set(0) |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
230 |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
231 def step(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
232 assert not self.finished |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
233 r = self.element.step() |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
234 if r is INCOMPLETE: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
235 return INCOMPLETE |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
236 self.n += 1 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
237 if self.counter: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
238 self.counter.set(self.n) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
239 if self.n < self.N: |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
240 self.element.start() |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
241 return INCOMPLETE |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
242 else: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
243 self.finished = True |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
244 return r |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
245 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
246 class SEQ(ELEMENT): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
247 def __init__(self, elements): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
248 self.elements = list(elements) |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
249 def start(self): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
250 if len(self.elements): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
251 self.elements[0].start() |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
252 self.pos = 0 |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
253 self.finished = False |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
254 def step(self): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
255 if self.pos == len(self.elements): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
256 self.finished=True |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
257 return |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
258 r = self.elements[self.pos].step() |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
259 if r is INCOMPLETE: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
260 return r |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
261 self.pos += 1 |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
262 if self.pos < len(self.elements): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
263 self.elements[self.pos].start() |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
264 return INCOMPLETE |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
265 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
266 class WEAVE(ELEMENT): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
267 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
268 Interleave execution of a number of elements. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
269 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
270 TODO: allow a schedule (at least relative frequency) of elements from each program |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
271 """ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
272 def __init__(self, n_required, elements): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
273 self.elements = elements |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
274 if n_required == -1: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
275 self.n_required = len(elements) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
276 else: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
277 self.n_required = n_required |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
278 def start(self): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
279 for el in self.elements: |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
280 el.start() |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
281 self.elem_finished = [0] * len(self.elements) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
282 self.idx = 0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
283 self.finished= False |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
284 def step(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
285 assert not self.finished # if this is triggered, we have a broken driver |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
286 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
287 #start with this check in case there were no elements |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
288 # it's possible for the number of finished elements to exceed the threshold |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
289 if sum(self.elem_finished) >= self.n_required: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
290 self.finished = True |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
291 return None |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
292 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
293 # step the active element |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
294 r = self.elements[self.idx].step() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
295 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
296 if r is not INCOMPLETE: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
297 self.elem_finished[self.idx] = True |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
298 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
299 # check for completion |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
300 if sum(self.elem_finished) >= self.n_required: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
301 self.finished = True |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
302 return None |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
303 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
304 # advance to the next un-finished element |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
305 self.idx = (self.idx+1) % len(self.elements) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
306 while self.elem_finished[self.idx]: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
307 self.idx = (self.idx+1) % len(self.elements) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
308 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
309 return INCOMPLETE |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
310 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
311 class POPEN(ELEMENT): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
312 def __init__(self, args): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
313 self.args = args |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
314 def start(self): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
315 self.p = subprocess.Popen(self.args) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
316 def step(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
317 r = self.p.poll() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
318 if r is None: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
319 return INCOMPLETE |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
320 return r |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
321 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
322 def PRINT(obj): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
323 return CALL(print_obj, obj) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
324 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
325 class SPAWN(ELEMENT): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
326 SUCCESS = 0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
327 def __init__(self, data, prog): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
328 self.data = data |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
329 self.prog = prog |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
330 def start(self): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
331 # pickle the (data, prog) pair |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
332 s = cPickle.dumps((self.data, self.prog)) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
333 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
334 # call python with a stub function that |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
335 # unpickles the data, prog pair and starts running the prog |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
336 self.rpipe, wpipe = os.pipe() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
337 code = 'import sys, plugin_JB; sys.exit(plugin_JB.SPAWN._main(%i))'%wpipe |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
338 self.p = subprocess.Popen( |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
339 ['python', '-c', code], |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
340 stdin=subprocess.PIPE) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
341 # send the data and prog to the other process |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
342 self.p.stdin.write(s) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
343 self.finished= False |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
344 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
345 #TODO: send over tgz of the modules this code needs |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
346 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
347 #TODO: When the client process is on a different machine, negotiate with the client |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
348 # process to determine which modules it needs, and send over the code for pure python |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
349 # ones. Make sure versions match for non-pure python ones. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
350 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
351 def step(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
352 assert not self.finished |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
353 r = self.p.poll() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
354 if r is None: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
355 return INCOMPLETE # typical exit case |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
356 self.finished = True |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
357 if r != self.SUCCESS: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
358 print "UH OH", r # TODO - ??? |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
359 rfile = os.fdopen(self.rpipe) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
360 # recv the revised of the data dictionary |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
361 data = cPickle.load(rfile) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
362 # modify the data dict in-place |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
363 # for new values to be visible to other components |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
364 self.data.update(data) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
365 rfile.close() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
366 #TODO: return something meaningful? like r? |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
367 return None |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
368 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
369 @staticmethod |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
370 def _main(wpipe): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
371 #TODO: unpack and install tgz of the modules this code needs |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
372 data, prog = cPickle.load(sys.stdin) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
373 rval = prog.run() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
374 os.write(wpipe, cPickle.dumps(data)) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
375 return SPAWN.SUCCESS |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
376 #os.close(wpipe) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
377 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
378 class Register(object): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
379 def __init__(self, registers, key): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
380 self.registers = registers |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
381 self.key = key |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
382 def set(self, val): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
383 self.registers[self.key] = val |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
384 def get(self): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
385 return self.registers[self.key] |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
386 class Registers(dict): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
387 def __call__(self, key): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
388 return Register(self, key) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
389 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
390 def print_obj(obj): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
391 print obj |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
392 def print_obj_attr(obj, attr): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
393 print getattr(obj, attr) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
394 def no_op(*args, **kwargs): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
395 pass |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
396 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
397 def importable_fn(d): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
398 d['new key'] = len(d) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
399 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
400 |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
401 if __name__ == '__main__': |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
402 print 'this is the library file, run "python plugin_JB_main.py"' |