How to use on_evict method in hypothesis

Best Python code snippet using hypothesis

lru.py

Source:lru.py Github

copy

Full Screen

1from __future__ import absolute_import, division, print_function2from heapdict import heapdict3from .common import ZictBase, close4def do_nothing(k, v):5 pass6class LRU(ZictBase):7 """ Evict Least Recently Used Elements8 Parameters9 ----------10 n: int11 Number of elements to keep, or total weight if weight= is used12 d: MutableMapping13 Dictionary in which to hold elements14 on_evict: list of callables15 Function:: k, v -> action to call on key value pairs prior to eviction16 weight: callable17 Function:: k, v -> number to determine the size of keeping the item in18 the mapping. Defaults to ``(k, v) -> 1``19 Examples20 --------21 >>> lru = LRU(2, dict(), on_evict=lambda k, v: print("Lost", k, v))22 >>> lru['x'] = 123 >>> lru['y'] = 224 >>> lru['z'] = 325 Lost x 126 """27 def __init__(self, n, d, on_evict=None, weight=lambda k, v: 1):28 self.d = d29 self.n = n30 self.heap = heapdict()31 self.i = 032 if callable(on_evict):33 on_evict = [on_evict]34 self.on_evict = on_evict or []35 self.weight = weight36 self.total_weight = 037 self.weights = dict()38 def __getitem__(self, key):39 result = self.d[key]40 self.i += 141 self.heap[key] = self.i42 return result43 def __setitem__(self, key, value):44 if key in self.d:45 del self[key]46 weight = self.weight(key, value)47 if weight <= self.n:48 self.d[key] = value49 self.i += 150 self.heap[key] = self.i51 self.weights[key] = weight52 self.total_weight += weight53 else:54 for cb in self.on_evict:55 cb(key, value)56 while self.total_weight > self.n:57 self.evict()58 def evict(self):59 """ Evict least recently used key60 This is typically called from internal use, but can be externally61 triggered as well.62 Returns63 -------64 k: key65 v: value66 w: weight67 """68 k, priority = self.heap.popitem()69 weight = self.weights.pop(k)70 self.total_weight -= weight71 v = self.d.pop(k)72 for cb in self.on_evict:73 cb(k, v)74 return k, v, weight75 def __delitem__(self, key):76 del self.d[key]77 del self.heap[key]78 self.total_weight -= self.weights.pop(key)79 def keys(self):80 return self.d.keys()81 def values(self):82 return self.d.values()83 def items(self):84 return self.d.items()85 def __len__(self):86 return len(self.d)87 def __iter__(self):88 return iter(self.d)89 def __contains__(self, key):90 return key in self.d91 def __str__(self):92 sub = str(self.d) if not isinstance(self.d, dict) else 'dict'93 return '<LRU: %s/%s on %s>' % (self.total_weight, self.n, sub)94 __repr__ = __str__95 def flush(self):96 self.d.flush()97 def close(self):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful