Skip to content

Commit c46d9ee

Browse files
author
Oliver Scott
committed
Moved Cache to utils.
1 parent c0f3aef commit c46d9ee

2 files changed

Lines changed: 63 additions & 57 deletions

File tree

scaffoldgraph/analysis/representation.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,12 @@
77
"""
88

99
from networkx import set_node_attributes
10-
from collections import OrderedDict
1110
from itertools import combinations
12-
from operator import eq as _eq
1311

1412
from rdkit import DataStructs
1513
from rdkit import Chem
1614

17-
18-
class Cache(OrderedDict):
19-
"""A basic implementation of an LRU cache using OrderedDict.
20-
21-
Adapted (slightly) from the collections ``OrderedDict``
22-
documentation.
23-
24-
.. _collections OrderedDict Documentation:
25-
https://docs.python.org/3/library/collections.html#collections.OrderedDict
26-
27-
"""
28-
def __init__(self, maxsize=None, *args, **kwargs):
29-
"""
30-
Parameters
31-
----------
32-
maxsize : int, None, optional
33-
Set the maximum size of the cache, if None the cache
34-
has no size limitation. The default is None.
35-
*args
36-
Variable length argument list.
37-
Passed to OrderedDict.
38-
**kwargs
39-
Arbitrary keyword arguments.
40-
Passed to OrderedDict.
41-
42-
"""
43-
self._maxsize = maxsize
44-
super(Cache, self).__init__(*args, **kwargs)
45-
46-
@property
47-
def maxsize(self):
48-
"""int: The maximum size of the cache."""
49-
return self._maxsize
50-
51-
def __getitem__(self, key):
52-
value = super().__getitem__(key)
53-
self.move_to_end(key)
54-
return value
55-
56-
def __setitem__(self, key, value):
57-
super().__setitem__(key, value)
58-
if self.maxsize and len(self) > self.maxsize:
59-
oldest = next(iter(self))
60-
del self[oldest]
61-
62-
def __eq__(self, other):
63-
if isinstance(other, Cache):
64-
return dict.__eq__(self, other) and all(map(_eq, self, other))
65-
return dict.__eq__(self, other)
66-
67-
def __repr__(self):
68-
return '{}(maxsize={})'.format(
69-
self.__class__.__name__,
70-
self.maxsize
71-
)
15+
from scaffoldgraph.utils.cache import Cache
7216

7317

7418
class MolecularSimilarityCache(object):

scaffoldgraph/utils/cache.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
scaffoldgraph.utils.cache
3+
"""
4+
5+
from collections import OrderedDict
6+
from operator import eq as _eq
7+
8+
9+
class Cache(OrderedDict):
10+
"""A basic implementation of an LRU cache using OrderedDict.
11+
12+
Adapted (slightly) from the collections ``OrderedDict``
13+
documentation.
14+
15+
.. _collections OrderedDict Documentation:
16+
https://docs.python.org/3/library/collections.html#collections.OrderedDict
17+
18+
"""
19+
def __init__(self, maxsize=None, *args, **kwargs):
20+
"""
21+
Parameters
22+
----------
23+
maxsize : int, None, optional
24+
Set the maximum size of the cache, if None the cache
25+
has no size limitation. The default is None.
26+
*args
27+
Variable length argument list.
28+
Passed to OrderedDict.
29+
**kwargs
30+
Arbitrary keyword arguments.
31+
Passed to OrderedDict.
32+
33+
"""
34+
self._maxsize = maxsize
35+
super(Cache, self).__init__(*args, **kwargs)
36+
37+
@property
38+
def maxsize(self):
39+
"""int: The maximum size of the cache."""
40+
return self._maxsize
41+
42+
def __getitem__(self, key):
43+
value = super().__getitem__(key)
44+
self.move_to_end(key)
45+
return value
46+
47+
def __setitem__(self, key, value):
48+
super().__setitem__(key, value)
49+
if self.maxsize and len(self) > self.maxsize:
50+
oldest = next(iter(self))
51+
del self[oldest]
52+
53+
def __eq__(self, other):
54+
if isinstance(other, Cache):
55+
return dict.__eq__(self, other) and all(map(_eq, self, other))
56+
return dict.__eq__(self, other)
57+
58+
def __repr__(self):
59+
return '{}(maxsize={})'.format(
60+
self.__class__.__name__,
61+
self.maxsize
62+
)

0 commit comments

Comments
 (0)