DisplayList in Archetypes manipulieren
DisplayLists sortiere, Elemente hinzufügen und löschen...
Da diese Infos etwas versteckt sind, habe ich sie hier mal festgehalten:
Static display lists, can look up on
either side of the dict, and get them in sorted order
NOTE: Both keys and values *must* contain unique entries! You can have
two times the same value. This is a "feature" not a bug. DisplayLists
are meant to be used as a list inside html form entry like a drop down.
>>> dl = DisplayList()
Add some keys
>>> dl.add('foo', 'bar')
>>> dl.add('egg', 'spam')
Assert some values
>>> dl.index
2
>>> dl.keys()
['foo', 'egg']
>>> dl.values()
['bar', 'spam']
>>> dl.items()
(('foo', 'bar'), ('egg', 'spam'))
You can't use e.g. objects as keys or values
>>> dl.add(object(), 'error')
Traceback (most recent call last):
TypeError: DisplayList keys must be strings or ints, got <type 'object'>
>>> dl.add('error', object())
Traceback (most recent call last):
TypeError: DisplayList values must be strings or ints, got <type 'object'>
GOTCHA
Adding a value a second time does overwrite the key, too!>>> dl.add('fobar' ,'spam')
>>> dl.keys()
['foo', 'fobar']
>>> dl.items()
(('foo', 'bar'), ('fobar', 'spam'))
Install warning hook for the next tests since they will raise a warning
and I don't want to spoil the logs.
>>> from Testing.ZopeTestCase import WarningsHook
>>> w = WarningsHook(); w.install()
Using ints as DisplayList keys works but will raise an deprecation warning
You should use IntDisplayList for int keys
>>> idl = DisplayList()
>>> idl.add(1, 'number one')
>>> idl.add(2, 'just the second')
>>> idl.items()
((1, 'number one'), (2, 'just the second'))
Remove warning hook
>>> w.uninstall(); del w
DisplayList sortieren
Zum sortieren stell eine DisplayList die beiden folgenden Methoden bereit:- sortedByValue()
- sortedByKey()
listSortedByKey = mydisplaylist.sortedByKey()