Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb7071ce

Browse files
committed
util: Added test for iterable list, and implemented __contains__ and __del__ functionality
1 parentfcc166d commitb7071ce

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

‎doc/source/changes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Changelog
1111
* **Blob** Type
1212

1313
* Added mode constants to ease the manual creation of blobs
14+
15+
* **IterableList**
16+
17+
* Added __contains__ and __delitem__ methods
1418

1519
* **More Changes**
1620

‎git/test/test_util.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,24 @@
77
importos
88
importtempfile
99

10-
fromgit.test.libimport*
1110
fromgit.utilimport*
11+
fromgit.test.libimport*
1212
fromgit.objects.utilimport*
1313
fromgitimport*
1414
fromgit.cmdimportdashify
1515

1616
importtime
1717

1818

19+
classTestIterableMember(object):
20+
"""A member of an iterable list"""
21+
__slots__= ("name","prefix_name")
22+
23+
def__init__(self,name):
24+
self.name=name
25+
self.prefix_name=name
26+
27+
1928
classTestUtils(TestBase):
2029
defsetup(self):
2130
self.testdict= {
@@ -107,3 +116,51 @@ def test_actor(self):
107116
assertisinstance(Actor.committer(cr),Actor)
108117
assertisinstance(Actor.author(cr),Actor)
109118
#END assure config reader is handled
119+
120+
deftest_iterable_list(self):
121+
forargsin (('name',), ('name','prefix_')):
122+
l=IterableList('name')
123+
124+
m1=TestIterableMember('one')
125+
m2=TestIterableMember('two')
126+
127+
l.extend((m1,m2))
128+
129+
assertlen(l)==2
130+
131+
# contains works with name and identity
132+
assertm1.nameinl
133+
assertm2.nameinl
134+
assertm2inl
135+
assertm2inl
136+
assert'invalid'notinl
137+
138+
# with string index
139+
assertl[m1.name]ism1
140+
assertl[m2.name]ism2
141+
142+
# with int index
143+
assertl[0]ism1
144+
assertl[1]ism2
145+
146+
# with getattr
147+
assertl.oneism1
148+
assertl.twoism2
149+
150+
# test exceptions
151+
self.failUnlessRaises(AttributeError,getattr,l,'something')
152+
self.failUnlessRaises(IndexError,l.__getitem__,'something')
153+
154+
# delete by name and index
155+
self.failUnlessRaises(IndexError,l.__delitem__,'something')
156+
del(l[m2.name])
157+
assertlen(l)==1
158+
assertm2.namenotinlandm1.nameinl
159+
del(l[0])
160+
assertm1.namenotinl
161+
assertlen(l)==0
162+
163+
self.failUnlessRaises(IndexError,l.__delitem__,0)
164+
self.failUnlessRaises(IndexError,l.__delitem__,'something')
165+
#END for each possible mode
166+

‎git/util.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ def author(cls, config_reader=None):
330330
but defaults to the committer"""
331331
returncls._main_actor(cls.env_author_name,cls.env_author_email,config_reader)
332332

333-
334333
classStats(object):
335334
"""
336335
Represents stat information as presented by git at the end of a merge. It is
@@ -560,6 +559,21 @@ def __init__(self, id_attr, prefix=''):
560559
raiseValueError("First parameter must be a string identifying the name-property. Extend the list after initialization")
561560
# END help debugging !
562561

562+
def__contains__(self,attr):
563+
# first try identy match for performance
564+
rval=list.__contains__(self,attr)
565+
ifrval:
566+
returnrval
567+
#END handle match
568+
569+
# otherwise make a full name search
570+
try:
571+
getattr(self,attr)
572+
returnTrue
573+
except (AttributeError,TypeError):
574+
returnFalse
575+
#END handle membership
576+
563577
def__getattr__(self,attr):
564578
attr=self._prefix+attr
565579
foriteminself:
@@ -576,7 +590,25 @@ def __getitem__(self, index):
576590
returngetattr(self,index)
577591
exceptAttributeError:
578592
raiseIndexError("No item found with id %r"% (self._prefix+index) )
593+
# END handle getattr
579594

595+
def__delitem__(self,index):
596+
delindex=index
597+
ifnotisinstance(index,int):
598+
delindex=-1
599+
name=self._prefix+index
600+
fori,iteminenumerate(self):
601+
ifgetattr(item,self._id_attr)==name:
602+
delindex=i
603+
break
604+
#END search index
605+
#END for each item
606+
ifdelindex==-1:
607+
raiseIndexError("Item with name %s not found"%name)
608+
#END handle error
609+
#END get index to delete
610+
list.__delitem__(self,delindex)
611+
580612

581613
classIterable(object):
582614
"""Defines an interface for iterable items which is to assure a uniform

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp