Back to index

plone3  3.1.7
Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes
Archetypes.utils.DisplayList Class Reference
Inheritance diagram for Archetypes.utils.DisplayList:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def __repr__
def __str__
def __call__
def fromList
def __len__
def __add__
def index_sort
def add
def getKey
def getValue
def getMsgId
def keys
def values
def items
def sortedByValue
def sortedByKey
def __cmp__
def __getitem__
def __getslice__

Public Attributes

 index

Static Public Attributes

tuple security = ClassSecurityInfo()

Private Attributes

 _keys
 _i18n_msgids
 _values
 _itor

Detailed Description

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

Definition at line 272 of file utils.py.


Constructor & Destructor Documentation

def Archetypes.utils.DisplayList.__init__ (   self,
  data = None 
)

Definition at line 336 of file utils.py.

00336 
00337     def __init__(self, data=None):
00338         self._keys = {}
00339         self._i18n_msgids = {}
00340         self._values = {}
00341         self._itor   = []
00342         self.index = 0
00343         if data:
00344             self.fromList(data)


Member Function Documentation

def Archetypes.utils.DisplayList.__add__ (   self,
  other 
)

Definition at line 363 of file utils.py.

00363 
00364     def __add__(self, other):
00365         a = tuple(self.items())
00366         if hasattr(other, 'items'):
00367             b = other.items()
00368         else: #assume a seq
00369             b = tuple(zip(other, other))
00370 
00371         msgids = self._i18n_msgids
00372         msgids.update(getattr(other, '_i18n_msgids', {}))
00373 
00374         v = DisplayList(a + b)
00375         v._i18n_msgids = msgids
00376         return v

Here is the call graph for this function:

Definition at line 351 of file utils.py.

00351 
00352     def __call__(self):
00353         return self

Here is the caller graph for this function:

def Archetypes.utils.DisplayList.__cmp__ (   self,
  dest 
)

Definition at line 475 of file utils.py.

00475 
00476     def __cmp__(self, dest):
00477         if not isinstance(dest, DisplayList):
00478             raise TypeError, 'Cant compare DisplayList to %s' % (type(dest))
00479 
00480         return cmp(self.sortedByKey()[:], dest.sortedByKey()[:])

Here is the call graph for this function:

def Archetypes.utils.DisplayList.__getitem__ (   self,
  key 
)

Definition at line 481 of file utils.py.

00481 
00482     def __getitem__(self, key):
00483         #Ok, this is going to pass a number
00484         #which is index but not easy to get at
00485         #with the data-struct, fix when we get real
00486         #itor/generators
00487         return self._itor[key]

Here is the caller graph for this function:

def Archetypes.utils.DisplayList.__getslice__ (   self,
  i1,
  i2 
)

Definition at line 488 of file utils.py.

00488 
00489     def __getslice__(self,i1,i2):
00490         r=[]
00491         for i in xrange(i1,i2):
00492             try: r.append((self._itor[i], self.getValue(self._itor[i]),))
00493             except IndexError: return r
00494         return DisplayList(r)

Here is the call graph for this function:

Definition at line 360 of file utils.py.

00360 
00361     def __len__(self):
00362         return self.index

Definition at line 345 of file utils.py.

00345 
00346     def __repr__(self):
00347         return '<DisplayList %s at %s>' % (self[:], id(self))

Definition at line 348 of file utils.py.

00348 
00349     def __str__(self):
00350         return str(self[:])

def Archetypes.utils.DisplayList.add (   self,
  key,
  value,
  msgid = None 
)

Reimplemented in Archetypes.utils.IntDisplayList.

Definition at line 380 of file utils.py.

00380 
00381     def add(self, key, value, msgid=None):
00382         if isinstance(key, int):
00383             deprecated('Using ints as DisplayList keys is deprecated (add)')
00384         if not isinstance(key, basestring) and not isinstance(key, int):
00385             raise TypeError('DisplayList keys must be strings or ints, got %s' %
00386                             type(key))
00387         if not isinstance(value, basestring) and not isinstance(value, int):
00388             raise TypeError('DisplayList values must be strings or ints, got %s' %
00389                             type(value))
00390         if msgid is not None:
00391             deprecated('Using explicit msgids for DisplayLists is deprecated. '
00392                         'Store Zope3 Messages as values directly.')
00393             if not isinstance(msgid, basestring):
00394                 raise TypeError('DisplayList msg ids must be strings, got %s' %
00395                                 type(msgid))
00396         self.index +=1
00397         k = (self.index, key)
00398         v = (self.index, value)
00399 
00400         self._keys[key] = v
00401         self._values[value] = k
00402         self._itor.append(key)
00403         if msgid is not None:
00404             self._i18n_msgids[key] = msgid

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.utils.DisplayList.fromList (   self,
  lst 
)

Definition at line 354 of file utils.py.

00354 
00355     def fromList(self, lst):
00356         for item in lst:
00357             if isinstance(item, list):
00358                 item = tuple(item)
00359             self.add(*item)

Here is the call graph for this function:

def Archetypes.utils.DisplayList.getKey (   self,
  value,
  default = None 
)
get key

Definition at line 405 of file utils.py.

00405 
00406     def getKey(self, value, default=None):
00407         """get key"""
00408         v = self._values.get(value, None)
00409         if v: return v[1]
00410         for k, v in self._values.items():
00411             if repr(value) == repr(k):
00412                 return v[1]
00413         return default

def Archetypes.utils.DisplayList.getMsgId (   self,
  key 
)

Reimplemented in Archetypes.utils.IntDisplayList.

Definition at line 428 of file utils.py.

00428 
00429     def getMsgId(self, key):
00430         "get i18n msgid"
00431         deprecated('DisplayList getMsgId is deprecated. Store Zope3 Messages '
00432                    'as values instead.')
00433         if isinstance(key, int):
00434             deprecated('Using ints as DisplayList keys is deprecated (msgid)')
00435         if not isinstance(key, basestring) and not isinstance(key, int):
00436             raise TypeError('DisplayList keys must be strings or ints, got %s' %
00437                             type(key))
00438         if self._i18n_msgids.has_key(key):
00439             return self._i18n_msgids[key]
00440         else:
00441             return self._keys[key][1]

Here is the call graph for this function:

def Archetypes.utils.DisplayList.getValue (   self,
  key,
  default = None 
)

Reimplemented in Archetypes.utils.Vocabulary, and Archetypes.utils.IntDisplayList.

Definition at line 414 of file utils.py.

00414 
00415     def getValue(self, key, default=None):
00416         "get value"
00417         if isinstance(key, int):
00418             deprecated('Using ints as DisplayList keys is deprecated (getValue)')
00419         if not isinstance(key, basestring) and not isinstance(key, int):
00420             raise TypeError('DisplayList keys must be strings or ints, got %s' %
00421                             type(key))
00422         v = self._keys.get(key, None)
00423         if v: return v[1]
00424         for k, v in self._keys.items():
00425             if repr(key) == repr(k):
00426                 return v[1]
00427         return default

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.utils.DisplayList.index_sort (   self,
  a,
  b 
)

Definition at line 377 of file utils.py.

00377 
00378     def index_sort(self, a, b):
00379         return  a[0] - b[0]

Here is the caller graph for this function:

items

Definition at line 454 of file utils.py.

00454 
00455     def items(self):
00456         """items"""
00457         keys = self.keys()
00458         return tuple([(key, self.getValue(key)) for key in keys])

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 442 of file utils.py.

00442 
00443     def keys(self):
00444         "keys"
00445         kl = self._values.values()
00446         kl.sort(self.index_sort)
00447         return [k[1] for k in kl]

Here is the call graph for this function:

Here is the caller graph for this function:

return a new display list sorted by key

Definition at line 467 of file utils.py.

00467 
00468     def sortedByKey(self):
00469         """return a new display list sorted by key"""
00470         def _cmp(a, b):
00471             return cmp(a[0], b[0])
00472         values = list(self.items())
00473         values.sort(_cmp)
00474         return DisplayList(values)

Here is the call graph for this function:

Here is the caller graph for this function:

return a new display list sorted by value

Definition at line 459 of file utils.py.

00459 
00460     def sortedByValue(self):
00461         """return a new display list sorted by value"""
00462         def _cmp(a, b):
00463             return cmp(a[1], b[1])
00464         values = list(self.items())
00465         values.sort(_cmp)
00466         return DisplayList(values)

Here is the call graph for this function:

Definition at line 448 of file utils.py.

00448 
00449     def values(self):
00450         "values"
00451         vl = self._keys.values()
00452         vl.sort(self.index_sort)
00453         return [v[1] for v in vl]

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Reimplemented in Archetypes.utils.Vocabulary.

Definition at line 338 of file utils.py.

Reimplemented in Archetypes.utils.Vocabulary.

Definition at line 340 of file utils.py.

Reimplemented in Archetypes.utils.Vocabulary.

Definition at line 337 of file utils.py.

Reimplemented in Archetypes.utils.Vocabulary.

Definition at line 339 of file utils.py.

Reimplemented in Archetypes.utils.Vocabulary.

Definition at line 341 of file utils.py.

tuple Archetypes.utils.DisplayList.security = ClassSecurityInfo() [static]

Reimplemented in Archetypes.utils.Vocabulary, and Archetypes.utils.IntDisplayList.

Definition at line 333 of file utils.py.


The documentation for this class was generated from the following file: