Iterating over marshal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Lesher

    #1

    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(mar shal.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(fob j):
    while 1:
    try:
    yield marshal.load(fo bj)
    except EOFError:
    raise StopIteration

    objs = [obj for obj in itermarshal(fob j)]

    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
    tlesher@gmail.c om

  • Fredrik Lundh

    #2
    Re: Iterating over marshal

    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(mar shal.load(fobj) )
    except EOFError:
    break
    the canonical way to do this is to put the objects in a sequence
    container *before* marshalling them.

    </F>

    Comment

    • Tim Lesher

      #3
      Re: Iterating over marshal

      On Oct 6, 4:19 pm, Fredrik Lundh <fred...@python ware.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(mar shal.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
      tlesher@gmail.c om

      Comment

      Working...