RawOffsets

class baseband.base.offsets.RawOffsets(frame_nr=None, offset=None, frame_nbytes=0)[source] [edit on github]

Bases: object

File offset tracker.

Keeps track of offsets from expected file position as a function of frame number, by keeping a joint list, of the first frame number beyond which a certain offset will hold.

The offset for a given frame number is retrieved via __getitem__, while new offsets are added via __setitem__ (keeping the list of frame numbers minimal for identical offsets).

Parameters:
frame_nrlist

Frame number for which one has offsets.

offsetlist

Corresponding offsets.

frame_nbytesint

Possible frame size to include in all returned offsets, i.e., output will be offset + index * frame_nbytes. Default: 0.

Examples

The usage is best seen through an example:

>>> from baseband.base.offsets import RawOffsets
>>> offsets = RawOffsets([6], [5])
>>> offsets[3]  # Implicitly 0 before first entry
0
>>> offsets[10]  # Assumed the same as 6
5
>>> offsets[10] = 9  # But suppose we find 10 has 9.
>>> offsets[10]  # Then it takes that
9
>>> offsets[9]  # But before is still 5.
5
>>> offsets[8] = 9  # But if we find 8 has 9 too.
>>> offsets[9]  # Then 9 is the same.
9
>>> offsets  # And the list is kept minimal.
RawOffsets(frame_nr=[6, 8], offset=[5, 9], frame_nbytes=0)
>>> offsets[9] = 9  # This is a no-op.
>>> offsets[10] = 10  # But this is not.
>>> offsets
RawOffsets(frame_nr=[6, 8, 10], offset=[5, 9, 10], frame_nbytes=0)

Similarly, if a frame size is passed in:

>>> offsets = RawOffsets([6, 8, 10], [5, 9, 10], frame_nbytes=1000)
>>> offsets
RawOffsets(frame_nr=[6, 8, 10], offset=[5, 9, 10], frame_nbytes=1000)
>>> offsets[1]
1000
>>> offsets[8]
8009
>>> offsets[8] = 8005  # This removes the offset for 9.
>>> offsets[8]
8005
>>> offsets
RawOffsets(frame_nr=[6, 10], offset=[5, 10], frame_nbytes=1000)