pygsti.baseobjs.smartcache

Defines SmartCache and supporting functions

Module Contents

Classes

SmartCache

Cache object that profiles itself

Functions

_csize(counter)

Computes the size of (number of elements in) a given Counter.

_average(l)

Computes the _average of the items in a list

_show_cache_percents(hits, misses, printer)

Shows effectiveness of a cache

_show_kvs(title, kvs, printer)

Pretty-print key-value pairs w/ a title and printer object

smart_cached(obj)

Decorator for applying a smart cache to a single function or method.

digest(obj, custom_digests=None)

Returns an MD5 digest of an arbitary Python object, obj.

_get_fn_name_key(fn)

Get the name (str) used to hash the function fn

_call_key(fn, args, custom_digests)

Returns a hashable key for caching the result of a function call.

Attributes

DIGEST_TIMES

pygsti.baseobjs.smartcache.DIGEST_TIMES
pygsti.baseobjs.smartcache._csize(counter)

Computes the size of (number of elements in) a given Counter.

Parameters

counter (Counter) – The counter to get the size of.

Returns

int

pygsti.baseobjs.smartcache._average(l)

Computes the _average of the items in a list

Parameters

l (list) – the list

Returns

int

pygsti.baseobjs.smartcache._show_cache_percents(hits, misses, printer)

Shows effectiveness of a cache

Parameters
  • hits (Counter) – cache hits

  • misses (Counter) – cache misses

  • printer (pygsti.objects.VerbosityPrinter) – logging object

Returns

None

pygsti.baseobjs.smartcache._show_kvs(title, kvs, printer)

Pretty-print key-value pairs w/ a title and printer object

Parameters
  • title (str) – Title to print

  • kvs (iterable) – An object that yields (key, value) pairs when you iterate it.

  • printer (VerbosityPrinter) – The printer object.

Returns

None

class pygsti.baseobjs.smartcache.SmartCache(decorating=(None, None))

Bases: object

Cache object that profiles itself

Parameters

decorating (tuple) – module and function being decorated by the smart cache

StaticCacheList

A list of all SmartCache instances.

Type

list

StaticCacheList = []
__setstate__(self, d)
__getstate__(self)
__pygsti_getstate__(self)
add_digest(self, custom)

Add a “custom” digest function, used for hashing otherwise un-hashable types.

Parameters

custom (function) – A hashing function, which takes two arguments: md5 (a running MD5 hash) and val (the value to be hashed). It should call md5.update to add to the running hash, and needn’t return anything.

Returns

None

low_overhead_cached_compute(self, fn, arg_vals, kwargs=None)

Cached compute with less profiling. See :method:`cached_compute` docstring.

Parameters
  • fn (function) – Cached function

  • arg_vals (tuple or list) – Arguments to cached function

  • kwargs (dictionary) – Keyword arguments to cached function

Returns

  • key (the key used to hash the function call)

  • result (result of fn called with arg_vals and kwargs)

cached_compute(self, fn, arg_vals, kwargs=None)

Shows effectiveness of a cache

Parameters
  • fn (function) – Cached function

  • arg_vals (tuple or list) – Arguments to cached function

  • kwargs (dictionary) – Keyword arguments to cached function

Returns

  • key (the key used to hash the function call)

  • result (result of fn called with arg_vals and kwargs)

static global_status(printer)

Show the statuses of all Cache objects

Parameters

printer (VerbosityPrinter) – The printer to use for output.

Returns

None

avg_timedict(self, d)

Given a dictionary of lists of times (d), returns a dict of the summed times.

Parameters

d (dict) – A dictionary whose values are lists of times.

Returns

dict

status(self, printer)

Show the status of a cache object instance

Parameters

printer (VerbosityPrinter) – The printer to use for output.

Returns

None

pygsti.baseobjs.smartcache.smart_cached(obj)

Decorator for applying a smart cache to a single function or method.

Parameters

obj (function) – function to decorate.

Returns

function

exception pygsti.baseobjs.smartcache.CustomDigestError

Bases: Exception

Custom Digest Exception type

pygsti.baseobjs.smartcache.digest(obj, custom_digests=None)

Returns an MD5 digest of an arbitary Python object, obj.

Parameters
  • obj (object) – Object to digest.

  • custom_digests (list, optional) – A list of custom digest functions. Each function should have the signature digest(md5 : hashlib.md5, value) and either digest value (calling md5.update or similar) or raise a CustomDigestError to indicate it was unable to digest value.

Returns

MD5_digest

pygsti.baseobjs.smartcache._get_fn_name_key(fn)

Get the name (str) used to hash the function fn

Parameters

fn (function) – The function to get the hash key for.

Returns

str

pygsti.baseobjs.smartcache._call_key(fn, args, custom_digests)

Returns a hashable key for caching the result of a function call.

Parameters
  • fn (function) – The function itself

  • args (list or tuple) – The function’s arguments.

  • custom_digests (list, optional) – A list of custom digest functions. Each function should have the signature digest(md5 : hashlib.md5, value) and either digest value (calling md5.update or similar) or raise a CustomDigestError to indicate it was unable to digest value.

Returns

tuple