Back to index

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

List of all members.

Public Member Functions

def add
def getValue
def getMsgId
def __repr__
def __str__
def __call__
def fromList
def __len__
def __add__
def index_sort
def getKey
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()

Detailed Description

Static display lists for integer keys, can look up on
either side of the dict, and get them in sorted order

The IntDisplayList can be used with integer values only. You should use it
in favor of a DisplayList if you want to use ints as keys. The support for
ints as keys for the ordinary DisplayList will be dropped in the next
release.

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.

>>> idl = IntDisplayList()

Add some keys
>>> idl.add(1, 'number one')
>>> idl.add(2, 'just the second')

Assert some values
>>> idl.index
2
>>> idl.keys()
[1, 2]
>>> idl.values()
['number one', 'just the second']
>>> idl.items()
((1, 'number one'), (2, 'just the second'))

You can use only ints as keys
>>> idl.add(object(), 'error')
Traceback (most recent call last):
TypeError: DisplayList keys must be ints, got <type 'object'>

>>> idl.add(42, object())
Traceback (most recent call last):
TypeError: DisplayList values must be strings or ints, got <type 'object'>

>>> idl.add('stringkey', 'error')
Traceback (most recent call last):
TypeError: DisplayList keys must be ints, got <type 'str'>

>>> idl.add(u'unicodekey', 'error')
Traceback (most recent call last):
TypeError: DisplayList keys must be ints, got <type 'unicode'>

GOTCHA
Adding a value a second time does overwrite the key, too!
>>> idl.add(3 , 'just the second')
>>> idl.keys()
[1, 3]
>>> idl.items()
((1, 'number one'), (3, 'just the second'))

It is possible to get the value also by a stringified int
>>> idl.getValue("1")
'number one'
>>> idl.getValue(u"1")
'number one'

Definition at line 499 of file utils.py.


Member Function Documentation

def Archetypes.utils.DisplayList.__add__ (   self,
  other 
) [inherited]

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:

def Archetypes.utils.DisplayList.__call__ (   self) [inherited]

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 
) [inherited]

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 
) [inherited]

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 
) [inherited]

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:

def Archetypes.utils.DisplayList.__len__ (   self) [inherited]

Definition at line 360 of file utils.py.

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

def Archetypes.utils.DisplayList.__repr__ (   self) [inherited]

Definition at line 345 of file utils.py.

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

def Archetypes.utils.DisplayList.__str__ (   self) [inherited]

Definition at line 348 of file utils.py.

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

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

Reimplemented from Archetypes.utils.DisplayList.

Definition at line 563 of file utils.py.

00563 
00564     def add(self, key, value, msgid=None):
00565         if not isinstance(key, int):
00566             raise TypeError('DisplayList keys must be ints, got %s' %
00567                             type(key))
00568         if not isinstance(value, basestring) and not isinstance(value, int):
00569             raise TypeError('DisplayList values must be strings or ints, got %s' %
00570                             type(value))
00571         if msgid is not None:
00572             deprecated('Using explicit msgids for IntDisplayLists is deprecated. '
00573                         'Store Zope3 Messages as values directly.')
00574             if not isinstance(msgid, basestring):
00575                 raise TypeError('DisplayList msg ids must be strings, got %s' %
00576                                 type(msgid))
00577         self.index +=1
00578         k = (self.index, key)
00579         v = (self.index, value)
00580 
00581         self._keys[key] = v
00582         self._values[value] = k
00583         self._itor.append(key)
00584         if msgid is not None:
00585             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 
) [inherited]

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 
) [inherited]
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.IntDisplayList.getMsgId (   self,
  key 
)

Reimplemented from Archetypes.utils.DisplayList.

Definition at line 601 of file utils.py.

00601 
00602     def getMsgId(self, key):
00603         "get i18n msgid"
00604         deprecated('IntDisplayList getMsgId is deprecated. Store Zope3 Messages'
00605                    ' as values instead.')
00606         if isinstance(key, basestring):
00607             key = int(key)
00608         elif isinstance(key, int):
00609             pass
00610         else:
00611             raise TypeError("Key must be string or int")
00612         if self._i18n_msgids.has_key(key):
00613             return self._i18n_msgids[key]
00614         else:
00615             return self._keys[key][1]

Here is the call graph for this function:

def Archetypes.utils.IntDisplayList.getValue (   self,
  key,
  default = None 
)
get value

Reimplemented from Archetypes.utils.DisplayList.

Definition at line 586 of file utils.py.

00586 
00587     def getValue(self, key, default=None):
00588         """get value"""
00589         if isinstance(key, basestring):
00590             key = int(key)
00591         elif isinstance(key, int):
00592             pass
00593         else:
00594             raise TypeError("Key must be string or int")
00595         v = self._keys.get(key, None)
00596         if v: return v[1]
00597         for k, v in self._keys.items():
00598             if repr(key) == repr(k):
00599                 return v[1]
00600         return default

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

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:

def Archetypes.utils.DisplayList.items (   self) [inherited]
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:

def Archetypes.utils.DisplayList.keys (   self) [inherited]

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:

def Archetypes.utils.DisplayList.sortedByKey (   self) [inherited]
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:

def Archetypes.utils.DisplayList.sortedByValue (   self) [inherited]
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:

def Archetypes.utils.DisplayList.values (   self) [inherited]

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 341 of file utils.py.

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

Reimplemented from Archetypes.utils.DisplayList.

Definition at line 560 of file utils.py.


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