Iterating over marshal

T

Tim Lesher

I'm using the marshal library to unmarshal a file containing one or
more objects. The canonical way seems to be:

objs = []
while 1:
try:
objs.append(marshal.load(fobj))
except EOFError:
break

Maybe it's just me, but it seems as if this should be iterable. I can
get the behavior I want by writing:

def itermarshal(fobj):
while 1:
try:
yield marshal.load(fobj)
except EOFError:
raise StopIteration

objs = [obj for obj in itermarshal(fobj)]

But it seems that this should be built-in somewhere. Given that the
marshal library has been around since roughly forever, is it just that
no one's bothered to add iteration support to it, or am I missing
something?

Thanks.
 
F

Fredrik Lundh

Tim said:
I'm using the marshal library to unmarshal a file containing one or
more objects. The canonical way seems to be:

objs = []
while 1:
try:
objs.append(marshal.load(fobj))
except EOFError:
break

the canonical way to do this is to put the objects in a sequence
container *before* marshalling them.

</F>
 
T

Tim Lesher

Tim said:
I'm using the marshal library to unmarshal a file containing one or
more objects. The canonical way seems to be:
objs = []
while 1:
try:
objs.append(marshal.load(fobj))
except EOFError:
breakthe canonical way to do this is to put the objects in a sequence
container *before* marshalling them.

That makes sense; unfortunately, I'm unmarshalling from an external
data source I don't control (the Perforce version control client).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top