Andriy Kornatskyy avatar Andriy Kornatskyy committed ed8fc4d

Refactored last_item_adapter. Added first_item_adapter.

Comments (0)

Files changed (1)

src/wheezy/core/collections.py

 from wheezy.core.comp import defaultdict
 
 
-class LastItemAdapter(object):
-    """ Adapts ``defaultdict(list)``.__getitem__ accessor
-        to return a list item from the list. If key is not
+def first_item_adapter(adaptee):
+    """ Adapts ``defaultdict(list)``.__getitem__ accessor to
+        return the first item from the list.
+
+        >>> d = defaultdict(list)
+        >>> d['a'].extend([1, 2, 3])
+        >>> a = first_item_adapter(d)
+
+        Return a first item from the list.
+
+        >>> a['a']
+        1
+    """
+    return ItemAdapter(adaptee, 0)
+
+
+def last_item_adapter(adaptee):
+    """ Adapts ``defaultdict(list)``.__getitem__ accessor to
+        return the last item from the list.
+
+        >>> d = defaultdict(list)
+        >>> d['a'].extend([1, 2, 3])
+        >>> a = last_item_adapter(d)
+
+        Return a last item from the list.
+
+        >>> a['a']
+        3
+    """
+    return ItemAdapter(adaptee, -1)
+
+
+class ItemAdapter(object):
+    """ Adapts ``defaultdict(list)``.__getitem__ accessor to
+        return item at ``index`` from the list. If ``key`` is not
         found return None.
     """
 
-    def __init__(self, adaptee):
+    def __init__(self, adaptee, index):
         """ ``adaptee`` must be defaultdict(list).
 
             >>> d = defaultdict(list)
-            >>> a = last_item_adapter(d)
+            >>> a = ItemAdapter(d, 0)
 
             Otherwise raise ``TypeError``.
 
-            >>> last_item_adapter(None) # doctest: +ELLIPSIS
+            >>> ItemAdapter(None, 0) # doctest: +ELLIPSIS
             Traceback (most recent call last):
                 ...
             TypeError: ...
          """
-        if adaptee is None or not(isinstance(adaptee, defaultdict) and
-                adaptee.default_factory is list):
+        if adaptee is None or not isinstance(adaptee, dict):
             raise TypeError('first argument must be defaultdict(list)')
         self.adaptee = adaptee
+        self.index = index
 
     def __getitem__(self, key):
         """
             >>> d = defaultdict(list)
             >>> d['a'].extend([1, 2, 3])
-            >>> a = last_item_adapter(d)
+            >>> a = ItemAdapter(d, 0)
+
+            Return a first item from the list.
+
+            >>> a['a']
+            1
+
+            >>> a = ItemAdapter(d, -1)
 
             Return a last item from the list.
 
         """
         l = self.adaptee[key]
         if l:
-            return l[-1]
+            return l[self.index]
         return None
-
-last_item_adapter = LastItemAdapter
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.