Skip to content

Commit b237fb3

Browse files
better doctests
1 parent fc53f3b commit b237fb3

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

Diff for: other/lru_cache_pythonic.py

+37-30
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,51 @@
66
the Python dictionary maintains the order of insertion of keys and ensures O(1) operations on insert, delete and access.
77
'''
88
class LRUCache(dict):
9-
'''
10-
Initialize an LRU Cache with given capacity.
11-
capacity : int -> the capacity of the LRU Cache
12-
'''
139
def __init__(self, capacity : int)->None:
10+
'''
11+
Initialize an LRU Cache with given capacity.
12+
capacity : int -> the capacity of the LRU Cache
13+
>>> cache = LRUCache(2)
14+
>>> cache
15+
{}
16+
'''
1417
self.remaining:int = capacity
1518

16-
'''
17-
This method gets the value associated with the key.
18-
key : Hashable -> a hashable object that is mapped to a value inside of the LRU cache.
19-
returns -> value : Any -> any object that is stored as a value inside of the LRU cache.
20-
21-
>>> cache = LRUCache(2)
22-
>>> cache.put(1,1)
23-
>>> cache.get(1)
24-
1
25-
>>> cache.get(2)
26-
KeyError: '2 not found.'
27-
'''
2819
def get(self, key:Hashable)->Any:
20+
'''
21+
This method gets the value associated with the key.
22+
key : Hashable -> a hashable object that is mapped to a value inside of the LRU cache.
23+
returns -> value : Any -> any object that is stored as a value inside of the LRU cache.
24+
25+
>>> cache = LRUCache(2)
26+
>>> cache.put(1,1)
27+
>>> cache.get(1)
28+
1
29+
>>> cache.get(2)
30+
Traceback (most recent call last):
31+
...
32+
KeyError: '2 not found.'
33+
'''
2934
if key not in self:
3035
raise KeyError(f"{key} not found.")
3136
val = self.pop(key) # Pop the key-value and re-insert to maintain the order
3237
self[key] = val
3338
return val
3439

35-
'''
36-
This method puts the value associated with the key provided inside of the LRU cache.
37-
key : Hashable -> a hashable object that is mapped to a value inside of the LRU cache.
38-
value: Any -> any object that is to be associated with the key inside of the LRU cache.
39-
>>> cache = LRUCache(2)
40-
>>> cache.put(3,3)
41-
>>> cache
42-
{3:3}
43-
>>> cache.put(2,2)
44-
>>> cache
45-
{3:3, 2:2}
46-
'''
40+
4741
def put(self, key:Hashable, value:Any)->None:
42+
'''
43+
This method puts the value associated with the key provided inside of the LRU cache.
44+
key : Hashable -> a hashable object that is mapped to a value inside of the LRU cache.
45+
value: Any -> any object that is to be associated with the key inside of the LRU cache.
46+
>>> cache = LRUCache(2)
47+
>>> cache.put(3,3)
48+
>>> cache
49+
{3: 3}
50+
>>> cache.put(2,2)
51+
>>> cache
52+
{3: 3, 2: 2}
53+
'''
4854
# To pop the last value inside of the LRU cache
4955
if key in self:
5056
self.pop(key)
@@ -85,9 +91,10 @@ def main()->None:
8591
except KeyError:
8692
print("Key not found in cache")
8793

88-
import doctest
89-
doctest.testmod()
94+
9095

9196
if __name__ == '__main__':
97+
import doctest
98+
doctest.testmod()
9299
main()
93100

0 commit comments

Comments
 (0)