Package cells :: Module model :: Class Model
[hide private]
[frames] | no frames]

Class Model
source code

object --+
         |
        Model
Known Subclasses:
family.Family

A class in which CellAttrs may be used. Models automatically bring their cells up-to-date at __init__-time. Cells may be altered at runtime by passing attrname=value, or attrname=hash to the constructor.

Nested Classes [hide private]
__metaclass__  

Instance Methods [hide private]
  __init__(self, *args, **kwargs)
__init__(self, [<attrname>=<value, rule or dict>], ...) -> None
  __setattr__(self, key, value)
Per KT's spec, Models may not set non-cell attributes after __init__.
  _buildcell(self, name, *args, **kwargs)
  _run_observers(self, attribute)
Runs each observer in turn.

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __str__


Class Methods [hide private]
  observer(attrib=None, oldvalue=None, newvalue=None)
A classmethod to add an observer attribute to a Model.

Class Variables [hide private]
_initialized  
_noncells  
_observernames  

Inherited from object: __class__


Instance Variables [hide private]
model_name A cell holding The name of this Model.
model_value A cell holding the value of this Model.
parent A cell for Family graph traversal.

Method Details [hide private]

__init__(self, *args, **kwargs)
(Constructor)

source code 

__init__(self, [<attrname>=<value, rule or dict>], ...) -> None

Initialize a Model with optional overrides. By passing a parameter with the same name as a cell attribute, you may override that cell attribute. For example:
>>> class A(cells.Model):
...     x = cells.makecell(value=1)
... 
>>> a1 = A()
>>> a1.x
1

>>> a2 = A(x="blah")
>>> a2.x
'blah'
This override can be arbitrarily complex; for instance, you can make a RuleCell into a ValueCell, change a attribute's celltype ... In short, anything you can do at Model defintion time you can alter at instantiation time:
>>> class B(cells.Model):
...     x = cells.makecell(rule=lambda s,p: 3 * s.y)
...     y = cells.makecell(value=2)
... 
>>> b = B()
>>> b.x
6

>>> b.y = 1
>>> b.x
3

>>> b = B(y=10)
>>> b.x
30

>>> b.y
10

>>> b = B(x={'celltype': cells.RuleThenInputCell})
>>> b.x
6

>>> b.y
2

>>> b.x = 5
>>> b.x
5

>>> b.y = 1
>>> b.x
5
Parameters:
  • attrname - The name of the attribute you wish to override. If this is set to a callable, it will override the rule for the cell. If it's set to a dictionary with one or more of 'rule', 'value', or 'celltype', those attributes will be overridden in the cell. Otherwise, it will override the value of the target cell.
Overrides: object.__init__

__setattr__(self, key, value)

source code 
Per KT's spec, Models may not set non-cell attributes after __init__.
Raises:
  • NonCellSetError - If you try to set a non-cell attribute
Overrides: object.__setattr__

_buildcell(self, name, *args, **kwargs)

source code 

_run_observers(self, attribute)

source code 
Runs each observer in turn. There's some optimization that could go on here, if it turns out to be neccessary.

observer(attrib=None, oldvalue=None, newvalue=None)

source code 
A classmethod to add an observer attribute to a Model. The observer may be set to fire on any change in the model, any change in an attribute, or when a function testing the new or old value of a cell returns true.
>>> 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 = A()
>>> a.x
4

>>> a.x = 5
New value of x is odd!

>>> a.x = 42
>>> a.x = 11
New value of x is odd!
Parameters:
  • attrib - An attribute name to attach the observer to
  • oldvalue - A function to run on the now-out-of-date value of a cell which changed in this datapulse; if the function returns True, the observer will fire. The signature for the function must be f(val) -> bool
  • newvalue - A function to run on up-to-date value; if the function returns True, the observer will fire. The signature for the function must be f(val) -> bool
Returns:
decorator


Class Variable Details [hide private]

_initialized

Value:
False                                                                  
      

_noncells

Value:
set(['__module__', '__metaclass__', 'observer', '_initialized', '__set\
attr__', '_buildcell', '_run_observers', '__doc__', '__init__'])       
      

_observernames

Value:
set([])                                                                
      

Instance Variable Details [hide private]

model_name

A cell holding The name of this Model. By default, None.
Value:
<cells.cellattr.CellAttr object at 0x70bab0>                           
      

model_value

A cell holding the value of this Model. By default, None.
Value:
<cells.cellattr.CellAttr object at 0x70bb70>                           
      

parent

A cell for Family graph traversal. By default, None.
Value:
<cells.cellattr.CellAttr object at 0x70bb90>