CacheFile

  • full name: tenpy.tools.cache.CacheFile

  • parent module: tenpy.tools.cache

  • type: class

Inheritance Diagram

Inheritance diagram of tenpy.tools.cache.CacheFile

Methods

CacheFile.__init__(storage)

CacheFile.clear()

CacheFile.close()

Close the associated storage container and shut down.

CacheFile.create_subcache(name)

Create another DictCache based on the same storage resource.

CacheFile.get(key[, default])

Same as self[key], but return default if key is not in self.

CacheFile.items()

CacheFile.keys()

CacheFile.open([storage_class, ...])

Interface for opening a Storage and creating a DictCache from it.

CacheFile.pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised.

CacheFile.popitem()

as a 2-tuple; but raise KeyError if D is empty.

CacheFile.preload(*keys[, raise_missing])

Pre-load the data for one or more keys from disk to RAM.

CacheFile.set_short_term_keys(*keys)

Set keys for data which should be kept in RAM for a while.

CacheFile.setdefault(k[,d])

CacheFile.trivial()

Create a trivial storage that keeps everything in RAM.

CacheFile.update([E, ]**F)

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

CacheFile.values()

class tenpy.tools.cache.CacheFile(storage)[source]

Bases: DictCache

Subclass of DictCache to handle opening and closing resources.

You should open this class with the open() method (or trivial()), and make sure that you call close() after usage. The easiest way to ensure this is to use a with statement, see open().

classmethod open(storage_class='Storage', use_threading=False, delete=True, max_queue_size=2, **storage_kwargs)[source]

Interface for opening a Storage and creating a DictCache from it.

Default parameters just give a dummy cache that keeps everything in memory. If you want to activate it to actually save things to disk, we found that the following cache_params parameters worked reasonably well, to be used for the simulation’s see init_cache:

cache_params:
    storage_class: PickleStorage
    use_threading: True  # reduce the OMP_NUM_THREADS if you use this!
    # further specify `directory` or `tmpdir` on the cluster node's local file system

Warning

Make sure that you call the close() method of the returned CacheFile to close opened files and clean up temporary files/directories. One way to ensure this is to use the class in a with statement like this:

with CacheFile.open(...) as cache:
    cache['my_data'] = (1, 2, 3)
    assert cache['my_data'] == (1, 2, 3)
# cache is closed again here, don't use it anymore

The Simulation handles it for you.

Parameters:
  • storage_class (str) – Name for a subclass of Storage to define how data is saved. Use just Storage to keep things in RAM, or, e.g., PickleStorage to actually save things to disk.

  • use_threading (bool) – If True, use the ThreadedStorage wrapper for thread-parallel disk I/O. In that case, you need to use the cache in a with statement (or manually call __enter__() and __exit__()).

  • delete (bool) – If True, delete the opened file/directory after closing the cache.

  • max_queue_size (int) – Only used for use_threading. Needs to be positive to limit the number of environments kept in RAM in case the disk is much slower then the actual update.

  • **storage_kwargs – Further keyword arguments given to the Storage.open() method of the storage_class.

close()[source]

Close the associated storage container and shut down.

clear() None.  Remove all items from D.
create_subcache(name)[source]

Create another DictCache based on the same storage resource.

Uses Storage.subcontainer() to create another storage container for a new DictCache. The data is still completely owned by the top-most Storage (in turn owned by the CacheFile). Hence, closing the parent CacheFile will close all DictCache instances generated with create_subcache; accessing the data is no longer possible afterwards.

Parameters:

name (str) – Name of a subdirectory for the PickleCache or of a hdf5 subgroup for the Hdf5Cache.

Returns:

cache – Another class instance of the same type as self.

Return type:

DictCache

get(key, default=None)[source]

Same as self[key], but return default if key is not in self.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

preload(*keys, raise_missing=False)[source]

Pre-load the data for one or more keys from disk to RAM.

Parameters:
  • *keys (str) – The keys which should be pre-loaded. Are added to the short_term_keys.

  • raise_missing (bool) – Whether to raise a KeyError if a given key does not exist in self.

set_short_term_keys(*keys)[source]

Set keys for data which should be kept in RAM for a while.

Disk input/output is slow, so we want to avoid unnecessary read/write cycles. This method allows to specify keys the data of which should be kept in RAM after setting/ reading, until the keys are updated with the next call to set_short_term_keys(). The data is still written to disk in each self[key] = data, but (subsequent) reading data = self[key] will be fast for the given keys.

Parameters:

*keys (str) – The keys for which data should be kept in RAM for quick short-term lookup.

setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D
classmethod trivial()[source]

Create a trivial storage that keeps everything in RAM.

update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() an object providing a view on D's values