def test_str_ascii_encoded_utf8(self): ltrs = string.ascii_letters + string.digits data = 'a' while all([(c in ltrs) for c in data]): data = random_str_some_unicode(random.randint(1, \ max_string_length)) data = data.encode('utf-8') 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 make_str_for_esc(include_escapes=None, include_leading_periods=False, no_unicode=False, pack_digits=True): sl = list(random_str_ascii(10)) if not no_unicode: sl += list(random_str_some_unicode(10)) if pack_digits: chars = b'0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F' sl += chars.decode('ascii').split(b' '.decode('ascii')) * 10 sl += [period] * 10 if include_escapes is not None: for c in include_escapes: sl += [c] * 3 random.shuffle(sl) s = b''.decode('ascii').join(sl).lstrip(period) if include_leading_periods: s = period * random.randint(1, 10) + s return s
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 test_dtype_structured_with_offsets_titles(self): base_dtypes = tuple((set(self.dtypes) | {'U2', 'S3'}) - {'U', 'S'}) for i in range(10): names = [] for _ in range(random.randint(1, 5)): s = random_str_ascii(random.randint(1, 10)) while s in names and s[0].isdigit(): s = random_str_ascii(random.randint(1, 10)) names.append(s) formats = [(random.choice(base_dtypes), random_numpy_shape(random.randint(1, 4), 10)) for _ in range(len(names))] titles = [ random_str_some_unicode(random.randint(1, 10)) for _ in range(len(names)) ] offsets = [random.randint(0, 100) for _ in range(len(names))] desc = { 'names': names, 'formats': formats, 'titles': titles, 'offsets': offsets } desc_with_itemsize = desc.copy() desc_with_itemsize['itemsize'] = \ np.dtype(desc).itemsize + random.randint(1, 100) # Make the dtypes in all combinations of the description and # align. Note that if the type isn't valid at all, it is not # tested. dts = [] for d in (desc, desc_with_itemsize): for align in (False, True): try: dts.append(np.dtype(d, align=align)) except: pass for dt in dts: self.check_dtype(dt)
def test_str_unicode(self): data = random_str_some_unicode(random.randint(1, max_string_length)) out = self.write_readback(data, random_name(), self.options) self.assert_equal(out, data)