3
3
4
4
from IPython import get_ipython
5
5
from IPython .core .magics .namespace import NamespaceMagics
6
-
7
6
_nms = NamespaceMagics ()
8
7
_Jupyter = get_ipython ()
9
8
_nms .shell = _Jupyter .kernel .shell
10
9
11
10
try :
12
- import numpy as np # noqa: F401
11
+ import numpy as np
13
12
except ImportError :
14
- pass
15
-
13
+ pass
16
14
17
15
def _getsizeof (x ):
18
16
# return the size of variable x. Amended version of sys.getsizeof
@@ -24,22 +22,39 @@ def _getsizeof(x):
24
22
else :
25
23
return getsizeof (x )
26
24
27
-
28
25
def _getshapeof (x ):
29
- # returns the shape of x if it has one
30
- # returns None otherwise - might want to return an empty string for an empty collum
26
+ #returns the shape of x if it has one
27
+ #returns None otherwise - might want to return an empty string for an empty column
31
28
try :
32
29
return x .shape
33
- except AttributeError : # x does not have a shape
30
+ except AttributeError : # x does not have a shape
34
31
return None
35
32
33
+ def _getcontentof (x ):
34
+ length = 150
35
+ if type (x ).__name__ == 'DataFrame' :
36
+ colnames = ', ' .join (x .columns .map (str ))
37
+ content = "Column names: %s" % colnames
38
+ elif type (x ).__name__ == 'Series' :
39
+ content = "Series [%d rows]" % x .shape
40
+ elif type (x ).__name__ == 'ndarray' :
41
+ content = x .__repr__ ()
42
+ else :
43
+ if hasattr (x , '__len__' ):
44
+ if len (x ) > length :
45
+ content = str (x [:length ])
46
+ else :
47
+ content = str (x )
48
+ if len (content ) > 150 :
49
+ return content [:150 ] + " ..."
50
+ return content
36
51
37
52
def var_dic_list ():
38
53
types_to_exclude = ['module' , 'function' , 'builtin_function_or_method' ,
39
54
'instance' , '_Feature' , 'type' , 'ufunc' ]
40
55
values = _nms .who_ls ()
41
- vardic = [{'varName' : v , 'varType' : type (eval (v )).__name__ , 'varSize' : str (_getsizeof (eval (v ))), 'varShape' : str (_getshapeof (eval (v ))) if _getshapeof (eval (v )) else '' , 'varContent' : str (eval (v ))[: 200 ] } # noqa
42
-
56
+ vardic = [{'varName' : v , 'varType' : type (eval (v )).__name__ , 'varSize' : str (_getsizeof (eval (v ))), 'varShape' : str (_getshapeof (eval (v ))) if _getshapeof (eval (v )) else '' , 'varContent' : _getcontentof (eval (v )) } # noqa
57
+
43
58
for v in values if (v not in ['_html' , '_nms' , 'NamespaceMagics' , '_Jupyter' ]) & (type (eval (v )).__name__ not in types_to_exclude )] # noqa
44
59
return json .dumps (vardic )
45
60
0 commit comments