Skip to content

Commit 0da48cb

Browse files
author
Alexandre Marie
committed
Cleaned try/except h5py blocks.
# Conflicts: # silx/io/test/test_fabioh5.py
1 parent e6ab9e3 commit 0da48cb

30 files changed

+167
-480
lines changed

examples/viewer3DVolume.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@
5252

5353
_logger = logging.getLogger(__name__)
5454

55-
56-
try:
57-
import h5py
58-
except ImportError:
59-
_logger.warning('h5py is not installed: HDF5 not supported')
60-
h5py = None
61-
55+
import h5py
6256

6357
def load(filename):
6458
"""Load 3D scalar field from file.
@@ -72,7 +66,7 @@ def load(filename):
7266
if not os.path.isfile(filename.split('::')[0]):
7367
raise IOError('No input file: %s' % filename)
7468

75-
if h5py is not None and h5py.is_hdf5(filename.split('::')[0]):
69+
if h5py.is_hdf5(filename.split('::')[0]):
7670
if '::' not in filename:
7771
raise ValueError(
7872
'HDF5 path not provided: Use <filename>::<path> format')

run_tests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ def importer(name):
115115
else:
116116
logger.info("Numpy %s", numpy.version.version)
117117

118-
119118
try:
120119
import h5py
121120
except Exception as error:

silx/app/convert.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,20 +307,14 @@ def check_gzip_compression_opts(value):
307307
_logger.debug("Backtrace", exc_info=True)
308308
hdf5plugin = None
309309

310+
import h5py
311+
310312
try:
311-
import h5py
312313
from silx.io.convert import write_to_h5
313314
except ImportError:
314315
_logger.debug("Backtrace", exc_info=True)
315-
h5py = None
316316
write_to_h5 = None
317317

318-
if h5py is None:
319-
message = "Module 'h5py' is not installed but is mandatory."\
320-
+ " You can install it using \"pip install h5py\"."
321-
_logger.error(message)
322-
return -1
323-
324318
if hdf5plugin is None:
325319
message = "Module 'hdf5plugin' is not installed. It supports additional hdf5"\
326320
+ " compressions. You can install it using \"pip install hdf5plugin\"."

silx/app/test/test_convert.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@
3535
import unittest
3636
import io
3737
import gc
38-
39-
try:
40-
import h5py
41-
except ImportError:
42-
h5py = None
38+
import h5py
4339

4440
import silx
4541
from .. import convert
@@ -103,14 +99,6 @@ def testHelp(self):
10399
result = e.args[0]
104100
self.assertEqual(result, 0)
105101

106-
@testutils.test_logging(convert._logger.name, error=1)
107-
def testH5pyNotInstalled(self):
108-
with testutils.EnsureImportError("h5py"):
109-
result = convert.main(["convert", "foo.spec", "bar.edf"])
110-
# we explicitly return -1 if h5py is not imported
111-
self.assertNotEqual(result, 0)
112-
113-
@unittest.skipIf(h5py is None, "h5py is required to test convert")
114102
def testWrongOption(self):
115103
# presence of a wrong option must cause a SystemExit or a return
116104
# with a non-zero status
@@ -120,14 +108,12 @@ def testWrongOption(self):
120108
result = e.args[0]
121109
self.assertNotEqual(result, 0)
122110

123-
@unittest.skipIf(h5py is None, "h5py is required to test convert")
124111
@testutils.test_logging(convert._logger.name, error=3)
125112
# one error log per missing file + one "Aborted" error log
126113
def testWrongFiles(self):
127114
result = convert.main(["convert", "foo.spec", "bar.edf"])
128115
self.assertNotEqual(result, 0)
129116

130-
@unittest.skipIf(h5py is None, "h5py is required to test convert")
131117
def testFile(self):
132118
# create a writable temp directory
133119
tempdir = tempfile.mkdtemp()

silx/app/view/main.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,7 @@ def mainQt(options):
8282
except ImportError:
8383
_logger.debug("Backtrace", exc_info=True)
8484

85-
try:
86-
import h5py
87-
except ImportError:
88-
_logger.debug("Backtrace", exc_info=True)
89-
h5py = None
90-
91-
if h5py is None:
92-
message = "Module 'h5py' is not installed but is mandatory."\
93-
+ " You can install it using \"pip install h5py\"."
94-
_logger.error(message)
95-
return -1
85+
import h5py
9686

9787
app = qt.QApplication([])
9888
qt.QLocale.setDefault(qt.QLocale.c())

silx/app/view/test/test_view.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535
import tempfile
3636
import shutil
3737
import os.path
38-
try:
39-
import h5py
40-
except ImportError:
41-
h5py = None
38+
import h5py
4239

4340
from silx.gui import qt
4441
from silx.app.view.Viewer import Viewer
@@ -56,22 +53,21 @@ def setUpModule():
5653
global _tmpDirectory
5754
_tmpDirectory = tempfile.mkdtemp(prefix=__name__)
5855

59-
if h5py is not None:
60-
# create h5 data
61-
filename = _tmpDirectory + "/data.h5"
62-
f = h5py.File(filename, "w")
63-
g = f.create_group("arrays")
64-
g.create_dataset("scalar", data=10)
65-
g.create_dataset("integers", data=numpy.array([10, 20, 30]))
66-
f.close()
56+
# create h5 data
57+
filename = _tmpDirectory + "/data.h5"
58+
f = h5py.File(filename, "w")
59+
g = f.create_group("arrays")
60+
g.create_dataset("scalar", data=10)
61+
g.create_dataset("integers", data=numpy.array([10, 20, 30]))
62+
f.close()
6763

68-
# create h5 data
69-
filename = _tmpDirectory + "/data2.h5"
70-
f = h5py.File(filename, "w")
71-
g = f.create_group("arrays")
72-
g.create_dataset("scalar", data=20)
73-
g.create_dataset("integers", data=numpy.array([10, 20, 30]))
74-
f.close()
64+
# create h5 data
65+
filename = _tmpDirectory + "/data2.h5"
66+
f = h5py.File(filename, "w")
67+
g = f.create_group("arrays")
68+
g.create_dataset("scalar", data=20)
69+
g.create_dataset("integers", data=numpy.array([10, 20, 30]))
70+
f.close()
7571

7672

7773
def tearDownModule():
@@ -167,7 +163,6 @@ def testCustomDataItemNone(self):
167163
self.assertIs(widget.getData(), None)
168164
self.assertIs(widget.getCustomNxdataItem(), data)
169165

170-
@unittest.skipIf(h5py is None, "Could not import h5py")
171166
def testRemoveDatasetsFrom(self):
172167
f = h5py.File(os.path.join(_tmpDirectory, "data.h5"))
173168
try:
@@ -179,7 +174,6 @@ def testRemoveDatasetsFrom(self):
179174
widget.setData(None)
180175
f.close()
181176

182-
@unittest.skipIf(h5py is None, "Could not import h5py")
183177
def testReplaceDatasetsFrom(self):
184178
f = h5py.File(os.path.join(_tmpDirectory, "data.h5"))
185179
f2 = h5py.File(os.path.join(_tmpDirectory, "data2.h5"))
@@ -248,7 +242,6 @@ def testCreateBadNxdata(self):
248242
self.assertIsNotNone(nxdata)
249243
self.assertFalse(item.isValid())
250244

251-
@unittest.skipIf(h5py is None, "Could not import h5py")
252245
def testRemoveDatasetsFrom(self):
253246
f = h5py.File(os.path.join(_tmpDirectory, "data.h5"))
254247
try:
@@ -261,7 +254,6 @@ def testRemoveDatasetsFrom(self):
261254
model.clear()
262255
f.close()
263256

264-
@unittest.skipIf(h5py is None, "Could not import h5py")
265257
def testReplaceDatasetsFrom(self):
266258
f = h5py.File(os.path.join(_tmpDirectory, "data.h5"))
267259
f2 = h5py.File(os.path.join(_tmpDirectory, "data2.h5"))

silx/gui/data/Hdf5TableView.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@
4444
from ..hdf5.Hdf5Formatter import Hdf5Formatter
4545
from ..hdf5._utils import htmlFromDict
4646

47-
try:
48-
import h5py
49-
except ImportError:
50-
h5py = None
47+
import h5py
5148

5249

5350
_logger = logging.getLogger(__name__)
@@ -416,7 +413,7 @@ def __initProperties(self):
416413

417414
self.__data.addHeaderRow(headerLabel="Data info")
418415

419-
if h5py is not None and hasattr(obj, "id") and hasattr(obj.id, "get_type"):
416+
if hasattr(obj, "id") and hasattr(obj.id, "get_type"):
420417
# display the HDF5 type
421418
self.__data.addHeaderValueRow("HDF5 type", self.__formatHdf5Type)
422419
self.__data.addHeaderValueRow("dtype", self.__formatDType)

silx/gui/data/TextFormatter.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@
3737

3838
from silx.gui import qt
3939

40-
try:
41-
import h5py
42-
except ImportError:
43-
h5py = None
40+
import h5py
4441

4542

4643
_logger = logging.getLogger(__name__)
@@ -324,10 +321,9 @@ def toString(self, data, dtype=None):
324321
if dtype.kind == 'S':
325322
return self.__formatCharString(data)
326323
elif dtype.kind == 'O':
327-
if h5py is not None:
328-
text = self.__formatH5pyObject(data, dtype)
329-
if text is not None:
330-
return text
324+
text = self.__formatH5pyObject(data, dtype)
325+
if text is not None:
326+
return text
331327
try:
332328
# Try ascii/utf-8
333329
text = "%s" % data.decode("utf-8")
@@ -341,15 +337,14 @@ def toString(self, data, dtype=None):
341337
elif isinstance(data, (numpy.integer)):
342338
if dtype is None:
343339
dtype = data.dtype
344-
if h5py is not None:
345-
enumType = h5py.check_dtype(enum=dtype)
346-
if enumType is not None:
347-
for key, value in enumType.items():
348-
if value == data:
349-
result = {}
350-
result["name"] = key
351-
result["value"] = data
352-
return self.__enumFormat % result
340+
enumType = h5py.check_dtype(enum=dtype)
341+
if enumType is not None:
342+
for key, value in enumType.items():
343+
if value == data:
344+
result = {}
345+
result["name"] = key
346+
result["value"] = data
347+
return self.__enumFormat % result
353348
return self.__integerFormat % data
354349
elif isinstance(data, (numbers.Integral)):
355350
return self.__integerFormat % data
@@ -375,21 +370,20 @@ def toString(self, data, dtype=None):
375370
template = self.__floatFormat
376371
params = (data.real)
377372
return template % params
378-
elif h5py is not None and isinstance(data, h5py.h5r.Reference):
373+
elif isinstance(data, h5py.h5r.Reference):
379374
dtype = h5py.special_dtype(ref=h5py.Reference)
380375
text = self.__formatH5pyObject(data, dtype)
381376
return text
382-
elif h5py is not None and isinstance(data, h5py.h5r.RegionReference):
377+
elif isinstance(data, h5py.h5r.RegionReference):
383378
dtype = h5py.special_dtype(ref=h5py.RegionReference)
384379
text = self.__formatH5pyObject(data, dtype)
385380
return text
386381
elif isinstance(data, numpy.object_) or dtype is not None:
387382
if dtype is None:
388383
dtype = data.dtype
389-
if h5py is not None:
390-
text = self.__formatH5pyObject(data, dtype)
391-
if text is not None:
392-
return text
384+
text = self.__formatH5pyObject(data, dtype)
385+
if text is not None:
386+
return text
393387
# That's a numpy object
394388
return str(data)
395389
return str(data)

silx/gui/data/test/test_arraywidget.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
from silx.gui.data import ArrayTableWidget
3737
from silx.gui.utils.testutils import TestCaseQt
3838

39-
try:
40-
import h5py
41-
except ImportError:
42-
h5py = None
39+
import h5py
4340

4441

4542
class TestArrayWidget(TestCaseQt):
@@ -190,7 +187,6 @@ def testReferenceReturned(self):
190187
self.assertIs(b0, b1)
191188

192189

193-
@unittest.skipIf(h5py is None, "Could not import h5py")
194190
class TestH5pyArrayWidget(TestCaseQt):
195191
"""Basic test for ArrayTableWidget with a dataset.
196192

silx/gui/data/test/test_dataviewer.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@
4242
from silx.gui.utils.testutils import SignalListener
4343
from silx.gui.utils.testutils import TestCaseQt
4444

45-
try:
46-
import h5py
47-
except ImportError:
48-
h5py = None
45+
import h5py
4946

5047

5148
class _DataViewMock(DataView):
@@ -170,8 +167,6 @@ def test_record_4d_data(self):
170167
self.assertEqual(DataViews.RAW_MODE, widget.displayedView().modeId())
171168

172169
def test_3d_h5_dataset(self):
173-
if h5py is None:
174-
self.skipTest("h5py library is not available")
175170
with self.h5_temporary_file() as h5file:
176171
dataset = h5file["data"]
177172
widget = self.create_widget()

0 commit comments

Comments
 (0)