mode " +bif not os path exists(filenameelse " +bself __fh open(filenamemodeself auto_flush auto_flush there are two different record sizes the binaryrecordfile record_size is the one set by the user and is the record size from the user' point of view the private binaryrecordfile __record_size is the real record size and includes the state byte we are careful not to truncate the file when we open it if it already exists (by using mode of " + ")and to create it if it does not exist (by using mode of " + ")--the "+part of the mode string is what signifies reading and writing if the binaryrecordfile auto_flush boolean is truethe file is flushed before every read and after every write @property def record_size(self)return self __record_size @property def name(self)return self __fh name def flush(self)self __fh flush(def close(self)self __fh close(we have made the record size and filename into read-only properties the record size we report to the user is the one they requested and matches their records the flush and close methods simply delegate to the file object def __setitem__(selfindexrecord)assert isinstance(record(bytesbytearray))"binary data requiredassert len(record=self record_size"record must be exactly { bytesformatself record_size)self __fh seek(index self __record_sizeself __fh write(_okayself __fh write(recordif self auto_flushself __fh flush(this method supports the brf[idata syntax where brf is binary record filei record index positionand data byte string notice that the record must be the same size as the size is specified when the binary record file was created