Package topo :: Package misc :: Module keyedlist :: Class KeyedList
[hide private]
[frames] | no frames]

Class KeyedList

source code


Extends the built-in type 'list' to support dictionary-like access using []. The internal representation is as an ordinary list of (key,value) pairs, not a hash table like an ordinary dictionary, so that the elements will remain ordered.

Note: Not all list operations will work as expected, because [] does not return the name tuple.

Redefined functions:

__getitem__ ([,])
__setitem__ ([,])
append  --  Now takes a tuple (key,value) so that value
            can be later accessed by [key].

New functions modeled from dictionaries:

get
set
has_key
keys
items
update

Key values are not allowed to be None, because None is a default return value for get() when there is no object by that name.

Instance Methods [hide private]
 
__getitem__(self, key)
The bracket [] accessor.
source code
 
__setitem__(self, k, v)
The bracket [] mutator.
source code
 
append(self, (key, value))
Append a new object to the end of the existing list.
source code
 
get(self, key, default=None)
Get the value with the specified key.
source code
 
set(self, key, value)
If the key already exists in the list, change the entry.
source code
 
has_key(self, key)
Return True iff key is found in the ordered list.
source code
 
items(self)
Provide the item function supported by dictionaries.
source code
 
keys(self)
A copy of the list of keys.
source code
 
values(self)
A copy of the list of values.
source code
 
update(self, b)
Updates (and overwrites) key/value pairs from b.
source code

Inherited from list: __add__, __contains__, __delitem__, __delslice__, __eq__, __ge__, __getattribute__, __getslice__, __gt__, __iadd__, __imul__, __init__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __repr__, __reversed__, __rmul__, __setslice__, __sizeof__, count, extend, index, insert, pop, remove, reverse, sort

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __str__, __subclasshook__

Class Variables [hide private]

Inherited from list: __hash__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__getitem__(self, key)
(Indexing operator)

source code 
The bracket [] accessor.
Overrides: list.__getitem__

__setitem__(self, k, v)
(Index assignment operator)

source code 
The bracket [] mutator. Overwrite value if key already exists, otherwise append.
Overrides: list.__setitem__

append(self, (key, value))

source code 

Append a new object to the end of the existing list.

Accepts a 2-tuple (key, value).

Strictly speaking, this operation did not need to be redefined in this subclass, but by forcing the tuple in the function parameters, we may be able to catch an erroneous assignment.

Overrides: list.append

get(self, key, default=None)

source code 

Get the value with the specified key.

Returns None if no value with that key exists.

set(self, key, value)

source code 
If the key already exists in the list, change the entry. Otherwise append the new (key,value) to the end of the list.

items(self)

source code 
Provide the item function supported by dictionaries. A keyed list already is stored in this format, so just returns the actual underlying list.