Commits

Lynn Rees committed df6367d

- cleanup
- more tests

Comments (0)

Files changed (7)

twoq/active/contexts.py

         self._outclear = queue._outclear
         self._incoming = queue.incoming
 
-    def __exit__(self, t, v, e):
-        pass
-
     def __call__(self, args):
         self._outextend(args)
 
         self._outappend(args)
 
 
-class ScratchContext(Context):
+class ManContext(Context):
 
     '''manual sync context manager'''
 
 
         @param queue: queue
         '''
-        super(ScratchContext, self).__init__(queue)
+        super(ManContext, self).__init__(queue)
         self._sxtend = queue._sxtend
         self._sappend = queue._sappend
         self._sclear = queue._sclear
         return self._scratch
 
 
-class AutoContext(ScratchContext):
+class AutoContext(ManContext):
 
     '''auto sync context manager'''
 
         self._inextend(self._outgoing)
 
 
-class ManContext(ScratchContext):
-
-    '''manual sync context manager'''
-
-    def __exit__(self, t, v, e):
-        # clear scratch queue
-        self._sclear()
-
-
 class SyncContext(Context):
 
     '''sync context manager'''

twoq/active/mixins.py

 
     _oextendleft = extendleft
 
-    def reverse(self, _reversed=None):
-        '''iterate over reversed incoming things, clearing as it goes'''
-        self.outgoing.extendleft(self.incoming)
-        self._inclear()
-        self._inextend(self.outgoing)
-        return self
-
-    _screverse = reverse
-
     ###########################################################################
     ## balance queues #########################################################
     ###########################################################################

twoq/mixins/mapping.py

 from operator import methodcaller as mc
 
 from twoq import support as ct
+from twoq.support import port
 
 __all__ = (
     'DelayMixin', 'CopyMixin', 'MappingMixin', 'RepeatMixin', 'MapMixin',
 )
+chain_iter = it.chain.from_iterable
 
 ###############################################################################
 ## mapping subroutines ########################################################
         return self
     
     _oinvoke = invoke
+    
+    def items(self, _s=it.starmap, _c=chain_iter, _m=ct.map, _i=port.items):
+        '''invoke call on each mapping to get key, value pairs'''
+        with self._sync as sync:
+            sync(_s(self._call, _c(_m(_i, sync.iterable))))
+        return self
+    
+    _ostarmap = items
 
     def map(self, _map=ct.map):
         '''invoke call on each incoming thing'''
         return self
     
     _omap = map
+    
+    def starmap(self, _map=it.starmap):
+        '''invoke call on each incoming pair of things'''
+        with self._sync as sync:
+            sync(_map(self._call, sync.iterable))
+        return self
+    
+    _ostarmap = starmap
 
 
 class RepeatMixin(local):

twoq/tests/active/auto/mixins/mapping.py

         self.assertEquals(
             self.qclass(1, 2, 3).tap(lambda x: x * 3).map().value(), [3, 6, 9],
         )
+        
+    def test_starmap(self):
+        self.assertEquals(
+            self.qclass(
+                (1, 2), (2, 3), (3, 4)
+            ).tap(lambda x, y: x * y).starmap().value(), [2, 6, 12],
+        )
+
+    def test_items(self):
+        self.assertEquals(
+            self.qclass(
+                dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)])
+            ).tap(lambda x, y: x * y).items().value(), [2, 6, 12, 2, 6, 12],
+        )
 
     def test_invoke(self):
         self.assertEquals(

twoq/tests/active/auto/mixins/ordering.py

             ).sort().value(),
             [5, 4, 6, 3, 1, 2],
         )
+        self.assertEqual(
+            self.qclass(4, 6, 65, 3, 63, 2,  4).sort().value(),
+            [2, 3, 4, 4, 6, 63, 65],
+        )
 
 
 class ARandomQMixin(object):

twoq/tests/active/man/mixins/mapping.py

         self.assertEquals(manq.value(), [3, 6, 9])
         self.assertFalse(manq.balanced)
 
+    def test_starmap(self):
+        manq = self.qclass(
+            (1, 2), (2, 3), (3, 4)
+        ).tap(lambda x, y: x * y).starmap()
+        self.assertTrue(manq.balanced)
+        manq.sync()
+        self.assertTrue(manq.balanced)
+        self.assertEquals(manq.value(), [2, 6, 12])
+        self.assertFalse(manq.balanced)
+
+    def test_items(self):
+        manq = self.qclass(
+            dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)])
+        ).tap(lambda x, y: x * y).items()
+        self.assertFalse(manq.balanced)
+        manq.sync()
+        self.assertTrue(manq.balanced)
+        self.assertEquals(manq.final(), [2, 6, 12, 2, 6, 12])
+        self.assertTrue(manq.balanced)
+
     def test_invoke(self):
         manq = self.qclass([5, 1, 7], [3, 2, 1]).args(1).invoke('index')
         self.assertTrue(manq.balanced)
         self.assertTrue(manq.balanced)
         self.assertEquals(manq.value(), [[1, 5, 7], [1, 2, 3]])
         self.assertFalse(manq.balanced)
+        manq.clear()
+        self.assertTrue(manq.balanced)
 
 
 class MCopyQMixin(object):

twoq/tests/active/man/mixins/ordering.py

         self.assertTrue(manq.balanced)
         self.assertEqual(manq.value(), [5, 4, 6, 3, 1, 2])
         self.assertFalse(manq.balanced)
-
+        manq = self.qclass(4, 6, 65, 3, 63, 2,  4).sort()
+        self.assertTrue(manq.balanced)
+        manq.sync()
+        self.assertTrue(manq.balanced)
+        self.assertEqual(
+            manq.value(),
+            [2, 3, 4, 4, 6, 63, 65],
+        )
+        self.assertFalse(manq.balanced)
+        self.assertEqual(manq.outcount(), 0)
 
 class MRandomQMixin(object):