Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An indexed list class:</p> <pre><code>class IndexedList(list): def __init__(self, items, attrs): super(IndexedList,self).__init__(items) # do indexing self._attrs = tuple(attrs) self._index = {} _add = self._addindex for obj in self: _add(obj) def _addindex(self, obj): _idx = self._index for attr in self._attrs: _idx[getattr(obj, attr)] = obj def _delindex(self, obj): _idx = self._index for attr in self._attrs: try: del _idx[getattr(obj,attr)] except KeyError: pass def __delitem__(self, ind): try: obj = list.__getitem__(self, ind) except (IndexError, TypeError): obj = self._index[ind] ind = list.index(self, obj) self._delindex(obj) return list.__delitem__(self, ind) def __delslice__(self, i, j): for ind in xrange(i,j): self.__delitem__(ind) def __getitem__(self, ind): try: return self._index[ind] except KeyError: return list.__getitem__(self, ind) def __getslice__(self, i, j): return IndexedList(list.__getslice__(self, i, j)) def __setitem__(self, ind, new_obj): try: obj = list.__getitem__(self, ind) except (IndexError, TypeError): obj = self._index[ind] ind = list.index(self, obj) self._delindex(obj) self._addindex(new_obj) return list.__setitem__(ind, new_obj) def __setslice__(self, i, j, newItems): _get = self.__getitem__ _add = self._addindex _del = self._delindex newItems = list(newItems) # remove indexing of items to remove for ind in xrange(i,j): _del(_get(ind)) # add new indexing if isinstance(newList, IndexedList): self._index.update(newList._index) else: for obj in newList: _add(obj) # replace items return list.__setslice__(self, i, j, newList) def append(self, obj): self._addindex(obj) return list.append(self, obj) def extend(self, newList): newList = list(newList) if isinstance(newList, IndexedList): self._index.update(newList._index) else: _add = self._addindex for obj in newList: _add(obj) return list.extend(self, newList) def insert(self, ind, new_obj): # ensure that ind is a numeric index try: obj = list.__getitem__(self, ind) except (IndexError, TypeError): obj = self._index[ind] ind = list.index(self, obj) self._addindex(new_obj) return list.insert(self, ind, new_obj) def pop(self, ind=-1): # ensure that ind is a numeric index try: obj = list.__getitem__(self, ind) except (IndexError, TypeError): obj = self._index[ind] ind = list.index(self, obj) self._delindex(obj) return list.pop(self, ind) def remove(self, ind_or_obj): try: obj = self._index[ind_or_obj] ind = list.index(self, obj) except KeyError: ind = list.index(self, ind_or_obj) obj = list.__getitem__(self, ind) self._delindex(obj) return list.remove(self, ind) </code></pre> <p>which can be used as:</p> <pre><code>class Child(object): def __init__(self, id, name): self.id = id self.name = name class Teacher(object): def __init__(self, id, name): self.id = id self.name = name class Classroom(object): def __init__(self, children, teachers): self.children = IndexedList(children, ('id','name')) self.teachers = IndexedList(teachers, ('id','name')) classroom = Classroom([Child('389','pete')], [Teacher('829','bob')]) print classroom.children[0].name # -&gt; pete classroom.children.append(Child('2344','joe')) print len(classroom.children) # -&gt; 2 print classroom.children[1].name # -&gt; joe print classroom.children['joe'].id # -&gt; 2344 print classroom.children['2344'].name # -&gt; joe p = classroom.children.pop('pete') print p.name # -&gt; pete print len(classroom.children) # -&gt; 1 </code></pre> <p><strong>Edit:</strong> I had made a mistake in some of the exception-handling (catching KeyError instead of IndexError); it is fixed. I will add some unit-testing code. If you run into any further errors, please let me know!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload