@@ -56,12 +56,46 @@ def _set_cache_(self, attr):
5656 pass
5757
5858
59+ class IterableList (list ):
60+ """
61+ List of iterable objects allowing to query an object by id or by named index::
62+
63+ heads = repo.heads
64+ heads.master
65+ heads['master']
66+ heads[0]
67+ """
68+ __slots__ = '_id_attr'
69+
70+ def __new__ (cls , id_attr ):
71+ return super (IterableList ,cls ).__new__ (cls )
72+
73+ def __init__ (self , id_attr ):
74+ self ._id_attr = id_attr
75+
76+ def __getattr__ (self , attr ):
77+ for item in self :
78+ if getattr (item , self ._id_attr ) == attr :
79+ return item
80+ # END for each item
81+ return list .__getattribute__ (self , attr )
82+
83+ def __getitem__ (self , index ):
84+ if isinstance (index , int ):
85+ return list .__getitem__ (self ,index )
86+
87+ try :
88+ return getattr (self , index )
89+ except AttributeError :
90+ raise IndexError ( "No item found with id %r" % index )
91+
5992class Iterable (object ):
6093 """
6194 Defines an interface for iterable items which is to assure a uniform
6295 way to retrieve and iterate items within the git repository
6396 """
6497 __slots__ = tuple ()
98+ _id_attribute_ = "attribute that most suitably identifies your instance"
6599
66100 @classmethod
67101 def list_items (cls , repo , * args , ** kwargs ):
@@ -75,7 +109,10 @@ def list_items(cls, repo, *args, **kwargs):
75109 Returns:
76110 list(Item,...) list of item instances
77111 """
78- return list (cls .iter_items (repo , * args , ** kwargs ))
112+ #return list(cls.iter_items(repo, *args, **kwargs))
113+ out_list = IterableList ( cls ._id_attribute_ )
114+ out_list .extend (cls .iter_items (repo , * args , ** kwargs ))
115+ return out_list
79116
80117
81118 @classmethod
0 commit comments