MCPcopy Create free account
hub / github.com/numpy/numpy / roundtrip

Method roundtrip

numpy/lib/tests/test_io.py:77–123  ·  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

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