1import numpy as np
2import pyhdf5_handler
3import datetime
4import pandas as pd
5
6
7
8if __name__ == '__main__':
9
10 #open an hdf5 database, test.hdf5.
11 hdf5 = pyhdf5_handler.open_hdf5("./test.hdf5")
12
13 #Create a group in the hdf5
14 hdf5 = pyhdf5_handler.add_hdf5_sub_group(hdf5, subgroup="my_group")
15 hdf5["my_group"]
16
17 #save any data in the hdf5 database
18 pyhdf5_handler.hdf5_dataset_creator(hdf5,"str","str")
19 pyhdf5_handler.hdf5_dataset_creator(hdf5,"numbers",1.0)
20 pyhdf5_handler.hdf5_dataset_creator(hdf5,"none",None)
21 pyhdf5_handler.hdf5_dataset_creator(hdf5,"timestamp_numpy",np.datetime64('2019-09-22T17:38:30'))
22 pyhdf5_handler.hdf5_dataset_creator(hdf5,"timestamp_datetime",datetime.datetime.fromisoformat('2019-09-22T17:38:30'))
23 pyhdf5_handler.hdf5_dataset_creator(hdf5,"timestamp_pandas",pd.Timestamp('2019-09-22T17:38:30'))
24 pyhdf5_handler.hdf5_dataset_creator(hdf5,"list_num",[1.0,2.0])
25 pyhdf5_handler.hdf5_dataset_creator(hdf5,"list_str",["a","b"])
26 pyhdf5_handler.hdf5_dataset_creator(hdf5,"list_mixte",[1.0,"a"])
27 pyhdf5_handler.hdf5_dataset_creator(hdf5,"list_date_numpy",[np.datetime64('2019-09-22 17:38:30'),np.datetime64('2019-09-22 18:38:30')])
28 pyhdf5_handler.hdf5_dataset_creator(hdf5,"list_date_datetime",[datetime.datetime.fromisoformat('2019-09-22 17:38:30'),datetime.datetime.fromisoformat('2019-09-22T18:38:30')])
29 pyhdf5_handler.hdf5_dataset_creator(hdf5,"list_date_pandas",[pd.Timestamp('2019-09-22 17:38:30'),pd.Timestamp('2019-09-22 17:38:30')])
30 pyhdf5_handler.hdf5_dataset_creator(hdf5,"list_date_range_pandas",pd.date_range(start='1/1/2018', end='1/08/2018'))
31
32 #write a python dictionary in the hdf5 database
33 dictionary={"dict":{
34 "int":1,
35 "float":2.0,
36 "none":None,
37 "timestamp":pd.Timestamp('2019-09-22 17:38:30'),
38 "list":[1,2,3,4],
39 "array": np.array([1,2,3,4]),
40 "date_range": pd.date_range(start='1/1/2018', end='1/08/2018'),
41 "list_mixte":[1.0,np.datetime64('2019-09-22 17:38:30')],
42 }
43 }
44
45 hdf5.attrs["attribute"]="myattribute"
46
47 pyhdf5_handler.src.hdf5_handler.save_dict_to_hdf5(hdf5, dictionary)
48
49 #handle structured ndarray
50 data = [('Alice', 25, 55.0), ('Bob', 32, 60.5)]
51 dtypes = [('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]
52 people = np.array(data, dtype=dtypes)
53
54 pyhdf5_handler.hdf5_dataset_creator(hdf5,"structured_array",people)
55
56 #viewing data stored in the hdf5 (recursive)
57 pyhdf5_handler.hdf5_view(hdf5)
58
59 #viwing element stored in the hdf5 (at the current level)
60 pyhdf5_handler.hdf5_ls(hdf5)
61
62 #read an hdf5 and import it as a python dictionary
63 data=pyhdf5_handler.read_hdf5_as_dict(hdf5)
64
65 #read a specific item
66 pyhdf5_handler.hdf5_read_dataset(hdf5["str"], hdf5.attrs["_str"])
67 pyhdf5_handler.hdf5_read_dataset(hdf5["list_date_numpy"], hdf5.attrs["_list_date_numpy"])
68
69 #close the hdf5
70 hdf5.close()
71
72 #handle file directly
73 pyhdf5_handler.hdf5file_ls("./test.hdf5")
74 pyhdf5_handler.hdf5file_ls("./test.hdf5",location="structured_array")
75
76 data=pyhdf5_handler.read_hdf5file_as_dict("./test.hdf5")
77
78 pyhdf5_handler.save_dict_to_hdf5file("./test.hdf5",data)
79
80 res=pyhdf5_handler.search_in_hdf5file("./test.hdf5", key="date_range", location="./", wait_time=0)
81
82 res=pyhdf5_handler.search_in_hdf5file("./test.hdf5", key="structured_array", location="./", wait_time=0)
83
84 pyhdf5_handler.get_hdf5file_item(path_to_hdf5="./test.hdf5", location="./", item="structured_array", search_attrs=False)
85
86 pyhdf5_handler.get_hdf5file_item(path_to_hdf5="./test.hdf5", location="./", item="list_mixte", search_attrs=False)
87
88 pyhdf5_handler.get_hdf5file_attribute(path_to_hdf5="./test.hdf5", location="./", attribute="_list_num", wait_time=0)
89
90 pyhdf5_handler.get_hdf5file_attribute(path_to_hdf5="./test.hdf5", location="./structured_array/ndarray_ds", attribute="_name", wait_time=0)
91
92
93