-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathnpytar.py
57 lines (42 loc) · 1.25 KB
/
npytar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import cStringIO as StringIO
import tarfile
import time
import zlib
import numpy as np
PREFIX = 'data/'
SUFFIX = '.npy.z'
class NpyTarWriter(object):
def __init__(self, fname):
self.tfile = tarfile.open(fname, 'w|')
def add(self, arr, name):
sio = StringIO.StringIO()
np.save(sio, arr)
zbuf = zlib.compress(sio.getvalue())
sio.close()
zsio = StringIO.StringIO(zbuf)
tinfo = tarfile.TarInfo('{}{}{}'.format(PREFIX, name, SUFFIX))
tinfo.size = len(zbuf)
tinfo.mtime = time.time()
zsio.seek(0)
self.tfile.addfile(tinfo, zsio)
zsio.close()
def close(self):
self.tfile.close()
class NpyTarReader(object):
def __init__(self, fname):
self.tfile = tarfile.open(fname, 'r|')
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
entry = self.tfile.next()
if entry is None:
raise StopIteration()
name = entry.name[len(PREFIX):-len(SUFFIX)]
fileobj = self.tfile.extractfile(entry)
buf = zlib.decompress(fileobj.read())
arr = np.load(StringIO.StringIO(buf))
return arr, name
def close(self):
self.tfile.close()