Skip to content

Commit d7b1d68

Browse files
authored
mask the import names for less likelihood of conflict (#147)
* mask the import names for less likelihood of conflict * typo
1 parent 93bfe84 commit d7b1d68

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

src/inspectorscripts.ts

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,77 +26,87 @@ _jupyterlab_variableinspector_nms = NamespaceMagics()
2626
_jupyterlab_variableinspector_Jupyter = get_ipython()
2727
_jupyterlab_variableinspector_nms.shell = _jupyterlab_variableinspector_Jupyter.kernel.shell
2828
29-
np = None
30-
pd = None
31-
pyspark = None
32-
tf = None
33-
ipywidgets = None
29+
__np = None
30+
__pd = None
31+
__pyspark = None
32+
__tf = None
33+
__K = None
34+
__ipywidgets = None
3435
3536
3637
def _check_imported():
37-
global np, pd, pyspark, tf, ipywidgets
38+
global __np, __pd, __pyspark, __tf, __K, __ipywidgets
3839
pkg_resources = [dist.project_name.replace("Python","") for
3940
dist in __import__("pkg_resources").working_set]
4041
if 'numpy' in pkg_resources:
4142
# don't really need the try
4243
try:
43-
import numpy as np
44+
import numpy as __np
4445
except ImportError:
45-
np = None
46+
__np = None
4647
4748
if 'pandas' in pkg_resources:
4849
try:
49-
import pandas as pd
50+
import pandas as __pd
5051
except ImportError:
51-
pd = None
52+
__pd = None
5253
5354
if 'pyspark' in pkg_resources:
5455
try:
55-
import pyspark
56+
import pyspark as __pyspark
5657
except ImportError:
57-
pyspark = None
58+
__pyspark = None
5859
5960
if 'tensorflow' in pkg_resources or 'keras' in pkg_resources:
6061
try:
61-
import tensorflow as tf
62-
import keras.backend as K
62+
import tensorflow as __tf
6363
except ImportError:
64-
tf = None
64+
__tf = None
65+
66+
try:
67+
import keras.backend as __K
68+
except ImportError:
69+
try:
70+
import tensorflow.keras.backend as __K
71+
except ImportError:
72+
__K = None
73+
74+
6575
6676
if 'ipywidgets' in pkg_resources:
6777
try:
68-
import ipywidgets
78+
import ipywidgets as __ipywidgets
6979
except ImportError:
70-
ipywidgets = None
80+
__ipywidgets = None
7181
7282
7383
def _jupyterlab_variableinspector_getsizeof(x):
7484
if type(x).__name__ in ['ndarray', 'Series']:
7585
return x.nbytes
76-
elif pyspark and isinstance(x, pyspark.sql.DataFrame):
86+
elif __pyspark and isinstance(x, __pyspark.sql.DataFrame):
7787
return "?"
78-
elif tf and isinstance(x, tf.Variable):
88+
elif __tf and isinstance(x, __tf.Variable):
7989
return "?"
80-
elif pd and type(x).__name__ == 'DataFrame':
90+
elif __pd and type(x).__name__ == 'DataFrame':
8191
return x.memory_usage().sum()
8292
else:
8393
return sys.getsizeof(x)
8494
8595
8696
def _jupyterlab_variableinspector_getshapeof(x):
87-
if pd and isinstance(x, pd.DataFrame):
97+
if __pd and isinstance(x, __pd.DataFrame):
8898
return "%d rows x %d cols" % x.shape
89-
if pd and isinstance(x, pd.Series):
99+
if __pd and isinstance(x, __pd.Series):
90100
return "%d rows" % x.shape
91-
if np and isinstance(x, np.ndarray):
101+
if __np and isinstance(x, __np.ndarray):
92102
shape = " x ".join([str(i) for i in x.shape])
93103
return "%s" % shape
94-
if pyspark and isinstance(x, pyspark.sql.DataFrame):
104+
if __pyspark and isinstance(x, __pyspark.sql.DataFrame):
95105
return "? rows x %d cols" % len(x.columns)
96-
if tf and isinstance(x, tf.Variable):
106+
if __tf and isinstance(x, __tf.Variable):
97107
shape = " x ".join([str(int(i)) for i in x.shape])
98108
return "%s" % shape
99-
if tf and isinstance(x, tf.Tensor):
109+
if __tf and isinstance(x, __tf.Tensor):
100110
shape = " x ".join([str(int(i)) for i in x.shape])
101111
return "%s" % shape
102112
if isinstance(x, list):
@@ -109,13 +119,13 @@ def _jupyterlab_variableinspector_getshapeof(x):
109119
def _jupyterlab_variableinspector_getcontentof(x):
110120
# returns content in a friendly way for python variables
111121
# pandas and numpy
112-
if pd and isinstance(x, pd.DataFrame):
122+
if __pd and isinstance(x, __pd.DataFrame):
113123
colnames = ', '.join(x.columns.map(str))
114124
content = "Columns: %s" % colnames
115-
elif pd and isinstance(x, pd.Series):
125+
elif __pd and isinstance(x, __pd.Series):
116126
content = str(x.values).replace(" ", ", ")[1:-1]
117127
content = content.replace("\\n", "")
118-
elif np and isinstance(x, np.ndarray):
128+
elif __np and isinstance(x, __np.ndarray):
119129
content = x.__repr__()
120130
else:
121131
content = str(x)
@@ -128,25 +138,25 @@ def _jupyterlab_variableinspector_getcontentof(x):
128138
129139
def _jupyterlab_variableinspector_is_matrix(x):
130140
# True if type(x).__name__ in ["DataFrame", "ndarray", "Series"] else False
131-
if pd and isinstance(x, pd.DataFrame):
141+
if __pd and isinstance(x, __pd.DataFrame):
132142
return True
133-
if pd and isinstance(x, pd.Series):
143+
if __pd and isinstance(x, __pd.Series):
134144
return True
135-
if np and isinstance(x, np.ndarray) and len(x.shape) <= 2:
145+
if __np and isinstance(x, __np.ndarray) and len(x.shape) <= 2:
136146
return True
137-
if pyspark and isinstance(x, pyspark.sql.DataFrame):
147+
if __pyspark and isinstance(x, __pyspark.sql.DataFrame):
138148
return True
139-
if tf and isinstance(x, tf.Variable) and len(x.shape) <= 2:
149+
if __tf and isinstance(x, __tf.Variable) and len(x.shape) <= 2:
140150
return True
141-
if tf and isinstance(x, tf.Tensor) and len(x.shape) <= 2:
151+
if __tf and isinstance(x, __tf.Tensor) and len(x.shape) <= 2:
142152
return True
143153
if isinstance(x, list):
144154
return True
145155
return False
146156
147157
148158
def _jupyterlab_variableinspector_is_widget(x):
149-
return ipywidgets and issubclass(x, ipywidgets.DOMWidget)
159+
return __ipywidgets and issubclass(x, __ipywidgets.DOMWidget)
150160
151161
152162
def _jupyterlab_variableinspector_dict_list():
@@ -156,15 +166,15 @@ def _jupyterlab_variableinspector_dict_list():
156166
obj = eval(v)
157167
if isinstance(obj, str):
158168
return True
159-
if tf and isinstance(obj, tf.Variable):
169+
if __tf and isinstance(obj, __tf.Variable):
160170
return True
161-
if pd and pd is not None and (
162-
isinstance(obj, pd.core.frame.DataFrame)
163-
or isinstance(obj, pd.core.series.Series)):
171+
if __pd and __pd is not None and (
172+
isinstance(obj, __pd.core.frame.DataFrame)
173+
or isinstance(obj, __pd.core.series.Series)):
164174
return True
165175
if str(obj)[0] == "<":
166176
return False
167-
if v in ['np', 'pd', 'pyspark', 'tf', 'ipywidgets']:
177+
if v in ['__np', '__pd', '__pyspark', '__tf', '__K', '__ipywidgets']:
168178
return obj is not None
169179
if str(obj).startswith("_Feature"):
170180
# removes tf/keras objects
@@ -192,26 +202,26 @@ def _jupyterlab_variableinspector_getmatrixcontent(x, max_rows=10000):
192202
# to do: add something to handle this in the future
193203
threshold = max_rows
194204
195-
if pd and pyspark and isinstance(x, pyspark.sql.DataFrame):
205+
if __pd and __pyspark and isinstance(x, __pyspark.sql.DataFrame):
196206
df = x.limit(threshold).toPandas()
197207
return _jupyterlab_variableinspector_getmatrixcontent(df.copy())
198-
elif np and pd and type(x).__name__ == "DataFrame":
208+
elif __np and __pd and type(x).__name__ == "DataFrame":
199209
if threshold is not None:
200210
x = x.head(threshold)
201211
x.columns = x.columns.map(str)
202212
return x.to_json(orient="table", default_handler=_jupyterlab_variableinspector_default, force_ascii=False)
203-
elif np and pd and type(x).__name__ == "Series":
213+
elif __np and __pd and type(x).__name__ == "Series":
204214
if threshold is not None:
205215
x = x.head(threshold)
206216
return x.to_json(orient="table", default_handler=_jupyterlab_variableinspector_default, force_ascii=False)
207-
elif np and pd and type(x).__name__ == "ndarray":
208-
df = pd.DataFrame(x)
217+
elif __np and __pd and type(x).__name__ == "ndarray":
218+
df = __pd.DataFrame(x)
209219
return _jupyterlab_variableinspector_getmatrixcontent(df)
210-
elif tf and (isinstance(x, tf.Variable) or isinstance(x, tf.Tensor)):
211-
df = K.get_value(x)
220+
elif __tf and (isinstance(x, __tf.Variable) or isinstance(x, __tf.Tensor)):
221+
df = __K.get_value(x)
212222
return _jupyterlab_variableinspector_getmatrixcontent(df)
213223
elif isinstance(x, list):
214-
s = pd.Series(x)
224+
s = __pd.Series(x)
215225
return _jupyterlab_variableinspector_getmatrixcontent(s)
216226
217227
@@ -220,7 +230,7 @@ def _jupyterlab_variableinspector_displaywidget(widget):
220230
221231
222232
def _jupyterlab_variableinspector_default(o):
223-
if isinstance(o, np.number): return int(o)
233+
if isinstance(o, __np.number): return int(o)
224234
raise TypeError
225235
226236

0 commit comments

Comments
 (0)