plainbox.testing_utils.resource

Implementation of simple resource sharing cache for unit tests

class plainbox.testing_utils.resource.Dict[source]

Bases: dict

A dict() that can be weakly referenced

See: http://docs.python.org/3/library/weakref.html

clear() → None. Remove all items from D.
copy() → a shallow copy of D
fromkeys()

Returns a new dict with keys from iterable and values equal to value.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
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.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from dict/iterable E and F.

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

values() → an object providing a view on D's values
class plainbox.testing_utils.resource.List[source]

Bases: list

A list() that can be weakly referenced

See: http://docs.python.org/3/library/weakref.html

append(object) → None -- append object to end
clear() → None -- remove all items from L
copy() → list -- a shallow copy of L
count(value) → integer -- return number of occurrences of value
extend(iterable) → None -- extend list by appending elements from the iterable
index(value[, start[, stop]]) → integer -- return first index of value.

Raises ValueError if the value is not present.

insert()

L.insert(index, object) – insert object before index

pop([index]) → item -- remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove(value) → None -- remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

L.reverse() – reverse IN PLACE

sort(key=None, reverse=False) → None -- stable sort *IN PLACE*
class plainbox.testing_utils.resource.ResourceCache(weak=True)[source]

Bases: object

Cache for expensive operations.

If your test needs to compute something (slowly) and reuse it in various different test_ methods then this will save time.

static convert_to_weakref_compat(obj)[source]

Convert the passed object to something that can be weakly reachable

get(key, operation)[source]

Get a value from the cache, falling back to computing it if needed

Gets something from the cache dictionary, referenced by the key. If the value is missing it is computed, by calling the operation, and stored in the cache.

comments powered by Disqus