Commits

Lynn Rees committed 4646df4

- fixes

  • Participants
  • Parent commits 7086040
  • Branches next

Comments (0)

Files changed (8)

File twoq/active/mixins.py

         try:
             incoming = deque(things[0]) if len(things) == 1 else deque(things)
         except TypeError:
-            incoming = deque([things])
+            incoming = deque(iter([things]))
         super(BaseMixin, self).__init__(incoming, deque())
         # set iterator
         self._iterator = self._iterexcept

File twoq/core.py

 from contextlib import contextmanager
 
 from stuf.utils import OrderedDict
+from stuf.six import binaries, texts, b, u
 from stuf.core import stuf, frozenstuf, orderedstuf
 
 from twoq.support import n2u, n2b
 
     def reswap(self):
         '''swap for preferred context'''
-        return self.swap(**self._CONFIG)
+        return self.swap(savepoint=False, **self._CONFIG)
 
     def unswap(self):
         '''swap for current default context'''
-        return self.swap()
+        return self.swap(savepoint=False)
 
     @contextmanager
     def ctx1(self, **kw):
 
     def reup(self):
         '''put incoming things in incoming things as one incoming thing'''
-        with self.ctx2():
+        with self.ctx2(savepoint=False):
             return self._append(list(self._iterable))
 
     def sync(self):
         '''shift outgoing things to incoming things'''
-        with self.autoctx(inq=self._OUTVAR, outq=self._INVAR):
+        with self.autoctx(inq=self._OUTVAR, outq=self._INVAR, savepoint=False):
             return self._xtend(self._iterable)
 
     def syncout(self):
         '''shift incoming things to outgoing things'''
-        with self.autoctx():
+        with self.autoctx(savepoint=False):
             return self._xtend(self._iterable)
 
     ###########################################################################
         '''set wrapper to `set`'''
         return self.wrap(set)
 
-    def byte_wrap(self, encoding='ISO-8859-1'):
+    def byte_wrap(self, encoding='ISO-8859-1', join=b('')):
         '''set wrapper to `bytes` with given `encoding`'''
-        return self.wrap(lambda x: n2b(x, encoding))
+        return self.wrap(lambda x: n2b(
+            join.join(binaries(i) for i in x), encoding)
+        )
 
     def deque_wrap(self):
         '''set wrapper to `deque`'''
         '''set wrapper to `stuf`'''
         return self.wrap(stuf)
 
-    def unicode_wrap(self, encoding='utf-8'):
+    def unicode_wrap(self, encoding='utf-8', join=u('')):
         '''set wrapper to `unicode` with given `encoding`'''
-        return self.wrap(lambda x: n2u(x, encoding))
+        return self.wrap(
+            lambda x: n2u(join.join(texts(i) for i in x), encoding)
+        )
 
     def list_wrap(self):
         '''clear current wrapper'''

File twoq/filtering.py

                         yield k, t(w(f, s, t, v))
                     else:
                         yield k, v
-        for member in ifilter(
-            truth, members(),
-        ):
+        for member in ifilter(truth, members()):
             yield member
 
     @staticmethod

File twoq/lazy/mixins.py

     '''base lazy things'''
 
     def __init__(self, *things, **kw):
-
-        try:
-            incoming = iter([things[0]]) if len(things) == 1 else iter(things)
-        except TypeError:
-            incoming = iter([things])
+        incoming = iter([things[0]]) if len(things) == 1 else iter(things)
         super(BaseMixin, self).__init__(incoming, iter([]))
         # work things
         self._work = iter([])
 
     def _append(self, things):
         '''append `things` to work things'''
-        return self.__buildchain(iter([things]))
+        UTILQ = self._UTILQ
+        setattr(self, UTILQ, chain(getattr(self, UTILQ), iter([things])))
+        return self
 
     def _appendleft(self, things):
         '''append `things` to left side of work things'''

File twoq/tests/auto/filtering.py

             value,
         )
 
+    def test_mro(self):
+        from inspect import isclass
+        class stooges: #@IgnorePep8
+            name = 'moe'
+            age = 40
+        class stoog2(stooges): #@IgnorePep8
+            name = 'larry'
+            age = 50
+        class stoog3(stoog2): #@IgnorePep8
+            name = 'curly'
+            age = 60
+        test = lambda x: not x[0].startswith('__')
+        value = self.qclass(
+            stoog3
+        ).tap(test, isclass).tuple_wrap().mro().members().untap().end(),
+        self.assertEqual(
+            value,
+            ((('age', 40), ('name', 'moe'), ('age', 50), ('name', 'larry'),
+            ('age', 60), ('name', 'curly'), ('stoog4', (('age', 969),
+            ('name', 'beastly')))),),
+            value,
+        )
+
     def test_pick(self):
         from stuf import stuf
         stooges = [

File twoq/tests/auto/queuing.py

         self.assertEqual(len(initial), 0)
 
     def test_extend(self):
-        self.assertEqual(
+        self.assertListEqual(
             self.qclass().extend([1, 2, 3, 4, 5, 6]).syncout().end(),
             [1, 2, 3, 4, 5, 6],
         )
 
     def test_outextend(self):
-        self.assertEqual(
+        self.assertListEqual(
             self.qclass().outextend([1, 2, 3, 4, 5, 6]).end(),
             [1, 2, 3, 4, 5, 6],
         )
     ## queue balancing ########################################################
     ###########################################################################
 
+    def test_undo(self):
+        queue = self.qclass(1, 2, 3).extendleft([1, 2, 3, 4, 5, 6]).syncout()
+        self.assertListEqual(queue.snapshot(), [6, 5, 4, 3, 2, 1, 1, 2, 3])
+        self.assertListEqual(queue.peek(), [6, 5, 4, 3, 2, 1, 1, 2, 3])
+        queue.append(1).undo().syncout()
+        self.assertListEqual(queue.snapshot(), [6, 5, 4, 3, 2, 1, 1, 2, 3])
+        queue.append(1).append(2).undo().syncout()
+        self.assertListEqual(queue.snapshot(), [6, 5, 4, 3, 2, 1, 1, 2, 3, 1])
+        queue.undo(everything=True).syncout()
+        self.assertListEqual(queue.syncout().end(), [1, 2, 3])
+
     def test_insync(self):
         q = self.qclass([1, 2, 3, 4, 5, 6]).syncout().clearin().sync()
         self.assertListEqual(list(q.incoming), list(q.outgoing))
             list(self.qclass(1, 2, 3, 4, 5, 6).syncout().results()),
             [1, 2, 3, 4, 5, 6],
         )
+
+    def test_unicode_wrap(self):
+        from stuf.six import texts, u
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).unicode_wrap().syncout().value(),
+            texts,
+        )
+        self.assertEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).unicode_wrap().syncout().value(),
+            u('123456'),
+        )
+
+    def test_byte_wrap(self):
+        from stuf.six import binaries, b
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).byte_wrap().syncout().value(),
+            binaries,
+        )
+        self.assertEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).byte_wrap().syncout().value(),
+            b('123456'),
+        )
+
+    def test_tuple_wrap(self):
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).tuple_wrap().syncout().value(),
+            tuple,
+        )
+        self.assertTupleEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).tuple_wrap().syncout().value(),
+            (1, 2, 3, 4, 5, 6),
+        )
+
+    def test_set_wrap(self):
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).set_wrap().syncout().value(),
+            set,
+        )
+        self.assertSetEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).set_wrap().syncout().value(),
+            set([1, 2, 3, 4, 5, 6]),
+        )
+
+    def test_deque_wrap(self):
+        from collections import deque
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).deque_wrap().syncout().value(),
+            deque,
+        )
+        self.assertEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).deque_wrap().syncout().value(),
+            deque([1, 2, 3, 4, 5, 6]),
+        )
+
+    def test_frozenset_wrap(self):
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).frozenset_wrap().syncout().value(),
+            frozenset,
+        )
+        self.assertSetEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).frozenset_wrap().syncout().value(),
+            frozenset([1, 2, 3, 4, 5, 6]),
+        )
+
+    def test_dict_wrap(self):
+        self.assertIsInstance(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).dict_wrap().syncout().value(),
+            dict,
+        )
+        self.assertDictEqual(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).dict_wrap().syncout().value(),
+            {1: 2, 3: 4, 5: 6},
+        )
+
+    def test_frozenstuf_wrap(self):
+        from stuf import frozenstuf
+        self.assertIsInstance(
+            self.qclass(
+                ('a1', 2), ('a3', 4), ('a5', 6)
+            ).frozenstuf_wrap().syncout().value(),
+            frozenstuf,
+        )
+        self.assertEqual(
+            self.qclass(
+                ('a1', 2), ('a3', 4), ('a5', 6)
+            ).frozenstuf_wrap().syncout().value(),
+           frozenstuf({'a1': 2, 'a3': 4, 'a5': 6}),
+        )
+
+    def test_ordereddict_wrap(self):
+        from stuf.utils import OrderedDict
+        self.assertIsInstance(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).ordereddict_wrap().syncout().value(),
+            OrderedDict,
+        )
+        self.assertDictEqual(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).ordereddict_wrap().syncout().value(),
+            OrderedDict({1: 2, 3: 4, 5: 6}),
+        )
+
+    def test_orderedstuf_wrap(self):
+        from stuf import orderedstuf
+        self.assertIsInstance(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).orderedstuf_wrap().syncout().value(),
+            orderedstuf,
+        )
+        self.assertEqual(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).orderedstuf_wrap().syncout().value(),
+           orderedstuf({1: 2, 3: 4, 5: 6}),
+        )
+
+    def test_stuf_wrap(self):
+        from stuf import stuf
+        self.assertIsInstance(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).stuf_wrap().syncout().value(),
+            stuf,
+        )
+        self.assertDictEqual(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).stuf_wrap().syncout().value(),
+           stuf({1: 2, 3: 4, 5: 6}),
+        )

File twoq/tests/man/filtering.py

             ('name', 'beastly')))),
         )
 
+    def test_mro(self):
+        from inspect import isclass
+        class stooges: #@IgnorePep8
+            name = 'moe'
+            age = 40
+        class stoog2(stooges): #@IgnorePep8
+            name = 'larry'
+            age = 50
+        class stoog3(stoog2): #@IgnorePep8
+            name = 'curly'
+            age = 60
+        self._true_true_false(
+            self.qclass(stoog3).tap(
+                lambda x: x, isclass
+            ).tuple_wrap().mro().members().untap().sync(),
+            self.assertEqual,
+            (('age', 60), ('name', 'curly'))
+        )
+
     def test_pick(self):
         from stuf import stuf
         stooges = [

File twoq/tests/man/queuing.py

 
     def test_repr(self):
         from stuf.six import strings
-        self.assertTrue(isinstance(
+        self.assertIsInstance(
             self.qclass([1, 2, 3, 4, 5, 6]).__repr__(), strings,
-        ))
+        )
 
     def test_peek(self):
         initial = self.qclass(1, 2, 3, 4, 5, 6)
         self.assertEqual(len(initial), 0)
 
     def test_extend(self):
-        self.assertEqual(
+        self.assertListEqual(
             self.qclass().extend([1, 2, 3, 4, 5, 6]).syncout().end(),
             [1, 2, 3, 4, 5, 6],
         )
 
     def test_outextend(self):
-        self.assertEqual(
+        self.assertListEqual(
             self.qclass().outextend([1, 2, 3, 4, 5, 6]).end(),
             [1, 2, 3, 4, 5, 6],
         )
 
     def test_extendleft(self):
-        self.assertEqual(
+        self.assertListEqual(
             self.qclass().extendleft([1, 2, 3, 4, 5, 6]).syncout().end(),
             [6, 5, 4, 3, 2, 1]
         )
     ## queue balancing ########################################################
     ###########################################################################
 
+    def test_undo(self):
+        queue = self.qclass(1, 2, 3).extendleft([1, 2, 3, 4, 5, 6]).syncout()
+        self.assertListEqual(queue.snapshot(), [6, 5, 4, 3, 2, 1, 1, 2, 3])
+        self.assertListEqual(queue.peek(), [6, 5, 4, 3, 2, 1, 1, 2, 3])
+        queue.append(1).undo().syncout()
+        self.assertListEqual(queue.snapshot(), [6, 5, 4, 3, 2, 1, 1, 2, 3])
+        queue.append(1).append(2).undo().syncout()
+        self.assertListEqual(queue.snapshot(), [6, 5, 4, 3, 2, 1, 1, 2, 3, 1])
+        queue.append(1).append(2).undo(2).syncout()
+        self.assertListEqual(queue.snapshot(), [6, 5, 4, 3, 2, 1, 1, 2, 3, 1])
+        queue.undo(everything=True).syncout()
+        self.assertListEqual(queue.syncout().end(), [1, 2, 3])
+
     def test_insync(self):
         q = self.qclass(1, 2, 3, 4, 5, 6).syncout().clearin().sync()
         self.assertEqual(list(q.incoming), list(q.outgoing))
     ###########################################################################
 
     def test_results(self):
-        self.assertEqual(
+        self.assertListEqual(
             list(self.qclass(1, 2, 3, 4, 5, 6).syncout().results()),
             [1, 2, 3, 4, 5, 6]
         )
+
+    def test_unicode_wrap(self):
+        from stuf.six import texts, u
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).unicode_wrap().syncout().value(),
+            texts,
+        )
+        self.assertEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).unicode_wrap().syncout().value(),
+            u('123456'),
+        )
+
+    def test_byte_wrap(self):
+        from stuf.six import binaries, b
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).byte_wrap().syncout().value(),
+            binaries,
+        )
+        self.assertEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).byte_wrap().syncout().value(),
+            b('123456'),
+        )
+
+    def test_tuple_wrap(self):
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).tuple_wrap().syncout().value(),
+            tuple,
+        )
+        self.assertTupleEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).tuple_wrap().syncout().value(),
+            (1, 2, 3, 4, 5, 6),
+        )
+
+    def test_set_wrap(self):
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).set_wrap().syncout().value(),
+            set,
+        )
+        self.assertSetEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).set_wrap().syncout().value(),
+            set([1, 2, 3, 4, 5, 6]),
+        )
+
+    def test_deque_wrap(self):
+        from collections import deque
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).deque_wrap().syncout().value(),
+            deque,
+        )
+        self.assertEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).deque_wrap().syncout().value(),
+            deque([1, 2, 3, 4, 5, 6]),
+        )
+
+    def test_frozenset_wrap(self):
+        self.assertIsInstance(
+            self.qclass(1, 2, 3, 4, 5, 6).frozenset_wrap().syncout().value(),
+            frozenset,
+        )
+        self.assertSetEqual(
+            self.qclass(1, 2, 3, 4, 5, 6).frozenset_wrap().syncout().value(),
+            frozenset([1, 2, 3, 4, 5, 6]),
+        )
+
+    def test_dict_wrap(self):
+        self.assertIsInstance(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).dict_wrap().syncout().value(),
+            dict,
+        )
+        self.assertDictEqual(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).dict_wrap().syncout().value(),
+            {1: 2, 3: 4, 5: 6},
+        )
+
+    def test_frozenstuf_wrap(self):
+        from stuf import frozenstuf
+        self.assertIsInstance(
+            self.qclass(
+                ('a1', 2), ('a3', 4), ('a5', 6)
+            ).frozenstuf_wrap().syncout().value(),
+            frozenstuf,
+        )
+        self.assertEqual(
+            self.qclass(
+                ('a1', 2), ('a3', 4), ('a5', 6)
+            ).frozenstuf_wrap().syncout().value(),
+           frozenstuf({'a1': 2, 'a3': 4, 'a5': 6}),
+        )
+
+    def test_ordereddict_wrap(self):
+        from stuf.utils import OrderedDict
+        self.assertIsInstance(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).ordereddict_wrap().syncout().value(),
+            OrderedDict,
+        )
+        self.assertDictEqual(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).ordereddict_wrap().syncout().value(),
+            OrderedDict({1: 2, 3: 4, 5: 6}),
+        )
+
+    def test_orderedstuf_wrap(self):
+        from stuf import orderedstuf
+        self.assertIsInstance(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).orderedstuf_wrap().syncout().value(),
+            orderedstuf,
+        )
+        self.assertEqual(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).orderedstuf_wrap().syncout().value(),
+           orderedstuf({1: 2, 3: 4, 5: 6}),
+        )
+
+    def test_stuf_wrap(self):
+        from stuf import stuf
+        self.assertIsInstance(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).stuf_wrap().syncout().value(),
+            stuf,
+        )
+        self.assertDictEqual(
+            self.qclass(
+                (1, 2), (3, 4), (5, 6)
+            ).stuf_wrap().syncout().value(),
+           stuf({1: 2, 3: 4, 5: 6}),
+        )