twoq / thingpipe / active.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# -*- coding: utf-8 -*-
'''actively evaluated pipelines'''

from copy import copy
from itertools import repeat
from collections import deque
from contextlib import contextmanager

from stuf.utils import clsname

from thingpipe.transform import StringMixin
from thingpipe.mapping import RepeatMixin, MapMixin
from thingpipe.order import RandomMixin, OrderMixin
from thingpipe.base import SLOTS, PipeMixin, ResultsMixin
from thingpipe.reducing import MathMixin, TruthMixin, ReduceMixin
from thingpipe.filtering import FilterMixin, ExtractMixin, SetMixin, SliceMixin


class ActiveMixin(PipeMixin):

    '''base active pipeline'''

    def __init__(self, *things, **kw):
        try:
            incoming = deque(things[0]) if len(things) == 1 else deque(things)
        except TypeError:
            incoming = deque()
            incoming.extend(things)
        super(ActiveMixin, self).__init__(incoming, deque())
        # set iterator
        self._iterator = self._iterexcept
        # work things
        self._work = deque()
        # utility things
        self._util = deque()

    ###########################################################################
    ## mode things ############################################################
    ###########################################################################

    def ro(self):
        '''switch to read-only mode'''
        with self.ctx3(outq=self._UTILVAR, savepoint=False):
            self._xtend(self._iterable)
        with self.ctx1(hard=True, workq=self._UTILVAR, savepoint=False):
            self.current_mode = self._RO
            return self

    ###########################################################################
    ## context things #######################################################
    ###########################################################################

    @contextmanager
    def ctx2(self, **kw):
        '''swap for two-armed context'''
        self.swap(
            outq=kw.get(self._OUTCFG, self._INVAR), context=self.ctx2(), **kw
        )
        getr_ = lambda x: getattr(self, x)
        outq = getr_(self._OUTQ)
        utilq = getr_(self._UTILQ)
        workq = getr_(self._WORKQ)
        # clear all work things
        workq.clear()
        # extend work things with outgoing things
        workq.extend(outq)
        # swap iterator
        self._iterator = self._breakcount
        yield
        # clear outgoing things if so configured
        if self._clearout:
            outq.clear()
        # extend outgoing things with utility things
        outq.extend(utilq)
        # clear utility things
        utilq.clear()
        # return to global context
        self.reswap()

    @contextmanager
    def ctx3(self, **kw):
        '''swap for three-armed context'''
        self.swap(
            utilq=kw.get(self._WORKCFG, self._WORKVAR), context=self.ctx3, **kw
        )
        getr_ = lambda x: getattr(self, x)
        outq = getr_(self._OUTQ)
        utilq = getr_(self._UTILQ)
        workq = getr_(self._WORKQ)
        # clear work things
        workq.clear()
        # extend work things with incoming things
        workq.extend(getr_(self._INQ))
        # swap iterators
        self._iterator = self._breakcount
        yield
        # clear outgoing things if so configured
        if self._clearout:
            outq.clear()
        # extend outgoing things with utility things
        outq.extend(utilq)
        # clear utility things
        utilq.clear()
        # return to global context
        self.reswap()

    @contextmanager
    def ctx4(self, **kw):
        '''swap for four-armed context'''
        self.swap(context=self.ctx4, **kw)
        getr_ = lambda x: getattr(self, x)
        outq = getr_(self._OUTQ)
        utilq = getr_(self._UTILQ)
        workq = getr_(self._WORKQ)
        # clear work things
        workq.clear()
        # extend work things with incoming things
        workq.extend(getr_(self._INQ))
        # swap iterators
        self._iterator = self._iterexcept
        yield
        # clear outgoing things if so configured
        if self._clearout:
            outq.clear()
        # extend outgoing things with utility things
        outq.extend(utilq)
        # clear utility things
        utilq.clear()
        # return to global context
        self.reswap()

    @contextmanager
    def autoctx(self, **kw):
        '''swap for auto-synchronizing context'''
        self.swap(context=self.autoctx, **kw)
        getr_ = lambda x: getattr(self, x)
        inq, workq = getr_(self._INQ), getr_(self._WORKQ)
        utilq,  outq = getr_(self._UTILQ), getr_(self._OUTQ)
        # clear work things
        workq.clear()
        # extend work things with incoming things
        workq.extend(inq)
        # swap iterators
        self._iterator = self._iterexcept
        yield
        # clear outgoing things if so configured
        if self._clearout:
            outq.clear()
        outq.extend(utilq)
        # clear incoming things
        inq.clear()
        inq.extend(utilq)
        # clear utility things
        utilq.clear()
        # return to global context
        self.reswap()

    ###########################################################################
    ## savepoint for things ###################################################
    ###########################################################################

    def _savepoint(self):
        '''take savepoint of incoming things'''
        self._savepoints.append(copy(getattr(self, self._INQ)))
        return self

    ###########################################################################
    ## iterate things #########################################################
    ###########################################################################

    @property
    def _iterable(self):
        '''iterable'''
        return self._iterator(self._WORKQ)

    def _breakcount(self, attr='_UTILQ'):
        '''
        breakcount iterator

        @param attr: things to iterate over
        '''
        dq = getattr(self, attr)
        length, call = len(dq), dq.popleft
        for i in repeat(None, length):  # @UnusedVariable
            yield call()

    def _iterexcept(self, attr='_UTILQ'):
        '''
        call a function repeatedly until an exception is raised

        Converts a call-until-exception interface to an iterator interface.
        Like `iter(call, sentinel)` but uses an exception instead of a sentinel
        to end the loop.

        Raymond Hettinger, Python Cookbook recipe # 577155
        '''
        call = getattr(self, attr).popleft
        try:
            while 1:
                yield call()
        except IndexError:
            pass

    ###########################################################################
    ## extend things ##########################################################
    ###########################################################################

    def _xtend(self, things):
        '''extend utility things with `things` wrapped'''
        getattr(self, self._UTILQ).extend(things)
        return self

    def _xtendleft(self, things):
        '''extend left side of utility things with `things`'''
        getattr(self, self._UTILQ).extendleft(things)
        return self

    def _iter(self, things):
        '''extend work things with `things` wrapped in iterator'''
        getattr(self, self._UTILQ).extend(iter(things))
        return self

    ###########################################################################
    ## append things ##########################################################
    ###########################################################################

    def _append(self, things):
        '''append `things` to utility things'''
        getattr(self, self._UTILQ).append(things)
        return self

    def _appendleft(self, things):
        '''append `things` to left side of utility things'''
        getattr(self, self._UTILQ).appendleft(things)
        return self

    ###########################################################################
    ## know things ############################################################
    ###########################################################################

    def __repr__(self):
        getr_, list_ = lambda x: getattr(self, x), list
        return self._repr(
            self.__module__,
            clsname(self),
            self.current_mode.upper(),
            self._INQ,
            list_(getr_(self._INQ)),
            self._WORKQ,
            list_(getr_(self._WORKQ)),
            self._UTILQ,
            list_(getr_(self._UTILQ)),
            self._OUTQ,
            list_(getr_(self._OUTQ)),
            id(self),
        )

    def __len__(self):
        '''number of incoming things'''
        return len(self.incoming)
    
    count = __len__

    def countout(self):
        '''number of outgoing things'''
        return len(self.outgoing)

    ###########################################################################
    ## clear things ###########################################################
    ###########################################################################

    def _clearu(self):
        '''clear utility things'''
        self._util.clear()
        return self

    def _clearw(self):
        '''clear work things'''
        self._work.clear()
        return self

    def clearin(self):
        '''clear incoming things'''
        self.incoming.clear()
        return self

    def clearout(self):
        '''clear outgoing things'''
        self.outgoing.clear()
        return self


class EndMixin(ResultsMixin):

    '''result things mixin'''
    
    def __iter__(self):
        '''yield outgoing things, clearing outgoing things as it iterates'''
        return self._iterexcept(self._OUTQ)
    
    results = __iter__

    def end(self):
        '''return outgoing things then clear wrap everything'''
        # return to default context
        self.unswap()
        wrap, outgoing = self._wrapper, self.outgoing
        wrap = self.outgoing.pop() if len(outgoing) == 1 else wrap(outgoing)
        # clear every last thing
        self.clear()._clearsp()
        return wrap

    def snapshot(self):
        '''snapshot of current outgoing things'''
        wrap = copy(self.outgoing)
        return wrap.pop() if len(wrap) == 1 else self._wrapper(wrap)

    def out(self):
        '''return outgoing things and clear outgoing things'''
        # return to default context
        self.unswap()
        wrap, outgoing = self._wrapper, self.outgoing
        wrap = outgoing.pop() if len(outgoing) == 1 else wrap(outgoing)
        # clear outgoing things
        self.clearout()
        return wrap


class ResultMixin(ActiveMixin, EndMixin):

    '''pipeline with with results extractor mixin'''


class activepipe(
    ResultMixin, FilterMixin, MapMixin, ReduceMixin, OrderMixin,
    ExtractMixin, SetMixin, SliceMixin, TruthMixin, MathMixin, RepeatMixin,
    RandomMixin, StringMixin,
):

    '''active pipeline'''

    __slots__ = SLOTS


class collectpipe(ResultMixin, ExtractMixin):

    '''collecting pipeline'''

    __slots__ = SLOTS


class setpipe(ResultMixin, SetMixin):

    '''set pipeline'''

    __slots__ = SLOTS


class slicepipe(ResultMixin, SliceMixin):

    '''slice pipeline'''

    __slots__ = SLOTS


class filterpipe(ResultMixin, FilterMixin, ExtractMixin, SetMixin, SliceMixin):

    '''filter pipeline'''

    __slots__ = SLOTS


class repeatpipe(ResultMixin, RepeatMixin):

    '''repeat pipeline'''

    __slots__ = SLOTS


class mappipe(ResultMixin, RepeatMixin, MapMixin):

    '''mapping pipeline'''

    __slots__ = SLOTS


class randompipe(ResultMixin, RandomMixin):

    '''randomizing pipeline'''

    __slots__ = SLOTS


class sortpipe(ResultMixin, OrderMixin, RandomMixin):

    '''ordering pipeline'''

    __slots__ = SLOTS


class mathpipe(ResultMixin, MathMixin):

    '''auto-balancing math pipeline'''

    __slots__ = SLOTS


class truthpipe(ResultMixin, TruthMixin):

    '''auto-balancing truth pipeline'''

    __slots__ = SLOTS


class reducepipe(ResultMixin, MathMixin, TruthMixin, ReduceMixin):

    '''auto-balancing reduce pipeline'''

    __slots__ = SLOTS


class stringpipe(ResultMixin, StringMixin):

    '''auto-balancing string transformation pipeline'''

    __slots__ = SLOTS


class transformpipe(ResultMixin, StringMixin):

    '''auto-balancing transformation pipeline'''

    __slots__ = SLOTS
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.