StringIO objects sharing a buffer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Lotze

    #1

    StringIO objects sharing a buffer

    Hi,

    I want to implement a tokenizer for some syntax. So I thought I'd subclass
    StringIO and make my new class return tokens on next().

    However, if I want to read tokens from two places in the string in turns,
    I'd either need to do some housekeeping of file pointers outside the
    tokenizer class (which is ugly) or use two tokenizers on the same data
    buffer (which seems impossible to me using my preferred approach as a
    file-like object has exactly one file pointer).

    Is there a way for multiple StringIO objects to share a buffer of data, or
    do I have to give up on subclassing StringIO for this purpose? (An
    alternative would be a tokenizer class that has a StringIO instead of
    being one and do the file pointer housekeeping in there.)

    --
    Thomas


  • Mathias Waack

    #2
    Re: StringIO objects sharing a buffer

    Thomas Lotze wrote:
    [color=blue]
    > Is there a way for multiple StringIO objects to share a buffer of
    > data, or do I have to give up on subclassing StringIO for this
    > purpose? (An alternative would be a tokenizer class that has a
    > StringIO instead of being one and do the file pointer housekeeping
    > in there.)[/color]

    I'm not sure if I understand the problem right, but there is a rule
    of thumb:
    Prefer delegation to inheritance. If you can use delegation, avoid
    inheritance. Esp. on python, where (from the point of view of usage)
    there is no distinction between delegation or inheritance.

    Mathias

    Comment

    Working...