file handling if the arguments are okaywe move the file position pointer to the first byte of the record--notice that here we use the real record sizethat iswe account for the state byte the seek(method moves the file pointer to an absolute byte position by default second argument can be given to make the movement relative to the current position or to the end (the attributes and methods provided by file objects are listed in tables and since the item is being set it obviously hasn' been deletedso we write the _okay state byteand then we write the user' binary record data the binary record file does not know or care about the record structure that is being used--only that records are of the right size we do not check whether the index is in range if the index is beyond the end of the file the record will be written in the correct position and every byte between the previous end of the file and the new record will automatically be set to "\ such blank records are neither _okay nor _deletedso we can distinguish them when we need to def __getitem__(selfindex)self __seek_to_index(indexstate self __fh read( if state !_okayreturn none return self __fh read(self record_sizewhen retrieving record there are four cases that we must account forthe record doesn' existthat isthe given index is beyond the endthe record is blankthe record has been deletedand the record is okay if the record doesn' exist the private __seek_to_index(method will raise an indexerror exception otherwiseit will seek to the byte where the record begins and we can read the state byte if the state is not _okay the record must either be blank or be deletedin which case we return noneotherwisewe read and return the record (another strategy would be to raise custom exception for blank or deleted recordssayblankrecorderror or deletedrecorderrorinstead of returning none def __seek_to_index(selfindex)if self auto_flushself __fh flush(self __fh seek( os seek_endend self __fh tell(offset index self __record_size if offset >endraise indexerror("no record at index position { }formatindex)self __fh seek(offset