NiBabel

Access a cacophony of neuro-imaging file formats

Previous topic

nibabel.nicom.structreader.Unpacker

Next topic

nibabel.nifti1

Reggie -- the one

nibabel.nicom.structreader.Unpacker

digraph inheritance98a91698ac { rankdir=LR; ratio=compress; fontsize=14; size="6.0, 8.0"; "Unpacker" [shape=ellipse,URL="#nibabel.nicom.structreader.Unpacker",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=14,color=dodgerblue1,style=filled,tooltip="Class to unpack values from buffer object",height=0.75]; }

class nibabel.nicom.structreader.Unpacker(buf, ptr=0, endian=None)

Class to unpack values from buffer object

The buffer object is usually a string. Caches compiled struct format strings so that repeated unpacking with the same format string should be faster than using struct.unpack directly.

Examples

>>> a = '1234567890' #23dt : bytes
>>> upk = Unpacker(a)
>>> upk.unpack('2s') #23dt next : bytes
('12',)
>>> upk.unpack('2s') #23dt next : bytes
('34',)
>>> upk.ptr
4
>>> upk.read(3) #23dt next : bytes
'567'
>>> upk.ptr
7

Initialize unpacker

Parameters:

buf : buffer

object implementing buffer protocol (e.g. str)

ptr : int, optional

offset at which to begin reads from buf

endian : None or str, optional

endian code to prepend to format, as for unpack endian codes. None (the default) corresponds to the default behavior of struct - assuming system endian unless you specify the byte order specifically in the format string passed to unpack

read(n_bytes=-1)

Return byte string of length n_bytes at current position

Returns sub-string from self.buf and updates self.ptr to the position after the read data.

Parameters:

n_bytes : int, optional

number of bytes to read. Can be -1 (the default) in which case we return all the remaining bytes in self.buf

Returns:

s : byte string

unpack(fmt)

Unpack values from contained buffer

Unpacks values from self.buf and updates self.ptr to the position after the read data.

Parameters:

fmt : str

format string as for unpack

Returns:

values : tuple

values as unpacked from self.buf according to fmt