annotate Port.py @ 92:827a675131dc

better attach behavior, "attached" -> "synced"
author dmcc
date Sat, 13 Jul 2002 03:01:42 +0000
parents 8856efb6516c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
1 from nodetypes import DiscoType
45b12307c695 Initial revision
drewp
parents:
diff changeset
2
45b12307c695 Initial revision
drewp
parents:
diff changeset
3 ANY = -1
45b12307c695 Initial revision
drewp
parents:
diff changeset
4
45b12307c695 Initial revision
drewp
parents:
diff changeset
5 class Port:
45b12307c695 Initial revision
drewp
parents:
diff changeset
6 def __setattr__(self, key, value):
45b12307c695 Initial revision
drewp
parents:
diff changeset
7 '''Alias for __setitem___'''
45b12307c695 Initial revision
drewp
parents:
diff changeset
8 self[key] = value
45b12307c695 Initial revision
drewp
parents:
diff changeset
9 def __setitem__(self, key, value):
45b12307c695 Initial revision
drewp
parents:
diff changeset
10 pass
45b12307c695 Initial revision
drewp
parents:
diff changeset
11 def __getattr__(self, key):
45b12307c695 Initial revision
drewp
parents:
diff changeset
12 '''Alias for __getitem___'''
45b12307c695 Initial revision
drewp
parents:
diff changeset
13 return self[key]
45b12307c695 Initial revision
drewp
parents:
diff changeset
14 def __getitem__(self, key):
45b12307c695 Initial revision
drewp
parents:
diff changeset
15 pass
45b12307c695 Initial revision
drewp
parents:
diff changeset
16
45b12307c695 Initial revision
drewp
parents:
diff changeset
17 class InputPort(Port):
3
8856efb6516c dmx out example node and types
drewp
parents: 2
diff changeset
18 def __init__(self, allowedtype, required=1, maxpins=ANY):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
19 self.pins = []
45b12307c695 Initial revision
drewp
parents:
diff changeset
20
45b12307c695 Initial revision
drewp
parents:
diff changeset
21 class OutputPort(Port):
45b12307c695 Initial revision
drewp
parents:
diff changeset
22 def __init__(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
23 self.pins = []
45b12307c695 Initial revision
drewp
parents:
diff changeset
24
45b12307c695 Initial revision
drewp
parents:
diff changeset
25 class Pin:
45b12307c695 Initial revision
drewp
parents:
diff changeset
26 def __init__(self, connection, value=DiscoType):
45b12307c695 Initial revision
drewp
parents:
diff changeset
27 pass
45b12307c695 Initial revision
drewp
parents:
diff changeset
28
45b12307c695 Initial revision
drewp
parents:
diff changeset
29 '''
45b12307c695 Initial revision
drewp
parents:
diff changeset
30 Snippet Pi=3: RFC 2: New port semantics
45b12307c695 Initial revision
drewp
parents:
diff changeset
31
45b12307c695 Initial revision
drewp
parents:
diff changeset
32 # an example of the max node's op
45b12307c695 Initial revision
drewp
parents:
diff changeset
33 def changed(self, inputs):
45b12307c695 Initial revision
drewp
parents:
diff changeset
34 # note how this function does not use stateaccess, as it doesn't use state
45b12307c695 Initial revision
drewp
parents:
diff changeset
35 return max(inputs.values())
45b12307c695 Initial revision
drewp
parents:
diff changeset
36
45b12307c695 Initial revision
drewp
parents:
diff changeset
37 # so, how the heck does this work?
45b12307c695 Initial revision
drewp
parents:
diff changeset
38 # we check the function to get the names of kw args in the function.
45b12307c695 Initial revision
drewp
parents:
diff changeset
39 # we always pass self, but everything else is optional
45b12307c695 Initial revision
drewp
parents:
diff changeset
40 # the node asked for inputs, which looks like this:
45b12307c695 Initial revision
drewp
parents:
diff changeset
41 # inputs = {'portname' : PortObj, 'portname2', PortObj}
45b12307c695 Initial revision
drewp
parents:
diff changeset
42 # somehow, the PortObjs are max'ible.
45b12307c695 Initial revision
drewp
parents:
diff changeset
43 # the node has only one output so it can just return the value to set the
45b12307c695 Initial revision
drewp
parents:
diff changeset
44 # output. (maybe)
45b12307c695 Initial revision
drewp
parents:
diff changeset
45 # alteratively, if we decide that you always return a new dict of outputs:
45b12307c695 Initial revision
drewp
parents:
diff changeset
46 # return {'outputportname' : max(inputs.values())}
45b12307c695 Initial revision
drewp
parents:
diff changeset
47 # which isn't horrible, but not great
45b12307c695 Initial revision
drewp
parents:
diff changeset
48
45b12307c695 Initial revision
drewp
parents:
diff changeset
49 # another example: an adder. the node has ports A and B, and an output C:
45b12307c695 Initial revision
drewp
parents:
diff changeset
50 # C also gets capped at stateaccess[min].
45b12307c695 Initial revision
drewp
parents:
diff changeset
51 def changed(self, a, b, c, stateaccess):
45b12307c695 Initial revision
drewp
parents:
diff changeset
52 c.set(max(stateaccess['min'], a + b))
45b12307c695 Initial revision
drewp
parents:
diff changeset
53 return {}
45b12307c695 Initial revision
drewp
parents:
diff changeset
54
45b12307c695 Initial revision
drewp
parents:
diff changeset
55 # or:
45b12307c695 Initial revision
drewp
parents:
diff changeset
56 def changed(self, a, b, stateaccess):
45b12307c695 Initial revision
drewp
parents:
diff changeset
57 c = max(stateaccess['min'], a + b)
45b12307c695 Initial revision
drewp
parents:
diff changeset
58 return {'c' : c}
45b12307c695 Initial revision
drewp
parents:
diff changeset
59
45b12307c695 Initial revision
drewp
parents:
diff changeset
60 # which i think is clearer. doing all port changes at the end has some
45b12307c695 Initial revision
drewp
parents:
diff changeset
61 # book-keeping advantages (we can detect easily which ports are changed)
45b12307c695 Initial revision
drewp
parents:
diff changeset
62 # the counter node could work this way:
45b12307c695 Initial revision
drewp
parents:
diff changeset
63
45b12307c695 Initial revision
drewp
parents:
diff changeset
64 def changed(self, someoutput):
45b12307c695 Initial revision
drewp
parents:
diff changeset
65 return {'someoutput' : someoutput + 1}
45b12307c695 Initial revision
drewp
parents:
diff changeset
66 '''
45b12307c695 Initial revision
drewp
parents:
diff changeset
67
45b12307c695 Initial revision
drewp
parents:
diff changeset
68 '''
45b12307c695 Initial revision
drewp
parents:
diff changeset
69 type 1: a, b, d, e
45b12307c695 Initial revision
drewp
parents:
diff changeset
70 type 2: b, c, d, f
45b12307c695 Initial revision
drewp
parents:
diff changeset
71
45b12307c695 Initial revision
drewp
parents:
diff changeset
72 conversion maps:
45b12307c695 Initial revision
drewp
parents:
diff changeset
73 a -> [ ]
45b12307c695 Initial revision
drewp
parents:
diff changeset
74 b -> b
45b12307c695 Initial revision
drewp
parents:
diff changeset
75 d -> d
45b12307c695 Initial revision
drewp
parents:
diff changeset
76 e -> f
45b12307c695 Initial revision
drewp
parents:
diff changeset
77 '''