MCPcopy
hub / github.com/numpy/numpy / roundtrip

Method roundtrip

numpy/lib/tests/test_io.py:86–132  ·  view source on GitHub ↗

save_func : callable Function used to save arrays to file. file_on_disk : bool If true, store the file on disk, instead of in a string buffer. save_kwds : dict Parameters passed to `save_func`. load_kwds : dict

(self, save_func, *args, **kwargs)

Source from the content-addressed store, hash-verified

84
85class RoundtripTest:
86 def roundtrip(self, save_func, *args, **kwargs):
87 """
88 save_func : callable
89 Function used to save arrays to file.
90 file_on_disk : bool
91 If true, store the file on disk, instead of in a
92 string buffer.
93 save_kwds : dict
94 Parameters passed to `save_func`.
95 load_kwds : dict
96 Parameters passed to `numpy.load`.
97 args : tuple of arrays
98 Arrays stored to file.
99
100 """
101 save_kwds = kwargs.get('save_kwds', {})
102 load_kwds = kwargs.get('load_kwds', {"allow_pickle": True})
103 file_on_disk = kwargs.get('file_on_disk', False)
104
105 if file_on_disk:
106 target_file = NamedTemporaryFile(delete=False)
107 load_file = target_file.name
108 else:
109 target_file = BytesIO()
110 load_file = target_file
111
112 try:
113 arr = args
114
115 save_func(target_file, *arr, **save_kwds)
116 target_file.flush()
117 target_file.seek(0)
118
119 if sys.platform == 'win32' and not isinstance(target_file, BytesIO):
120 target_file.close()
121
122 arr_reloaded = np.load(load_file, **load_kwds)
123
124 finally:
125 if not isinstance(target_file, BytesIO):
126 target_file.close()
127 # holds an open file descriptor so it can't be deleted on win
128 if 'arr_reloaded' in locals():
129 if not isinstance(arr_reloaded, np.lib.npyio.NpzFile):
130 os.remove(target_file.name)
131
132 return arr, arr_reloaded
133
134 def check_roundtrips(self, a):
135 self.roundtrip(a)

Callers 3

check_roundtripsMethod · 0.95
test_1DMethod · 0.95
test_mmapMethod · 0.95

Calls 4

seekMethod · 0.80
getMethod · 0.45
flushMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected