PlasmaCalcs.quantities.caching.VarCache
- class PlasmaCalcs.quantities.caching.VarCache(*, MBmax=None, nmax=None, remove_strategy='used')
Bases:
objectcache of vars. {var: {id: CachedQuantity}.id is quant.id for CachedQuantity quant;it helps when determining which cached quantity to pop, if cache size is limited.Use self.append_cq(var, CachedQuantity) to add a CachedQuantity to this cache.Use self.append(var, value, behavior) to create a CachedQuantity and add it to this cache.Tracks self.order_entered (updated only when entering a new var),and self.order_used (updated when entering OR loading a var).Each of these is an OrderedDict with keys (var, qid) tuples; values None.The ‘latest’ entry is at the end.- MBmax: None or number
- maximum size in MB of this cache. None –> no maximum.if provided along with nmax, the more restrictive one will be used.
- nmax: None or number
- maximum number of entries in this cache. None –> no maximum.if provided along with MBmax, the more restrictive one will be used.
- remove_strategy: ‘used’ or ‘entered’
- whether to remove the oldest entries based on order_used or order_entered.Only applies when cache size exceeds max (determined by MBmax and nmax).
- __init__(*, MBmax=None, nmax=None, remove_strategy='used')
Methods
__init__(*[, MBmax, nmax, remove_strategy])append(var, value, behavior[, ukey])append_cq(var, cached_quantity)clear()get(var, behavior)inverse_lookup(var, behavior, *[, lenient, ...])items()keys()lookup(var, behavior)mark_used(var, quant)pop([itemkey, strategy, _clear_empty_var])pop_used()remove(var, behavior, *[, missing_ok])timing()values()Attributes
- property MB
- alias to size_MB
- append(var, value, behavior, ukey=None, **kw)
- create CachedQuantity(value, behavior, ukey=ukey, **kw), and append it to this cache.
var: str value: any behavior: dict or Behavior
(if dict, internally convert to Behavior.)- ukey: None or str
- if provided, pop ‘units’ from behavior, and ukey is the string to usefor getting the conversion factor for value to another units system.See help(self) for details.
- append_cq(var, cached_quantity)
- add cached_quantity to this cache.if self.nmax or self.MBmax is provided, remove entries until size is below max.if cached_quantity is compatible with any existing cached quantity for var,delete the pre-existing one(s) before adding the new one.
- clear()
- empties the cache of all cached values.
- get(var, behavior)
- return cached value from CachedQuantity with matching behavior, if possible,else raise CacheNotApplicableError.behavior is matching if CachedQuantity.matches_behavior(behavior),i.e. if CachedQuantity.behavior.compatible(behavior, lenient=True).
- inverse_lookup(var, behavior, *, lenient=True, subdims=True, **kw_compatible)
- returns list of quantities from self.cache[var] for which behavior.compatible(quantity.behavior)
- items()
- return items of self.cache.
- iter_quantities()
- iterate over all quantities in this cache.
- keys()
- return keys of self.cache.
- lookup(var, behavior)
- returns (relevance, quant) for cached quantity for var, with quantity.relevant(behavior).returns (False, None) if no quantity.relevant(behavior).else, returns (True, quant) if exact match, or (subdims, quant) if subdim match.subdim match occurs when dim from cached quantity is a value within dim from behaviore.g. fluid=0 in cached quantity, fluid=[0,1,2] in behavior.
- mark_used(var, quant)
- mark (var, quant) as having just been used.This affects bookkeeping, as well as pop order for strategy=’used’, if cache gets too large.
- property n_used
- total number of times a value was loaded from this cache.
- property n_used_here
- total number of times loaded quantities currently in the cache.
- pop(itemkey=None, *, strategy=None, _clear_empty_var=True)
- remove and return an entry from this cache.When called with no arguments, e.g. self.pop(), removes the oldest entry.
- itemkey: None or (var, cached_quantity.id) tuple
- if provided, pop THIS entry from the cache. (self.cache[var][cached_quantity.id])else, pop oldest entry.
- strategy: None, ‘used’, or ‘entered’
- determines meaning of ‘oldest’. If None, use self.remove_strategy.
- _clear_empty_var: bool
- whether to delete self.cache[var] if it becomes empty due to popping this item.
- pop_entered()
- remove and return the oldest entry from this cache, based on self.order_entered.
- pop_used()
- remove and return the oldest entry from this cache, based on self.order_used.
- remove(var, behavior, *, missing_ok=False, **kw)
- remove all quantities for which behavior.compatible(quantity.behavior, lenient=True), for var.
- missing_ok: bool
- if not missing_ok, raise CacheNotApplicableError if there are no relevant quantities.
additional kwargs are passed to behavior.compatible(…, **kw). E.g. could use lenient=False instead.returns list of all removed quantities.
- property size
- number of entries in this cache.
- property size_MB
- total size of this cache, in MB.
- property time_saved
- total time saved by using this cache, in seconds.
- property time_saved_here
- total time saved by quantities which are currently in the cache.
- timing()
- return dict of timing stats.
- values()
- return values of self.cache.