Useful decorator

Paul McGuire ptmcg at austin.rr.com
Sat Apr 14 19:48:30 EDT 2007


On Apr 14, 5:03 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> In the course of writing a bunch of generator-oriented code I kept
> wanting to temporarily truncate the output of generators for debugging,
> i.e. I might have a function like
>
>     def generate_bazillion_items():
>        for line in bazillion_line_file:
>           yield itemify(line)
>
> where I wanted to test the program on just the first 20 lines of the
> file.  After a number of rounds of editing the program to wrap an
> xrange(20) loop I went to using islice(bazillion_line_file, 20), but
> even more handy was to write this decorator:
>
>     def truncate(n):
>          from itertools import islice
>          def trunc(gen):  # truncate generator to n items
>             return lambda *a,**kw: islice(gen(*a,**kw), n)
>          return trunc
>
> Then to chop bazillion_items to 20 items, I just write:
>
>     @truncate(20)
>     def generate_bazillion_items():
>        for line in bazillion_line_file:
>           yield itemify(line)
>
> When I want to run the whole file, I comment out the @truncate line,
> and if I want to debug again, I just uncomment it.

You should add this to the Python Wiki at http://wiki.python.org/moin/PythonDecoratorLibrary

-- Paul




More information about the Python-list mailing list