annotate port.py @ 0:45b12307c695

Initial revision
author drewp
date Wed, 03 Jul 2002 09:37:57 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
1
45b12307c695 Initial revision
drewp
parents:
diff changeset
2 '''
45b12307c695 Initial revision
drewp
parents:
diff changeset
3 Snippet Pi=3: RFC 2: New port semantics
45b12307c695 Initial revision
drewp
parents:
diff changeset
4 '''
45b12307c695 Initial revision
drewp
parents:
diff changeset
5
45b12307c695 Initial revision
drewp
parents:
diff changeset
6 # an example of the max node's op
45b12307c695 Initial revision
drewp
parents:
diff changeset
7 def changed(self, inputs):
45b12307c695 Initial revision
drewp
parents:
diff changeset
8 # note how this function does not use stateaccess, as it doesn't use state
45b12307c695 Initial revision
drewp
parents:
diff changeset
9 return max(inputs.values())
45b12307c695 Initial revision
drewp
parents:
diff changeset
10
45b12307c695 Initial revision
drewp
parents:
diff changeset
11 # so, how the heck does this work?
45b12307c695 Initial revision
drewp
parents:
diff changeset
12 # we check the function to get the names of kw args in the function.
45b12307c695 Initial revision
drewp
parents:
diff changeset
13 # we always pass self, but everything else is optional
45b12307c695 Initial revision
drewp
parents:
diff changeset
14 # the node asked for inputs, which looks like this:
45b12307c695 Initial revision
drewp
parents:
diff changeset
15 # inputs = {'portname' : PortObj, 'portname2', PortObj}
45b12307c695 Initial revision
drewp
parents:
diff changeset
16 # somehow, the PortObjs are max'ible.
45b12307c695 Initial revision
drewp
parents:
diff changeset
17 # the node has only one output so it can just return the value to set the
45b12307c695 Initial revision
drewp
parents:
diff changeset
18 # output. (maybe)
45b12307c695 Initial revision
drewp
parents:
diff changeset
19 # alteratively, if we decide that you always return a new dict of outputs:
45b12307c695 Initial revision
drewp
parents:
diff changeset
20 # return {'outputportname' : max(inputs.values())}
45b12307c695 Initial revision
drewp
parents:
diff changeset
21 # which isn't horrible, but not great
45b12307c695 Initial revision
drewp
parents:
diff changeset
22
45b12307c695 Initial revision
drewp
parents:
diff changeset
23 # another example: an adder. the node has ports A and B, and an output C:
45b12307c695 Initial revision
drewp
parents:
diff changeset
24 # C also gets capped at stateaccess[min].
45b12307c695 Initial revision
drewp
parents:
diff changeset
25 def changed(self, a, b, c, stateaccess):
45b12307c695 Initial revision
drewp
parents:
diff changeset
26 c.set(max(stateaccess['min'], a + b))
45b12307c695 Initial revision
drewp
parents:
diff changeset
27 return {}
45b12307c695 Initial revision
drewp
parents:
diff changeset
28
45b12307c695 Initial revision
drewp
parents:
diff changeset
29 # or:
45b12307c695 Initial revision
drewp
parents:
diff changeset
30 def changed(self, a, b, stateaccess):
45b12307c695 Initial revision
drewp
parents:
diff changeset
31 c = max(stateaccess['min'], a + b)
45b12307c695 Initial revision
drewp
parents:
diff changeset
32 return {'c' : c}
45b12307c695 Initial revision
drewp
parents:
diff changeset
33
45b12307c695 Initial revision
drewp
parents:
diff changeset
34 # which i think is clearer. doing all port changes at the end has some
45b12307c695 Initial revision
drewp
parents:
diff changeset
35 # book-keeping advantages (we can detect easily which ports are changed)
45b12307c695 Initial revision
drewp
parents:
diff changeset
36 # the counter node could work this way:
45b12307c695 Initial revision
drewp
parents:
diff changeset
37
45b12307c695 Initial revision
drewp
parents:
diff changeset
38 def changed(self, someoutput):
45b12307c695 Initial revision
drewp
parents:
diff changeset
39 return {'someoutput' : someoutput + 1}
45b12307c695 Initial revision
drewp
parents:
diff changeset
40
45b12307c695 Initial revision
drewp
parents:
diff changeset
41