Re: Decorator Pattern with Iterator

Discussion in 'Python' started by Ian Kelly, Jun 11, 2012.

  1. Ian Kelly

    Ian Kelly Guest

    On Mon, Jun 11, 2012 at 1:51 AM, Tom Harris <> wrote:
    > Greetings,
    >
    > I have a class that implements the iterator protocol, and tokenises a string
    > into a series of tokens. As well as the token, it keeps track of some
    > information such as line number, source file, etc.


    So each processor needs to be able to access that information? A
    decorator pattern for the processors to propagate that information
    down might look like this:

    class TokenProcessor(object):
    def __init__(self, processor):
    self._processor = processor
    def __call__(self, tokens):
    self._tokens = tokens
    return self._processor(tokens)
    @property
    def line_number(self):
    return self._tokens.line_number
    @property
    def source_file(self):
    return self._tokens.source_file

    @TokenProcessor
    def processor(tokens):
    for token in tokens:
    line_number = tokens.line_number
    do_stuff(token, line_number)
    yield token

    for token in processor_1(processor_2(tokeniser)):
    do_more_stuff(token)
     
    Ian Kelly, Jun 11, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean Lutrin
    Replies:
    8
    Views:
    662
    Chris Uppal
    Nov 18, 2004
  2. George Sakkis

    Decorator pattern for new-style classes ?

    George Sakkis, Apr 23, 2005, in forum: Python
    Replies:
    1
    Views:
    320
    Michele Simionato
    Apr 25, 2005
  3. Gregory
    Replies:
    3
    Views:
    355
    Gregory
    Dec 15, 2006
  4. glomde
    Replies:
    5
    Views:
    526
    glomde
    Mar 29, 2007
  5. Wildemar Wildenburger

    Observer-Pattern by (simple) decorator

    Wildemar Wildenburger, Jun 1, 2007, in forum: Python
    Replies:
    14
    Views:
    499
    Wildemar Wildenburger
    Jun 2, 2007
Loading...

Share This Page