Spaces:
Running
on
T4
Running
on
T4
Create file_handler.py
Browse files- file_handler.py +38 -0
file_handler.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class File:
|
2 |
+
def __init__(self, path):
|
3 |
+
self.path = path
|
4 |
+
|
5 |
+
def exists(self):
|
6 |
+
return os.path.exists(self.path)
|
7 |
+
|
8 |
+
def save(self, data):
|
9 |
+
raise NotImplementedError
|
10 |
+
|
11 |
+
def load(self):
|
12 |
+
raise NotImplementedError
|
13 |
+
|
14 |
+
class MsgpackFile(File):
|
15 |
+
def save(self, data):
|
16 |
+
with open(self.path, "wb") as file:
|
17 |
+
packed = msgpack.packb(data, use_bin_type=True)
|
18 |
+
file.write(packed)
|
19 |
+
|
20 |
+
def load(self):
|
21 |
+
with open(self.path, "rb") as file:
|
22 |
+
byte_data = file.read()
|
23 |
+
return msgpack.unpackb(byte_data, raw=False)
|
24 |
+
|
25 |
+
class DenseArrayNumpyFile(File):
|
26 |
+
def save(self, data):
|
27 |
+
np.savez_compressed(self.path, *data)
|
28 |
+
|
29 |
+
def load(self):
|
30 |
+
return list(np.load(self.path).values())
|
31 |
+
|
32 |
+
class SparseArrayScipyFile(File):
|
33 |
+
def save(self, sparse_matrices):
|
34 |
+
combined_sparse_matrix = vstack(sparse_matrices)
|
35 |
+
save_npz(self.path, combined_sparse_matrix)
|
36 |
+
|
37 |
+
def load(self):
|
38 |
+
return load_npz(self.path)
|