Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19c0efe

Browse files
committed
just a minor refactoring
1 parent a51d8c4 commit 19c0efe

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

lib/core/bigarray.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@
1313

1414
from lib.core.settings import BIGARRAY_CHUNK_LENGTH
1515

16+
class Cache:
17+
"""
18+
Auxiliary class used for storing cached chunks
19+
"""
20+
21+
def __init__(self, index, data, dirty):
22+
self.index = index
23+
self.data = data
24+
self.dirty = dirty
25+
1626
class BigArray(list):
1727
"""
18-
List-like object used for storing large amounts of data (disk cached)
28+
List-like class used for storing large amounts of data (disk cached)
1929
"""
2030

2131
def __init__(self):
@@ -35,7 +45,7 @@ def append(self, value):
3545
def pop(self):
3646
if len(self.chunks[-1]) < 1:
3747
self.chunks.pop()
38-
with open(self.chunks[-1], 'rb') as fp:
48+
with open(self.chunks[-1], "rb") as fp:
3949
self.chunks[-1] = pickle.load(fp)
4050
return self.chunks[-1].pop()
4151

@@ -49,16 +59,16 @@ def _dump(self, value):
4959
handle, filename = tempfile.mkstemp()
5060
self.filenames.add(filename)
5161
os.close(handle)
52-
with open(filename, 'w+b') as fp:
62+
with open(filename, "w+b") as fp:
5363
pickle.dump(value, fp)
5464
return filename
5565

5666
def _checkcache(self, index):
57-
if (self.cache and self.cache[0] != index and self.cache[2]):
58-
filename = self._dump(self.cache[1])
59-
self.chunks[self.cache[0]] = filename
60-
if not (self.cache and self.cache[0] == index):
61-
with open(self.chunks[index], 'rb') as fp:
67+
if (self.cache and self.cache.index != index and self.cache.dirty):
68+
filename = self._dump(self.cache.data)
69+
self.chunks[self.cache.index] = filename
70+
if not (self.cache and self.cache.index == index):
71+
with open(self.chunks[index], "rb") as fp:
6272
self.cache = (index, pickle.load(fp), False)
6373

6474
def __getitem__(self, y):
@@ -69,7 +79,7 @@ def __getitem__(self, y):
6979
return chunk[offset]
7080
else:
7181
self._checkcache(index)
72-
return self.cache[1][offset]
82+
return self.cache.data[offset]
7383

7484
def __setitem__(self, y, value):
7585
index = y / BIGARRAY_CHUNK_LENGTH
@@ -79,8 +89,8 @@ def __setitem__(self, y, value):
7989
chunk[offset] = value
8090
else:
8191
self._checkcache(index)
82-
self.cache[1][offset] = value
83-
self.cache[2] = True # dirty flag
92+
self.cache.data[offset] = value
93+
self.cache.dirty = True
8494

8595
def __repr__(self):
8696
return "%s%s" % ("..." if len(self.chunks) > 1 else "", self.chunks[-1].__repr__())

0 commit comments

Comments
 (0)