33
44from IPython import get_ipython
55from IPython .core .magics .namespace import NamespaceMagics
6-
76_nms = NamespaceMagics ()
87_Jupyter = get_ipython ()
98_nms .shell = _Jupyter .kernel .shell
109
1110try :
12- import numpy as np # noqa: F401
11+ import numpy as np
1312except ImportError :
14- pass
15-
13+ pass
1614
1715def _getsizeof (x ):
1816 # return the size of variable x. Amended version of sys.getsizeof
@@ -24,22 +22,39 @@ def _getsizeof(x):
2422 else :
2523 return getsizeof (x )
2624
27-
2825def _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
3128 try :
3229 return x .shape
33- except AttributeError : # x does not have a shape
30+ except AttributeError : # x does not have a shape
3431 return None
3532
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
3651
3752def var_dic_list ():
3853 types_to_exclude = ['module' , 'function' , 'builtin_function_or_method' ,
3954 'instance' , '_Feature' , 'type' , 'ufunc' ]
4055 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+
4358 for v in values if (v not in ['_html' , '_nms' , 'NamespaceMagics' , '_Jupyter' ]) & (type (eval (v )).__name__ not in types_to_exclude )] # noqa
4459 return json .dumps (vardic )
4560
0 commit comments