file handling bike _bike_from_record(recordself __index_from_identity[bike identityindex the bikestock bikestock class is custom collection class that aggregates binary record file (self __fileand dictionary (self __index_from_identitywhose keys are bike ids and whose values are record index positions once the file has been opened (and created if it didn' already exist)we iterate over its contents (if anyeach bike is retrieved and converted from bytes object to bikestock bike using the private _bike_from_record(functionand the bike' identity and index are added to the self __index_from_identity dictionary def append(selfbike)index len(self __fileself __file[index_record_from_bike(bikeself __index_from_identity[bike identityindex appending new bike is matter of finding suitable index position and setting the record at that position to the bike' binary representation we also take care to update the self __index_from_identity dictionary def __delitem__(selfidentity)del self __file[self __index_from_identity[identity]deleting bike record is easywe just find its record index position from its identity and delete the record at that index position in the case of the bikestock bikestock class we have not made use of the binaryrecordfile binaryrecordfile' undeletion capability def __getitem__(selfidentity)record self __file[self __index_from_identity[identity]return none if record is none else _bike_from_record(recordbike records are retrieved by bike id if there is no such id the lookup in the self __index_from_identity dictionary will raise keyerror exceptionand if the record is blank or deleted the binaryrecordfile binaryrecordfile will return none but if record is retrieved we return it as bikestock bike object def __change_stock(selfidentityamount)index self __index_from_identity[identityrecord self __file[indexif record is nonereturn false bike _bike_from_record(recordbike quantity +amount self __file[index_record_from_bike(bikereturn true