Skip to content

unpacker.tell() like in file.tell() #76

Closed

Description

@sirex

When reading large file of objects, encoded with MessagePack it is important to know object offset.

If you use f.tell() it returns read buffer offset, not actual object offset.

It would be great to have some thing like this:

unpacker = msgpack.Unpacker(stream)
for obj in unpacker:
    offset = unpacker.tell()

Or even this:

unpacker = msgpack.Unpacker(stream, tell=True)
for offset, obj in unpacker:
    # ...

Activity

  1. jnothman commented on Oct 15, 2013

    @jnothman
    Contributor

    This sort of low-level file tracking can be achieved through the write_bytes arg to Unpacker.unpack, Unpacker.skip, etc. For example:

    class StreamCounter(object)
        __slots__ = ('nbytes',)
        def __init__(self):
            self.nbytes = 0
        def __call__(self, s):
            self.nbytes += len(s)
    
    unpacker = msgpack.Unpacker(stream)
    counter = StreamCounter()
    while True:
        try:
            obj = unpacker.unpack(write_bytes=counter)
        except msgpack.OutOfData:
            break
        offset = counter.nbytes

    (Yes, it will be slower than if tell were natively supported.)

  2. sirex commented on Oct 16, 2013

    @sirex
    Author

    @jnothman thanks, some how I missed this write_bytes parameter, and it solves my issue.

    Now, as a workaround I packb obj and count bytes in it, to get offset, but using write_bytes is much more efficient. Thank again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions