Skip to content

Commit 27cee26

Browse files
authored
Merge pull request #420 from CDAT/check_for_imports
Check for imports
2 parents 58ba16a + 26a5739 commit 27cee26

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ aliases:
2626
command: |
2727
export PATH=$WORKDIR/miniconda/bin:$PATH
2828
conda config --set always_yes yes --set changeps1 no
29-
conda update -y -q conda
29+
#conda update -y -q conda
3030
conda config --set anaconda_upload no
3131
if [[ $PY_VER = "py2" ]]; then
3232
conda create -q -n $PY_VER $CUSTOM_CHANNELS $CHANNELS $PKGS $TEMP_PKGS "python<3"
@@ -90,7 +90,7 @@ aliases:
9090
ln -s ../recipe vcs
9191
export PKG_NAME=vcs
9292
export USER=cdat
93-
export VERSION=8.1
93+
export VERSION=8.2
9494
export LABEL=nightly
9595
python ./prep_for_build.py -l $VERSION
9696
conda build $PKG_NAME $CHANNELS

vcs/displayplot.py

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,19 @@
2929
from .xmldocs import listdoc # noqa
3030
from functools import partial
3131

32-
try:
33-
import IPython.display
34-
HAVE_IPY = True
35-
try:
36-
import sidecar
37-
HAVE_SIDECAR = True
38-
except Exception:
39-
HAVE_SIDECAR = False
32+
33+
# Will attempt to import module, returns module and true if successful
34+
def try_import_module(module, sub_modules=None):
4035
try:
41-
import ipywidgets
42-
HAVE_IPYWIDGETS = True
43-
except Exception: # no widgets
44-
HAVE_IPYWIDGETS = False
45-
except Exception: # no IPython
46-
HAVE_IPY = False
47-
HAVE_SIDECAR = False
48-
HAVE_IPYWIDGETS = False
36+
return __import__(module, fromlist=sub_modules), True
37+
except ImportError:
38+
return None, False
39+
40+
41+
# Save whether modules were successfully imported
42+
IPython, HAVE_IPY = try_import_module("IPython", ["display"])
43+
ipywidgets, HAVE_IPYWIDGETS = try_import_module("ipywidgets")
44+
sidecar, HAVE_SIDECAR = try_import_module("sidecar")
4945

5046

5147
try:
@@ -59,7 +55,7 @@ def get_update_array_kw(disp, array, widgets, debug_target=None):
5955
return {}
6056
if debug_target is not None:
6157
with debug_target:
62-
print("ok looking at kewwords for", array.id)
58+
print("ok looking at keywords for", array.id)
6359
kw = {}
6460
for ax in array.getAxisList():
6561
if debug_target is not None:
@@ -179,24 +175,29 @@ def handle_slider_change(self, change, widgets, name):
179175
debug_target = self._parent._display_target_out
180176
else:
181177
debug_target = None
182-
kw1 = get_update_array_kw(disp, disp.array[0], widgets, debug_target)
183-
kw2 = get_update_array_kw(disp, disp.array[1], widgets, debug_target)
178+
kw1 = get_update_array_kw(
179+
disp, disp.array[0], widgets, debug_target)
180+
kw2 = get_update_array_kw(
181+
disp, disp.array[1], widgets, debug_target)
184182
# Ok in some case (u/v e.g) same dims but different name on 2nd array
185183
if disp.array[1] is not None:
186184
for axId in kw1:
187185
if axId not in kw2: # probably should be there as wll
188-
ax = disp.array[0].getAxis(disp.array[0].getAxisIndex(axId))
186+
ax = disp.array[0].getAxis(
187+
disp.array[0].getAxisIndex(axId))
189188
if debug:
190189
with self._parent._display_target_out:
191-
print("Examing axis:", axId, "vs", ax, hasattr(ax, "axis"))
190+
print("Examing axis:", axId, "vs",
191+
ax, hasattr(ax, "axis"))
192192
if hasattr(ax, "axis"): # special dim (T,Z,Y,X)
193193
if debug:
194194
with self._parent._display_target_out:
195195
print("Examing axis:", axId, "vs", ax.axis)
196196
for ax2 in disp.array[1].getAxisList():
197197
if debug:
198198
with self._parent._display_target_out:
199-
print("Examing axis:", axId, "vs", ax2.id)
199+
print("Examing axis:",
200+
axId, "vs", ax2.id)
200201
if hasattr(ax2, "axis") and ax2.axis == ax.axis:
201202
kw2[ax2.id] = kw1[ax.id]
202203
if debug:
@@ -235,7 +236,8 @@ def handle_slider_change(self, change, widgets, name):
235236
sp = slider.description
236237
if debug:
237238
with self._parent._display_target_out:
238-
print("OPk looking at:", name, slider.description, sp, sp == name)
239+
print("OPk looking at:", name,
240+
slider.description, sp, sp == name)
239241
if sp == name:
240242
value = label.values[change["new"]]
241243
label.value = "{}".format(value)
@@ -264,7 +266,8 @@ def generate_sliders(self, debug):
264266
widgets = []
265267
for disp_name in self._parent.display_names:
266268
disp = vcs.elements["display"][disp_name]
267-
gm_info = vcs.graphicsmethodinfo(vcs.getgraphicsmethod(disp.g_type, disp.g_name))
269+
gm_info = vcs.graphicsmethodinfo(
270+
vcs.getgraphicsmethod(disp.g_type, disp.g_name))
268271
data = disp.array[0]
269272
if data is None:
270273
continue
@@ -274,7 +277,8 @@ def generate_sliders(self, debug):
274277
if dim.isTime():
275278
values = dim.asComponentTime()
276279
else:
277-
values = ["{}{}".format(value, units) for value in dim[:]]
280+
values = ["{}{}".format(value, units)
281+
for value in dim[:]]
278282
slider = ipywidgets.IntSlider(
279283
value=0,
280284
min=0,
@@ -291,7 +295,8 @@ def generate_sliders(self, debug):
291295
label.values = values
292296
box = ipywidgets.HBox([slider, label])
293297
widgets.append(box)
294-
funcs.append(partial(self.handle_slider_change, name=dim.id))
298+
funcs.append(
299+
partial(self.handle_slider_change, name=dim.id))
295300
dimensions.add((dim.id, units, dim[0], dim[-1]))
296301
for i, wdgt in enumerate(widgets):
297302
slider = wdgt.children[0]
@@ -320,11 +325,13 @@ def _repr_png_(self):
320325
self._parent._display_target_image = ipywidgets.Image()
321326
if HAVE_IPYWIDGETS:
322327
if debug:
323-
self._parent._display_target_out = ipywidgets.Output(layout={'border': '1px solid black'})
328+
self._parent._display_target_out = ipywidgets.Output(
329+
layout={'border': '1px solid black'})
324330
else:
325331
self._parent._display_target_out = None
326332
widgets = self.generate_sliders(debug)
327-
vbox = ipywidgets.VBox(widgets + [self._parent._display_target_image])
333+
vbox = ipywidgets.VBox(
334+
widgets + [self._parent._display_target_image])
328335
if HAVE_SIDECAR and sidecar_on:
329336
with self._parent._display_target:
330337
IPython.display.clear_output()
@@ -482,19 +489,19 @@ def _set_backend(self, value):
482489
"dictionary of things the backend wants to be able to reuse")
483490

484491
##########################################################################
485-
# #
486-
# Initialize the display plot attributes. #
487-
# #
492+
# #
493+
# Initialize the display plot attributes. #
494+
# #
488495
##########################################################################
489496
def __init__(self, Dp_name, Dp_name_src='default', parent=None):
490-
# #
497+
# #
491498
###################################################################
492-
# Initialize the display plot's class and its members #
493-
# The getDpmember function retrieves the values of the #
494-
# display plot members in the C structure and passes back the #
495-
# appropriate Python Object. #
499+
# Initialize the display plot's class and its members #
500+
# The getDpmember function retrieves the values of the #
501+
# display plot members in the C structure and passes back the #
502+
# appropriate Python Object. #
496503
###################################################################
497-
# #
504+
# #
498505
self.extradisplays = []
499506
self._name = Dp_name
500507
self.s_name = 'Dp'
@@ -526,9 +533,9 @@ def __init__(self, Dp_name, Dp_name_src='default', parent=None):
526533

527534
vcs.elements["display"][self._name] = self
528535
##########################################################################
529-
# #
530-
# List out display plot members (attributes). #
531-
# #
536+
# #
537+
# List out display plot members (attributes). #
538+
# #
532539
##########################################################################
533540

534541
def list(self):

0 commit comments

Comments
 (0)