Documentation Status Test Coverage

fixedrec – fixed size record IO

Documentation is available at:

Installing from PyPI

This is what you want if you just want to use fixedrec:

pip install fixedrec

As a source package

This is what you want if you are developing fixedrec or want to make local changes to the source code.

pip install -e <path>

Developing fixedrec


if you’re using this as a template for new projects, remember to python register <projectname> before you upload to PyPi.

Uploading to PyPI

  • only source distribution:

    python sdist upload
  • source and windows installer:

    python sdist bdist_wininst upload
  • source, windows, and wheel installer:

    python sdist bdist_wininst bdist_wheel upload
  • create a documentation bundle to upload to PyPi:

    cd build/sphinx/html && zip -r ../../../ *

Running tests

One of:

python test
py.test fixedrec

with coverage (one of):

py.test --cov=.
python --cov=.
coverage run && coverage report

Building documentation

python build_sphinx

API documentation


class fixedrec.fixedrec.RecordFile(fname, blocksize=4, overwrite=False)[source]

Low level fixed-size record file. Record numbers are zero-based.


Close file and write statusrec.


Return number of records in the fils.


Create a new keystore file (overwriting/deleting any existing file.

fname (str): Filename to use.
(file): The created file.

Return the current position.


Go to start of first record.


Go to the starting position of the last record.


Position the file at record n, if n == -1, then go to the end of the file (writing then becomes appending). It is possible to go past the end of the file and write a record, causing the file to grow.


Advance forward or backward (negative n) n records.

open(fname, overwrite)[source]

Open or create the file.


Open the file for read and write if possible.


Read a block at the current position.

swap(a, b)[source]

Swap records at positions a and b.


Truncate the file at recnum (ie. recnum will be gone).

write(data, flush=True)[source]

Write data to file at current position.

exception fixedrec.fixedrec.RecordFileError[source]

Base Exception for block files.


This module contains enough knowledge about struct format strings to figure out size and position of fields.

class fixedrec.layout.Field(**kw)[source]

Representation of a field defined in a struct format string. (should only be created from Layout.


Get this field’s value from data.

set_value(data, value)[source]

Set this field to value in data.

class fixedrec.layout.Layout(layout, *names, **kw)[source]

Record layout.


record_layout = Layout(
layoutre = <_sre.SRE_Pattern object>

regex matching one field in a struct format string

record_prefix = {'!': 'network-endian', '@': 'native aligned', '=': 'native', '<': 'little-endian', '>': 'big-endian'}

legal struct format string record prefixes


Split the byte string data into a list of substrings holding the data for each field.

struct_field_sizes = {'Q': 8, 'c': 1, 'b': 1, 'd': 8, 'P': 4, 'f': 4, 'i': 4, 'h': 2, 'l': 4, 'p': 1, 'L': 4, 'q': 8, 'I': 4, 'N': 4, 'B': 1, 'H': 2, 'x': 1, 's': 1, 'n': 8, '?': 1}

byte sizes corresponding to struct character codes

struct_field_types = {'Q': 'unsigned long long', 'c': 'char', 'b': 'signed char', 'd': 'double', 'P': 'void *', 'f': 'float', 'i': 'int', 'h': 'short', 'l': 'long', 'p': 'char[]', 'L': 'unsigned long', 'q': 'long long', 'I': 'unsigned int', 'N': 'size_t', 'B': 'unsigned char', 'H': 'unsigned short', 'x': 'pad byte', 's': 'char[]', 'n': 'ssize_t', '?': '_Bool'}

types corresponding to struct character codes


class fixedrec.record.Record(layout, data=None, **kw)[source]

Record base class, providing attribute access and pretty printing.


class StatusRecord(Record):
    RECTYPE = 'ver'
    layout = Layout(

    def __init__(self, data=None, **kw):
        super(StatusRecord, self).__init__(
            StatusRecord.layout, data, **kw
        if data is None:
            self.rectype = StatusRecord.RECTYPE
            self.version = '1.0.0'
            self.reclen = len(StatusRecord.layout)
   = b'\r'
   = b'\n'
    def set_checksum(self):
        # checksum of all preceeding fields.
        cksm_field = self._layout['chksum']
        cksm = utils.bsd_checksum(self._data[:cksm_field.position])
        cksm_field.set_value(self._data, cksm)

Or at least as close to a dict as we can get and still preserve field order.


Return data for each field of the layout.


Make binary data more readable by converting padding bytes in string fields to underscore (_) and padding fields to star (*).


Return the constructor for the record type.


Decorator that add the client record class cls to the record type registry (as a constructor).


Is the record type registered with a constructor?

Utility functions

Utility functions.

fixedrec.utils.n_(s, replacement='_')[source]

Make binary fields more readable.

fixedrec.utils.pad(data, size, padchar=' ')[source]

Pad the data to exactly length = size.

class fixedrec.utils.pset(*args, **kwds)[source]

A property set is an OrderedDict with prettier string display (useful when working with record lengths that are wider than your terminal).

fixedrec.utils.split_fields(s, sizes)[source]

Split a string into fields based on field sizes.

fixedrec.utils.split_string(s, *ndxs)[source]

String sub-class with a split() method that splits a given indexes.


>>> print split_string('D2008022002', 1, 5, 7, 9)
['D', '2008', '02', '20', '02']

This is a Python implementation of the bsd 16 bit checksum algorithm.

The original code and license is listed at the end of this file, and was fetched from revision 225736.

Listed in its own file to preserve original copyright statement and conditions.


Implementation of the bsd 16-bit checksum algorithm.

Indices and tables