structread

Discussion in 'Python' started by Lawrence D'Oliveiro, Oct 20, 2009.

  1. This routine is so useful, I wonder there's nothing like it in module
    struct, or anywhere else I'm aware of:

    def structread(fromfile, decode_struct) :
    """reads sufficient bytes from fromfile to be unpacked according to
    decode_struct, and returns the unpacked results."""
    return struct.unpack(decode_struct, fromfile.read(struct.calcsize(decode_struct)))
    #end structread

    For a non-trivial example of its use, see
    <http://www.codecodex.com/wiki/index.php?title=Reading_time_zone_files>.
    Lawrence D'Oliveiro, Oct 20, 2009
    #1
    1. Advertising

  2. Lawrence D'Oliveiro

    Aahz Guest

    In article <hbk4p5$uc$>,
    Lawrence D'Oliveiro <_zealand> wrote:
    >
    >This routine is so useful, I wonder there's nothing like it in module
    >struct, or anywhere else I'm aware of:
    >
    > def structread(fromfile, decode_struct) :
    > """reads sufficient bytes from fromfile to be unpacked according to
    > decode_struct, and returns the unpacked results."""
    > return struct.unpack(decode_struct, fromfile.read(struct.calcsize(decode_struct)))
    > #end structread


    Guido is generally opposed to simple one-liners.
    --
    Aahz () <*> http://www.pythoncraft.com/

    Member of the Groucho Marx Fan Club
    Aahz, Oct 23, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page