MiscUtils.PickleCache | index /usr/local/share/webware/MiscUtils/PickleCache.py

`PickleCache.py
PickleCache provides tools for keeping fast-loading cached versions of
files so that subsequent loads are faster. This is similar to how Python
silently caches .pyc files next to .py files.
The typical scenario is that you have a type of text file that gets
"translated" to Pythonic data (dictionaries, tuples, instances, ints,
etc.). By caching the Python data on disk in pickle format, you can
avoid the expensive translation on subsequent reads of the file.
Two real life cases are MiscUtils.DataTable, which loads and represents
comma-separated files, and MiddleKit which has an object model file.
So for examples on using this module, load up the following files and
search for "Pickle":
Webware/MiscUtils/DataTable.py
MiddleKit/Core/Model.py
The cached file is named the same as the original file with
'.pickle.cache' suffixed. The utility of '.pickle' is to denote the file
format and the utilty of '.cache' is to provide '*.cache' as a simple
pattern that can be removed, ignored by backup scripts, etc.
The treatment of the cached file is silent and friendly just like
Python's approach to .pyc files. If it cannot be read or written for
various reasons (cache is out of date, permissions are bad, wrong python
version, etc.), then it will be silently ignored.
GRANULARITY
In constructing the test suite, I discovered that if the source file is
newly written less than 1 second after the cached file, then the fact
that the source file is newer will not be detected and the cache will
still be used. I believe this is a limitation of the granularity of
os.path.getmtime(). If anyone knows of a more granular solution, please
let me know.
This would only be a problem in programmatic situations where the source
file was rapidly being written and read. I think that's fairly rare.
SEE ALSO
http://www.python.org/doc/current/lib/module-pickle.html`

Modules

``

os | sys |  |

Classes

``

__builtin__.object
  PickleCache
    PickleCacheReader
    PickleCacheWriter

class PickleCache(__builtin__.object)
`` | `Simple abstract base class for PickleCacheReader and PickleCacheWriter.`

Methods defined here:

picklePath(self, filename)

Data descriptors defined here:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

class PickleCacheReader(PickleCache)

``

Method resolution order:
  PickleCacheReader
  PickleCache
  __builtin__.object

Methods defined here:

read(self, filename, pickleProtocol=None, source=None, verbose=None)
  `Read data from pickle cache.
  Returns the data from the pickle cache version of the filename,
  if it can read. Otherwise returns None, which also indicates
  that writePickleCache() should be subsequently called after
  the original file is read.`

Methods inherited from PickleCache:

picklePath(self, filename)

Data descriptors inherited from PickleCache:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

class PickleCacheWriter(PickleCache)

``

Method resolution order:
  PickleCacheWriter
  PickleCache
  __builtin__.object

Methods defined here:

write(self, data, filename, pickleProtocol=None, source=None, verbose=None)

Methods inherited from PickleCache:

picklePath(self, filename)

Data descriptors inherited from PickleCache:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

Functions

``

dump(...)
  `dump(obj, file, protocol=0) -- Write an object in pickle format to the given file.
  See the Pickler docstring for the meaning of optional argument proto.`

load(...)
  `load(file) -- Load a pickle from the given file`

readPickleCache = read(self, filename, pickleProtocol=None, source=None, verbose=None) method of PickleCacheReader instance
  `Read data from pickle cache.
  Returns the data from the pickle cache version of the filename,
  if it can read. Otherwise returns None, which also indicates
  that writePickleCache() should be subsequently called after
  the original file is read.`

sleep(...)
  `sleep(seconds)
  Delay execution for a given number of seconds. The argument may be
  a floating point number for subsecond precision.`

writePickleCache = write(self, data, filename, pickleProtocol=None, source=None, verbose=None) method of PickleCacheWriter instance

Data
`` |  | maxPickleProtocol = 2 verbose = False versionInfo = (2, 7, 5, 'final', 0)