-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
175 lines (121 loc) · 5.06 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
:Object: **variables**: List of all variables available in the interactive namespace.
Instance of ``__init__.Variables``.
:Object: **hdf5_files**: List of all HDF5 files available in the interactive namespace.
Instance of ``__init__.HDF5Files``.
:Object: **compression**: Dictionary that contains dataset compression parameters.
Available parameters:
- chunks (default = ``True``)
- compression (default = ``lzf``)
- shuffle (default = ``True``)
Detailed information on dataset compression can be found in `h5py dataset documentation <http://docs.h5py.org/en/latest/high/dataset.html>`_.
"""
import datetime as _d
import workspace as _w
hdf5_files = _w.HDF5Files()
variables = _w.Variables()
compression = _w.compression
def add(filename, mode='a'):
"""
Creates or opens a HDF5 file on the hard drive and adds it to the interactive namespace.
Parameters
----------
filename : str
Name of the HDF5 file.
mode : str, optional
Mode in which to open file, one of ('r', 'r+', 'w', 'w-', 'x', 'a').
+------------+----------------------------------------------------------+
| **r** | Readonly, file must exist |
+------------+----------------------------------------------------------+
| **r+** | Read/write, file must exist |
+------------+----------------------------------------------------------+
| **w** | Create file, truncate if exists |
+------------+----------------------------------------------------------+
| **w- or x**| Create file, fail if exists |
+------------+----------------------------------------------------------+
| **a** | Read/write if exists, create otherwise (**default**) |
+------------+----------------------------------------------------------+
Notes
------
Based on ``h5py.File()``.
The root group is automatically created when the HDF5 file is created.
Examples
---------
Add a sample file to the interactive namespace::
>>> <interface_name>.add("example.h5")
"""
if mode == 'w':
mode == 'w-'
# Ponowne otwarcie uprzednio otwartego pliku nie generuje
# błędu
f = _w.h5py.File(filename, mode)
# Usuń i dodaj aby elementy listy lof występowały w
# kolejności dodawania plików do zasobów
if f in _w.lof:
_w.lof.remove(f)
_w.lof.append(f)
_w.update()
def clear():
"""
Closes all open HDF5 files and removes them from the interactive namespace.
Files are not physically deleted form the hard drive, but rather deleted from current interactive namespace.
Notes
------
Based on ``h5py.File()``.
Examples
---------
Clears current interactive namespace::
>>> <interface_name>.clear()
"""
for f in _w.lof:
f.close()
_w.lof.clear()
_w.update()
def close(obj=-1):
"""close(obj=-1)
Closes HDF5 file with selected index and removes it from the interactive namespace.
The default index is minus one, which means the last item on the list of files.
Notes
------
Based on ``h5py.File()``.
This method does not guarantee that the file will be correctly saved. To ensure no data corruption, ``api.flush()`` should be called before ``api.close()``.
Examples
---------
Add a sample file to the interactive namespace::
>>> <interface_name>.add("example.h5")
File *"example.h5"* is present in interactive namespace with index = -1.
In order to close this file and remove from the interactive namespace we need to call ``api.close()`` method on ``<interface_name>[file_index]`` file object::
>>> <interface_name>[-1].close()
"""
_w.lof.pop(_w.index(obj)).close()
_w.update()
def flush(obj=-1):
"""flush(obj=-1)
Forces data in HDF5 file to be copied from the memory buffer and saved on the hard drive.
The default index is minus one, which means the last item on the list of files.
Notes
------
Based on ``h5py.File()``.
Examples
---------
Ensure that data from the selected HDF5 file is saved on the hard drive::
>>> <interface_name>[file_index].flush()
Close it::
>>> <interface_name>[file_index].close()
"""
_w.lof[_w.index(obj)].flush()
def _template(cls):
def create_variable(obj=-1, name=None, parent=None):
try:
f = _w.lof[_w.index(obj._index)]
except AttributeError:
f = _w.lof[_w.index(obj)]
if not name:
name = cls.__name__.lower()[0] + str(
_d.datetime.now().strftime('%Y_%m_%d_%H_%M_%S'))
cls.create(f.create_group(name), _w.fingerprint(parent))
_w.update()
return getattr(_w.interactive_namespace, name)
return create_variable
for _cls in _w.list_of_variables:
vars()[_w.create_fcn_name(_cls)] = _template(_cls)