def check_all_valid_str_keys(tp, option_keywords): options = hdf5storage.Options(**option_keywords) key_value_names = (options.dict_like_keys_name, options.dict_like_values_name) data = random_dict(tp) for k in key_value_names: if k in data: del data[k] # Make a random name. name = random_name() # Write the data to the proper file with the given name with the # provided options. The file needs to be deleted after to keep junk # from building up. fld = None try: fld = tempfile.mkstemp() os.close(fld[0]) filename = fld[1] hdf5storage.write(data, path=name, filename=filename, options=options) with h5py.File(filename, mode='r') as f: for k in key_value_names: assert escape_path(k) not in f[name] for k in data: assert escape_path(k) in f[name] except: raise finally: if fld is not None: os.remove(fld[1])
def check_dict_like_key_leading_periods(self, tp): data = random_dict(tp) prefix = '.' * random.randint(1, 10) key = prefix + random_str_ascii(max_dict_key_length) data[key] = random_int() out = self.write_readback(data, random_name(), self.options) self.assert_equal(out, data)
def check_dict_like_key_back_slash(self, tp): data = random_dict(tp) ch = '\\' key = ch.join( [random_str_ascii(max_dict_key_length) for i in range(2)]) data[key] = random_int() out = self.write_readback(data, random_name(), self.options) self.assert_equal(out, data)
def check_dict_like_other_type_key(self, tp, other_tp): data = random_dict(tp) key_gen = random_str_some_unicode(max_dict_key_length) if other_tp == 'numpy.bytes_': key = np.bytes_(key_gen.encode('UTF-8')) elif other_tp == 'numpy.unicode_': key = np.unicode_(key_gen) elif other_tp == 'bytes': key = key_gen.encode('UTF-8') elif other_tp == 'int': key = random_int() elif other_tp == 'float': key = random_float() data[key] = random_int() out = self.write_readback(data, random_name(), self.options) self.assert_equal(out, data)
def check_string_type_non_str_key(tp, other_tp, option_keywords): options = hdf5storage.Options(**option_keywords) key_value_names = (options.dict_like_keys_name, options.dict_like_values_name) data = random_dict(tp) for k in key_value_names: if k in data: del data[k] keys = list(data.keys()) key_gen = random_str_some_unicode(max_dict_key_length) if other_tp == 'numpy.bytes_': key = np.bytes_(key_gen.encode('UTF-8')) elif other_tp == 'numpy.unicode_': key = np.unicode_(key_gen) elif other_tp == 'bytes': key = key_gen.encode('UTF-8') data[key] = random_int() keys.append(key_gen) # Make a random name. name = random_name() # Write the data to the proper file with the given name with the # provided options. The file needs to be deleted after to keep junk # from building up. fld = None try: fld = tempfile.mkstemp() os.close(fld[0]) filename = fld[1] hdf5storage.write(data, path=name, filename=filename, options=options) with h5py.File(filename, mode='r') as f: assert_equal_nose(set(keys), set(f[name].keys())) except: raise finally: if fld is not None: os.remove(fld[1])
def check_str_key_previously_invalid_char(tp, ch, option_keywords): options = hdf5storage.Options(**option_keywords) key_value_names = (options.dict_like_keys_name, options.dict_like_values_name) data = random_dict(tp) for k in key_value_names: if k in data: del data[k] # Add a random invalid str key using the provided character key = key_value_names[0] while key in key_value_names: key = ch.join( [random_str_ascii(max_dict_key_length) for i in range(2)]) data[key] = random_int() # Make a random name. name = random_name() # Write the data to the proper file with the given name with the # provided options. The file needs to be deleted after to keep junk # from building up. fld = None try: fld = tempfile.mkstemp() os.close(fld[0]) filename = fld[1] hdf5storage.write(data, path=name, filename=filename, options=options) with h5py.File(filename, mode='r') as f: for k in key_value_names: assert escape_path(k) not in f[name] for k in data: assert escape_path(k) in f[name] except: raise finally: if fld is not None: os.remove(fld[1])
def check_dict_like(self, tp): data = random_dict(tp) out = self.write_readback(data, random_name(), self.options) self.assert_equal(out, data)