cybertools/util/jeep.txt
helmutm bc83147047 added jeep module, a general purpose class for objects acting like a list, a dictionary and a normal object with attributes
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1606 fd906abe-77d9-0310-91a1-e0d9ade77398
2007-03-02 18:58:44 +00:00

89 lines
1.9 KiB
Text

==============================
Jeep - a General Purpose Class
==============================
$Id$
>>> from cybertools.util.jeep import Jeep
>>> jeep = Jeep()
>>> jeep.first = 'first value'
>>> jeep.second = 'second value'
>>> jeep.first
'first value'
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:
>>> list(jeep)
['first', 'second']
Direct index access to certain entries gives the corresponding value,
not the key:
>>> jeep[1]
'second value'
Changing Jeep Objects
---------------------
Assignment by dictionary or attribute access appends the newly assigned
attribute:
>>> jeep['third'] = 'third value'
>>> jeep.third
'third value'
>>> list(jeep)
['first', 'second', 'third']
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']
>>> jeep[1]
'new second value'
More Dictionary Methods
-----------------------
>>> jeep.keys()
['first', 'second', 'third']
>>> jeep.values()
['first value', 'new second value', 'third value']
>>> jeep.items()
[('first', 'first value'), ('second', 'new second value'), ('third', 'third value')]
>>> jeep.get('second')
'new second value'
>>> jeep.get('fourth', 'default')
'default'
>>> jeep.get('fourth') is None
True
>>> jeep['fourth']
Traceback (most recent call last):
...
KeyError: 'fourth'
>>> dict(jeep)
{'second': 'new second value', 'third': 'third value', 'first': 'first value'}
Constructors
------------
>>> jeep2 = Jeep((('f', '1st'), ('s', '2nd'), ('t', '3rd')))
>>> list(jeep2)
['f', 's', 't']