Iterating over a stream with only readline() method [newbie question]

Alex Martelli aleaxit at yahoo.com
Sat Oct 7 05:33:36 EDT 2000


"Syam Pannala" <syam at c544784-a.stcla1.sfba.home.com> wrote in message
news:slrn8ttc2s.6lo.syam at c544784-a.stcla1.sfba.home.com...
> [Newbie Alert!!]

Welcome!

>     i have a stream which provides only a readline() method (it can come
from
> a variety of sources including network and i dont want to buffer it so
> that i can implement readlines()).

OK.  You have an object that has a slightly different interface from
either a fileobject or a sequence.

> What is the python idiom for iterating over this stream until it throws
> EOFError ?

Best solution may be to use the Adapter idiom, aka Wrapper idiom: it's
often best when you want to slightly change an object's interface.  For
example:

class AsSequence:
    def __init__(self, theobj):
        self.obj = theobj
    def __getitem__(self, i):
        try: return self.obj.readline()
        except EOFError: raise IndexError

Here, we are adapting the actual object's interface (aka protocol), "Get
next item by calling readline, raise EOFError when done", to the sequence
interface/protocol needed by Python (specifically, the for statement), "Get
next item by calling __getitem__ with increasing index, raise IndexError
when done".  This is all done in the __getitem__ method.  The __init__
just does the typical "set argument to a field of self" initialization
idiom.

> for i in stream.readlines():
>    ..use i ..
>
> i want the ..use i.. code not to change very much. Also, i think

So you will now say:

    for i in AsSequence(stream):
        .. use i ..

without any change at all to the ..use i.. code.  The Adapter (aka
Wrapper) idiom (aka Design Pattern: it's identified as such in the
seminal "Design Patterns" book by the so-called Gang Of Four) is
powerful, handy, and of very frequent use.


> while 1:
>     try:
> i = stream.readline()
>     expcept EOFError:
>         break
>     ... use i ..
>
> looks very inelegant, even though that is what i have used as the current
> implementation.

I agree with your assessment.  I think the Adapter is much better.


Alex






More information about the Python-list mailing list