2 from struct
import unpack, calcsize
7 from collections
import OrderedDict
15 This package provides a wrapper for the binary and xdmf output. 17 In order to load some data. 23 def __init__(self, filename, usecache=False, mode='r'):
24 if isinstance(filename, (tuple, list)):
27 raise IOError(
"No file given!")
31 raise IOError(
"No file found, invalid pattern '%s'" % filename)
34 if not os.path.isfile(file):
35 raise IOError(
"Not a file/missing: '%s'" % file)
77 if not ((0 <= n)
and (n < self.
N)):
79 if n
not in self.
files:
80 preload = [k
for k, v
in list(self.
stats.items())
if v > 0]
93 delta = n - self.
frame 96 for i
in itertools.count(n, delta):
106 "File with number %i not in allowed range [0,%i]." %
116 return [0, self.
N - 1]
122 raise Exception(
"Write by getting a reference to the object first!")
125 return k
in list(self.
keys())
127 def get(self, k, default=None):
131 return self.
data[k].T
133 if default
is not None:
135 print(
"Available keys:")
136 print((
'\n'.join(list(self.
keys()))))
137 raise Exception(
"The key '%s' could not be found." % k)
155 """ Reads out the header of the binary file. 157 The structure of the fixed size binary data is written down 163 3:
lambda d:
"S%i" % d,
178 f = mmap.mmap(m.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
180 = f.read(6), f.read(2), f.read(1), f.read(2), f.read(2)
190 while len(keylen) == 4:
191 keylen, = unpack(
'%si' % self.
endian, keylen)
192 key, t, datalen = unpack(
194 (self.
endian, keylen), f.read(
196 key = key.decode(
"utf-8")
199 if t
in [6, 7, 8, 12]:
200 translate = {6: 3, 7: 3, 8: 4, 12: 5}
214 self.
dtype[key] = (self.
dtypes[t](dims), np.int64(offset))
215 f.seek(datalen, os.SEEK_CUR)
221 for key, value
in self.
dtype.items():
222 dtype, offset = value
224 dtscalar = np.dtype(dtype.split(
')')[-1])
234 def __init__(self, maxitems, max_workers=None, *args, **kwargs):
235 super(cache, self).
__init__(*args, **kwargs)
237 self.
pool = futures.ThreadPoolExecutor(max_workers=max_workers)
242 super(cache, self).popitem(
False)
243 fut = self.
pool.submit(val)
248 return super(cache, self).
__getitem__(key).result()
251 self.
pool.shutdown(
False)
255 f = np.memmap(fname, dtype=dtype, mode=
'r') 259 if preload
is not None:
268 cache = obj.cache = {}
270 @functools.wraps(obj)
271 def memoizer(*args, **kwargs):
272 key = str(args) + str(kwargs)
274 cache[key] = obj(*args, **kwargs)
def __setitem__(self, key, val)
type(dict_typ) function, pointer, public dict(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16, n17, n18, n19, n20)
Construct a new dictionary from several key/value pairs. Together with the Assign subroutine and over...
def __setitem__(self, key, val)
def __contains__(self, k)
def __init__(self, filename, usecache=False, mode='r')
def __getitem__(self, key)
def memopen(fname, dtype, preload=None)
def get(self, k, default=None)
def __init__(self, maxitems, max_workers=None, args, kwargs)