minor fixes
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1614 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
041c305d29
commit
84432873de
2 changed files with 16 additions and 19 deletions
18
util/jeep.py
18
util/jeep.py
|
@ -41,7 +41,7 @@ class Jeep(object):
|
|||
|
||||
def __iter__(self):
|
||||
for key in self._sequence:
|
||||
yield key
|
||||
yield self[key]
|
||||
|
||||
def __setattr__(self, attr, value):
|
||||
if not attr in self._attributes:
|
||||
|
@ -71,13 +71,14 @@ class Jeep(object):
|
|||
return getattr(self, key, _notfound) is not _notfound
|
||||
|
||||
def keys(self):
|
||||
return list(self)
|
||||
return [key for key in self._sequence]
|
||||
|
||||
def values(self):
|
||||
return list(self)
|
||||
return [self[k] for k in self]
|
||||
|
||||
def items(self):
|
||||
return [(k, self[k]) for k in self]
|
||||
return [(k, self[k]) for k in self._sequence]
|
||||
|
||||
def get(self, key, default=None):
|
||||
return getattr(self, key, default)
|
||||
|
@ -86,17 +87,12 @@ class Jeep(object):
|
|||
return self._sequence.index(key)
|
||||
|
||||
def append(self, obj):
|
||||
key = getattr(obj, '__name__', _notfound)
|
||||
if key is _notfound:
|
||||
raise AttributeError("No '__name__' attribute present")
|
||||
if key in self:
|
||||
raise ValueError("Object already present")
|
||||
self[key] = obj
|
||||
self.insert(len(self), obj)
|
||||
|
||||
def insert(self, idx, obj):
|
||||
key = getattr(obj, '__name__', _notfound)
|
||||
key = getattr(obj, '__name__', getattr(obj, 'name', _notfound))
|
||||
if key is _notfound:
|
||||
raise AttributeError("No '__name__' attribute present")
|
||||
raise AttributeError("No name attribute present")
|
||||
if key in self:
|
||||
raise ValueError("Object already present")
|
||||
self._sequence.insert(idx, key)
|
||||
|
|
|
@ -17,11 +17,12 @@ In addition to the usual access via dot notation all attributes can be
|
|||
accessed via dictionary notation:
|
||||
|
||||
The third type of interface provided by Jeep objects is the sequence or
|
||||
iterator interface. Converting a jeep object to a list iterates over the
|
||||
keys:
|
||||
iterator interface. Converting a jeep object to a list iterates over its
|
||||
values (that is different from the dictionary behaviour, but is what
|
||||
you want usually; use the ``.keys()`` method to get at the keys, see below):
|
||||
|
||||
>>> list(jeep)
|
||||
['first', 'second']
|
||||
['first value', 'second value']
|
||||
|
||||
Direct index access to certain entries gives the corresponding value,
|
||||
not the key:
|
||||
|
@ -40,14 +41,14 @@ attribute:
|
|||
'third value'
|
||||
|
||||
>>> list(jeep)
|
||||
['first', 'second', 'third']
|
||||
['first value', 'second value', 'third value']
|
||||
|
||||
Assigning a new value to an already existing attribute does not change the
|
||||
order but only changes the attribute's value
|
||||
|
||||
>>> jeep.second = 'new second value'
|
||||
>>> list(jeep)
|
||||
['first', 'second', 'third']
|
||||
['first value', 'new second value', 'third value']
|
||||
>>> jeep[1]
|
||||
'new second value'
|
||||
|
||||
|
@ -112,13 +113,13 @@ to a Jeep object as the dictionary key can be obtained from these attribute.
|
|||
|
||||
>>> t1 = Term('term1', 'title 1')
|
||||
>>> jeep.append(t1)
|
||||
>>> list(jeep)
|
||||
>>> jeep.keys()
|
||||
['first', 'second', 'term1']
|
||||
>>> jeep.term1.title
|
||||
'title 1'
|
||||
|
||||
>>> jeep.insert(1, Term('term2', 'title 2'))
|
||||
>>> list(jeep)
|
||||
>>> jeep.keys()
|
||||
['first', 'term2', 'second', 'term1']
|
||||
>>> jeep[1].title
|
||||
'title 2'
|
||||
|
@ -136,5 +137,5 @@ Constructors
|
|||
|
||||
>>> jeep2 = Jeep((('f', '1st'), ('s', '2nd'), ('t', '3rd')))
|
||||
>>> list(jeep2)
|
||||
['f', 's', 't']
|
||||
['1st', '2nd', '3rd']
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue