| Home | Trees | Index | Help |
|
|---|
|
|
object --+
|
Observer
Wrapper for a function which fires when a Model updates
and certain conditions are met. Observers may be bound to specific
attributes or whether a function returns true when handed a cell's old
value or new value, or any combination of the above. An observer that has
no conditions on its running runs whenever the Model updates. Observers
with multiple conditions will only fire when all the conditions pass.
Observers run at most once per datapulse.
Model.observer decorator to add Observers to
Models:
>>> import cells >>> class A(cells.Model): ... x = cells.makecell(value=4) ... >>> @A.observer(attrib="x", ... newvalue=lambda a: a % 2) ... def odd_x_obs(model): ... print "New value of x is odd!" ... >>> @A.observer(attrib="x") ... def x_obs(model): ... print "x got changed!" ... >>> @A.observer() ... def model_obs(model): ... print "something in the model changed" ... >>> @A.observer(attrib="x", ... newvalue=lambda a: a % 2, ... oldvalue=lambda a: not (a % 2)) ... def was_even_now_odd_x_obs(model): ... print "New value of x is odd, and it was even!" ... >>> a = A() something in the model changed x got changed! >>> a.x = 5 something in the model changed x got changed! New value of x is odd! New value of x is odd, and it was even! >>> a.x = 11 something in the model changed x got changed! New value of x is odd! >>> a.x = 42 something in the model changed x got changed!
|
|||
|
__init__(self,
attrib,
oldvalue,
newvalue,
func)
Initializes a new Observer. |
|||
|
run_if_applicable(self,
model,
attr)
Determine whether this observer should fire, and fire if appropriate. |
|||
|
Inherited from |
|||
|
|||
|
Inherited from |
|||
|
|||
| attrib | (optional) The cell name this observer watches. | ||
| func | The function to run when the observer fires. | ||
| last_ran | The DP this observer last ran in. | ||
| newvalue | A function (signature: f(val) -> bool) which, if
it returns True when passed a changed cell's out-of-date
value, allows the observer to fire. |
||
| oldvalue | A function (signature: f(val) -> bool) which, if
it returns True when passed a changed cell's out-of-date
value, allows the observer to fire. |
||
|
|||
Initializes a new Observer. All arguments are required, but only func is required to be anything but none. See attrib, oldvalue, and newvalue instance variable docs for explanation of their utility.
|
|
|
|||
attrib(optional) The cell name this observer watches. Only when a cell with this name changes will the observer fire. You may also pass a list of cell names to "watch". |
funcThe function to run when the observer fires. Signature:f(model_instance) -> (ignored)
|
last_ranThe DP this observer last ran in. Observers only run once per DP. |
newvalueA function (signature:f(val) -> bool) which, if it
returns True when passed a changed cell's out-of-date value,
allows the observer to fire.
|
oldvalueA function (signature:f(val) -> bool) which, if it
returns True when passed a changed cell's out-of-date value,
allows the observer to fire.
|
| Home | Trees | Index | Help |
|
|---|
| Generated by Epydoc 3.0alpha2 on Sun Aug 20 15:38:10 2006 | http://epydoc.sf.net |