pygsti.baseobjs.smartcache

Defines SmartCache and supporting functions

Module Contents

Classes

SmartCache

Cache object that profiles itself

Functions

smart_cached(obj)

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

digest(obj[, custom_digests])

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

Attributes

DIGEST_TIMES

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

Bases: object

Cache object that profiles itself

Parameters

decoratingtuple

module and function being decorated by the smart cache

Attributes

StaticCacheListlist

A list of all SmartCache instances.

Construct a smart cache object

Parameters

decoratingtuple

module and function being decorated by the smart cache

StaticCacheList = []
add_digest(custom)

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

Parameters
customfunction

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(fn, arg_vals, kwargs=None)

Cached compute with less profiling. See cached_compute() docstring.

Parameters
fnfunction

Cached function

arg_valstuple or list

Arguments to cached function

kwargsdictionary

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(fn, arg_vals, kwargs=None)

Shows effectiveness of a cache

Parameters
fnfunction

Cached function

arg_valstuple or list

Arguments to cached function

kwargsdictionary

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
printerVerbosityPrinter

The printer to use for output.

Returns

None

avg_timedict(d)

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

Parameters
ddict

A dictionary whose values are lists of times.

Returns

dict

status(printer)

Show the status of a cache object instance

Parameters
printerVerbosityPrinter

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

objfunction

function to decorate.

Returns

function

exception pygsti.baseobjs.smartcache.CustomDigestError

Bases: Exception

Custom Digest Exception type

Initialize self. See help(type(self)) for accurate signature.

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

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

Parameters

objobject

Object to digest.

custom_digestslist, 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