diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2011-06-08 10:43:47 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2011-06-08 11:01:37 +0200 |
commit | ee9d6b97ad7d7bf6761161dc411258f2aa86d8c0 (patch) | |
tree | 24ca6faef1d8983dd43c3c6d54ba344025fef256 /git/util.py | |
parent | 4772fe04c38d2d0bd75d6a0f9d5905ee3ee08ae6 (diff) | |
download | gitpython-ee9d6b97ad7d7bf6761161dc411258f2aa86d8c0.tar.gz |
util: Added test for iterable list, and implemented __contains__ and __del__ functionality
Diffstat (limited to 'git/util.py')
-rw-r--r-- | git/util.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/git/util.py b/git/util.py index d248b1ee..01940763 100644 --- a/git/util.py +++ b/git/util.py @@ -734,6 +734,21 @@ class IterableList(list): raise ValueError("First parameter must be a string identifying the name-property. Extend the list after initialization") # END help debugging ! + def __contains__(self, attr): + # first try identy match for performance + rval = list.__contains__(self, attr) + if rval: + return rval + #END handle match + + # otherwise make a full name search + try: + getattr(self, attr) + return True + except (AttributeError, TypeError): + return False + #END handle membership + def __getattr__(self, attr): attr = self._prefix + attr for item in self: @@ -750,7 +765,24 @@ class IterableList(list): return getattr(self, index) except AttributeError: raise IndexError( "No item found with id %r" % (self._prefix + index) ) + # END handle getattr + def __delitem__(self, index): + delindex = index + if not isinstance(index, int): + delindex = -1 + name = self._prefix + index + for i, item in enumerate(self): + if getattr(item, self._id_attr) == name: + delindex = i + break + #END search index + #END for each item + if delindex == -1: + raise IndexError("Item with name %s not found" % name) + #END handle error + #END get index to delete + list.__delitem__(self, delindex) #} END utilities |