FileNameSequencer

class baseband.helpers.sequentialfile.FileNameSequencer(template, header={})[source] [edit on github]

Bases: object

List-like generator of filenames using a template.

The template is formatted, filling in any items in curly brackets with values from the header. It is additionally possible to insert a file number equal to the indexing value, indicated with ‘{file_nr}’.

The length of the instance will be the number of files that exist that match the template for increasing values of the file number (when writing, it is the number of files that have so far been generated).

Parameters:
templatestr

Template to format to get specific filenames. Curly bracket item keywords are case-sensitive (eg. ‘{FRAME_NR}’ or ‘{Frame_NR}’ will not use header['frame_nr'].

headerdict-like

Structure holding key’d values that are used to fill in the format.

Examples

>>> from baseband import vdif
>>> from baseband.helpers import sequentialfile as sf
>>> vfs = sf.FileNameSequencer('a{file_nr:03d}.vdif')
>>> vfs[10]
'a010.vdif'
>>> from baseband.data import SAMPLE_VDIF
>>> with vdif.open(SAMPLE_VDIF, 'rb') as fh:
...     header = vdif.VDIFHeader.fromfile(fh)
>>> vfs = sf.FileNameSequencer('obs.edv{edv:d}.{file_nr:05d}.vdif', header)
>>> vfs[10]
'obs.edv3.00010.vdif'