468,305 Members | 1,472 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,305 developers. It's quick & easy.

Iterating over marshal

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.

--
Tim Lesher
tl*****@gmail.com

Oct 6 '06 #1
2 1149
Tim Lesher wrote:
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>

Oct 6 '06 #2
On Oct 6, 4:19 pm, Fredrik Lundh <fred...@pythonware.comwrote:
Tim Lesher wrote:
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).

--
Tim Lesher
tl*****@gmail.com

Oct 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.