GeezerVison is showing github.com — do these new results make sense? · benthomasson/deepdiff@40bce63
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: deepdiff/lfucache.py +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | from collections import defaultdict | 8 | 8 | from ordered_set import OrderedSet | 9 | 9 | from threading import Lock | | 10 | +from statistics import mean | 10 | 11 | from deepdiff.helper import not_found, dict_ | 11 | 12 |
| 12 | 13 |
| @@ -201,6 +202,9 @@ def get_sorted_cache_keys(self): | 201 | 202 | result.sort(key=lambda x: -x[1]) | 202 | 203 | return result | 203 | 204 |
| | 205 | + def get_average_frequency(self): | | 206 | + return mean(freq.freq_node.freq for freq in self.cache.values()) | | 207 | + | 204 | 208 |
| 205 | 209 | class DummyLFU: | 206 | 210 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/test_lfucache.py +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 |
| 7 | 7 | class TestLFUcache: | 8 | 8 |
| 9 | | - @pytest.mark.parametrize("items, size, expected_results", [ | 10 | | - (['a', 'a', 'b', 'a', 'c', 'b', 'd'], 3, [('b', 2), ('c', 1), ('d', 1)]), | 11 | | - (['a', 'a', 'b', 'a', 'c', 'b', 'd', 'e', 'c', 'b'], 3, [('b', 3), ('d', 1), ('e', 1)]), | 12 | | - (['a', 'a', 'b', 'a', 'c', 'b', 'd', 'e', 'c', 'b', 'b', 'c', 'd', 'b'], 3, [('b', 5), ('c', 3), ('d', 2)]), | | 9 | + @pytest.mark.parametrize("items, size, expected_results, expected_freq", [ | | 10 | + (['a', 'a', 'b', 'a', 'c', 'b', 'd'], 3, [('b', 2), ('c', 1), ('d', 1)], '1.333'), | | 11 | + (['a', 'a', 'b', 'a', 'c', 'b', 'd', 'e', 'c', 'b'], 3, [('b', 3), ('d', 1), ('e', 1)], '1.666'), | | 12 | + (['a', 'a', 'b', 'a', 'c', 'b', 'd', 'e', 'c', 'b', 'b', 'c', 'd', 'b'], 3, [('b', 5), ('c', 3), ('d', 2)], '3.333'), | 13 | 13 | ]) | 14 | | - def test_lfu(self, items, size, expected_results): | | 14 | + def test_lfu(self, items, size, expected_results, expected_freq): | 15 | 15 | lfucache = LFUCache(size) | 16 | 16 | for item in items: | 17 | 17 | lfucache.set(item, value='{}_cached'.format(item)) | 18 | 18 | for item in items: | 19 | 19 | lfucache.get(item) | 20 | 20 | results = lfucache.get_sorted_cache_keys() | 21 | 21 | assert expected_results == results | | 22 | + freq = lfucache.get_average_frequency() | | 23 | + assert expected_freq == str(freq)[:5] | 22 | 24 |
| 23 | 25 | def test_get_multithreading(self): | 24 | 26 | keys = 'aaaaaaaaaaaaaaaaaaaaaaaaaaabbc' | |
|