开发者

What is a good way to decorate an iterator to alter the value before next is called in python?

开发者 https://www.devze.com 2023-03-12 11:33 出处:网络
I am working on a problem that involves validating a format from within unified diff patch. The variables within the inner format can span multiple lines at a time, so I wrote a generator that pulls

I am working on a problem that involves validating a format from within unified diff patch.

The variables within the inner format can span multiple lines at a time, so I wrote a generator that pulls each line and yields the variable when it is complete.

To avoid having to rewrite this function wh开发者_Python百科en reading from a unified diff file, I created a generator to strip the unified diff characters from the line before passing it to the inner format validator. However, I am getting stuck in an infinite loop (both in the code and in my head). I have abstracted to problem to the following code. I'm sure there is a better way to do this. I just don't know what it is.

from collections import Iterable

def inner_format_validator(inner_item):
    # Do some validation to inner items
    return inner_item[0] != '+'

def inner_gen(iterable):
    for inner_item in iterable:
        # Operates only on inner_info type data
        yield inner_format_validator(inner_item)

def outer_gen(iterable):
    class DecoratedGenerator(Iterable):
        def __iter__(self):
            return self
        def next(self):
            # Using iterable from closure
            for outer_item in iterable:
                self.outer_info = outer_item[0]
                inner_item = outer_item[1:]
                return inner_item
    decorated_gen = DecoratedGenerator()
    for inner_item in inner_gen(decorated_gen):
        yield inner_item, decorated_gen.outer_info

if __name__ == '__main__':    
    def wrap(string):
        # The point here is that I don't know what the first character will be
        pseudo_rand = len(string)
        if pseudo_rand * pseudo_rand % 2 == 0:
            return '+' + string
        else:
            return '-' + string

    inner_items = ["whatever"] * 3
    # wrap screws up inner_format_validator
    outer_items = [wrap("whatever")] * 3
    # I need to be able to
    # iterate over inner_items
    for inner_info in inner_gen(inner_items):
        print(inner_info)
    # and iterate over outer_items
    for outer_info, inner_info in outer_gen(outer_items):
        # This is an infinite loop
        print(outer_info)
        print(inner_info)

Any ideas as to a better, more pythonic way to do this?


I would do something simpler, like this:

def outer_gen(iterable):

    iterable = iter(iterable)
    first_item = next(iterable)
    info = first_item[0]

    yield info, first_item[1:]

    for item in iterable:
        yield info, item

This will execute the 4 first lines only once, then enter the loop and yield what you want.

You probably want to add some try/except to cacth IndexErrors here and there.

If you want to take values while they start with something or the contrary, remember you can use a lot of stuff from the itertools toolbox, and in particular dropwhile, takewhile and chain:

>>> import itertools
>>> l = ['+foo', '-bar', '+foo']
>>> list(itertools.takewhile(lambda x: x.startswith('+'), l))
['+foo']
>>> list(itertools.dropwhile(lambda x: x.startswith('+'), l))
['-bar', '+foo']
>>> a = itertools.takewhile(lambda x: x.startswith('+'), l)
>>> b = itertools.dropwhile(lambda x: x.startswith('+'), l)
>>> list(itertools.chain(a, b))
['+foo', '-bar', '+foo']

And remember that you can create generators like comprehension lists, store them in variables and chain them, just like you would pipe linux commands:

import random

def create_item():
    return random.choice(('+', '-')) + random.choice(('foo', 'bar'))

random_items = (create_item() for s in xrange(10))
added_items = ((i[0], i[1:]) for i in random_items if i.startswith('+'))
valid_items = ((prefix, line) for prefix, line in added_items if 'foo' in line)

print list(valid_items)

With all this, you should be able to find some pythonic way to solve your problem :-)


I still don't like this very much, but at least it's shorter and a tad more pythonic:

from itertools import imap, izip
from functools import partial

def inner_format_validator(inner_item):
    return not inner_item.startswith('+')

inner_gen = partial(imap, inner_format_validator)

def split(astr):
    return astr[0], astr[1:]

def outer_gen(iterable):
    outer_stuff, inner_stuff = izip(*imap(split, iterable))
    return izip(inner_gen(inner_stuff), outer_stuff)

[EDIT] inner_gen() and outer_gen() without imap and partial:

def inner_gen(iterable):
    for each in iterable:
        yield inner_format_validator(each)

def outer_gen(iterable):
    outer_stuff, inner_stuff = izip(*(split(each) for each in iterable))
    return izip(inner_gen(inner_stuff), outer_stuff)

Maybe this is a better, though different, solution:

def transmogrify(iter_of_iters, *transmogrifiers):
    for iters in iter_of_iters:
        yield (
            trans(each) if trans else each
                for trans, each in izip(transmogrifiers, iters)
        )

for outer, inner in transmogrify(imap(split, stuff), inner_format_validator, None):
    print inner, outer


I think it will do what you intended if you change the definition of DecoratedGenerator to this:

class DecoratedGenerator(Iterable):
    def __iter__(self):
        # Using iterable from closure
        for outer_item in iterable:
            self.outer_info = outer_item[0]
            inner_item = outer_item[1:]
            yield inner_item

Your original version never terminated because its next() method was stateless and would return the same value every time it was called. You didn't need to have a next() method at all, though--you can implement __iter__() yourself (as I did), and then it all works fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消