By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

Iterating over marshal

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.