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

Blogs

Check out the latest blogs from LambdaTest on this topic:

30 Top Automation Testing Tools In 2022

The sky’s the limit (and even beyond that) when you want to run test automation. Technology has developed so much that you can reduce time and stay more productive than you used to 10 years ago. You needn’t put up with the limitations brought to you by Selenium if that’s your go-to automation testing tool. Instead, you can pick from various test automation frameworks and tools to write effective test cases and run them successfully.

Introducing LambdaTest Analytics: Test Reporting Made Awesome ????

Collecting and examining data from multiple sources can be a tedious process. The digital world is constantly evolving. To stay competitive in this fast-paced environment, businesses must frequently test their products and services. While it’s easy to collect raw data from multiple sources, it’s far more complex to interpret it properly.

How to Position Your Team for Success in Estimation

Estimates are critical if you want to be successful with projects. If you begin with a bad estimating approach, the project will almost certainly fail. To produce a much more promising estimate, direct each estimation-process issue toward a repeatable standard process. A smart approach reduces the degree of uncertainty. When dealing with presales phases, having the most precise estimation findings can assist you to deal with the project plan. This also helps the process to function more successfully, especially when faced with tight schedules and the danger of deviation.

Why Selenium WebDriver Should Be Your First Choice for Automation Testing

Developed in 2004 by Thoughtworks for internal usage, Selenium is a widely used tool for automated testing of web applications. Initially, Selenium IDE(Integrated Development Environment) was being used by multiple organizations and testers worldwide, benefits of automation testing with Selenium saved a lot of time and effort. The major downside of automation testing with Selenium IDE was that it would only work with Firefox. To resolve the issue, Selenium RC(Remote Control) was used which enabled Selenium to support automated cross browser testing.

Options for Manual Test Case Development &#038; Management

The purpose of developing test cases is to ensure the application functions as expected for the customer. Test cases provide basic application documentation for every function, feature, and integrated connection. Test case development often detects defects in the design or missing requirements early in the development process. Additionally, well-written test cases provide internal documentation for all application processing. Test case development is an important part of determining software quality and keeping defects away from customers.

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