>>> from peak.events import trellis, collections |
>>> from peak.events import trellis, collections |
|
|
|
|
|
.. contents:: **Table of Contents** |
|
|
|
|
SortedSet |
SortedSet |
--------- |
--------- |
|
|
example, the Trellis package includes a ``SortedSet`` type that maintains an |
example, the Trellis package includes a ``SortedSet`` type that maintains an |
index of items sorted by keys, with a cell that lists changed regions. |
index of items sorted by keys, with a cell that lists changed regions. |
|
|
A ``trellis.SortedSet`` is a specialized component that lets you wrap a |
A ``collections.SortedSet`` is a specialized component that lets you wrap a |
``trellis.Set`` (or anything similar to one, that offers iteration plus |
``trellis.Set`` (or anything similar to one, that offers iteration plus |
``added`` and ``removed`` cells) with a sort key, to get a list:: |
``added`` and ``removed`` cells) with a sort key, to get a list:: |
|
|
>>> class Viewer(trellis.Component): |
>>> class Viewer(trellis.Component): |
... trellis.values(model = None) |
... trellis.values(model = None) |
... |
... |
... @trellis.action |
... @trellis.observer |
... def view_it(self): |
... def view_it(self): |
... if self.model is not None: |
... if self.model is not None: |
... print self.model |
... print self.model |
>>> view.model = None # quiet, please |
>>> view.model = None # quiet, please |
|
|
>>> class Watcher(trellis.Component): |
>>> class Watcher(trellis.Component): |
... @trellis.action |
... @trellis.observer |
... def dump(self): |
... def dump(self): |
... print myIndex.changes |
... print myIndex.changes |
|
|
>>> myIndex |
>>> myIndex |
[0, 1, 4] |
[0, 1, 4] |
|
|
TODO: ``reversed`` flag, test changes to ``.data`` |
As does setting or clearing the ``reverse`` flag (which reverses the effect of |
|
the sort key):: |
|
|
|
>>> myIndex.reverse = True |
|
[(0, 3, 3)] |
|
[] |
|
>>> myIndex |
|
[4, 1, 0] |
|
|
|
>>> myItems.add(7) |
|
[(0, 0, 1)] |
|
[] |
|
>>> myIndex |
|
[7, 4, 1, 0] |
|
|
|
>>> myItems.add(2) |
|
[(2, 2, 1)] |
|
[] |
|
>>> myIndex |
|
[7, 4, 2, 1, 0] |
|
|
|
>>> myItems.remove(7) |
|
[(0, 1, 0)] |
|
[] |
|
|
|
>>> myItems.remove(2) |
|
[(1, 2, 0)] |
|
[] |
|
|
|
>>> myIndex |
|
[4, 1, 0] |
|
|
|
>>> myIndex.reverse = False |
|
[(0, 3, 3)] |
|
[] |
|
|
|
>>> myIndex |
|
[0, 1, 4] |
|
|
|
|
|
TODO: test changes to ``.data`` |
|
|
|
|
SubSet |
SubSet |
|
|
>>> def show(): |
>>> def show(): |
... print "Changes:", o.changes |
... print "Changes:", o.changes |
>>> c = trellis.ActionCell(show) |
>>> c = trellis.ObserverCell(show) |
>>> c.value |
|
Changes: {} |
Changes: {} |
|
|
The ``changes`` attribute is a dictionary (a regular dictionary, not a |
The ``changes`` attribute is a dictionary (a regular dictionary, not a |