def dump(this, filename, gzip=False, lock=None, timeout=None): """ Writes a python object to a file, using python cPickle Supports also '~' or '~user'. @param this: The object, which will be written to disk @type this: Any python object @param filename: Filename of the new file @type filename: String @param gzip: Use gzip to compress the file @type gzip: Boolean @param lock: Use a lockfile to restrict access """ ## check whether file is locked ## file locked? filename = os.path.expanduser(filename) if lock is not None: lockdir = filename + '.lock' if timeout is not None and timeout > 0: end_time = timeout + time.time() while True: try: os.mkdir(lockdir) except OSError as e: # File is already locked if e.errno == errno.EEXIST: if timeout is not None and time.time() > end_time: raise IOError("Failed to acquire Lock") else: raise IOError("Failed to acquire Lock") else: break if gzip: import gzip stream = gzip.GzipFile(filename, 'wb') else: stream = open(filename, 'wb') try: if type(this).__name__ == 'array': import Numeric #@UnresolvedImport p = Numeric.Pickler(stream) p.dump(this) else: Pickle.dump(this, stream, 2) finally: stream.close() if lock is not None: ## release lock try: os.rmdir(lockdir) except: raise IOError('missing lockfile {0}'.format(lockdir))