|
#!/usr/bin/python |
|
""" pr( ... x= y= ... ), probj() for testing """ |
|
# must be thousands such |
|
# use with https://docs.python.org/2.7/library/logging.html ? |
|
|
|
from __future__ import division |
|
import numpy as np |
|
|
|
__version__ = "2016-03-06 mar denis" |
|
|
|
#............................................................................... |
|
def pr( *args, **kwargs ): |
|
""" pr( "stage 42:", ntop=ntop, df=df ... ) |
|
probj each arg / kwarg, summary line / ndarray |
|
apply liberally |
|
""" |
|
print "" |
|
for arg in args: |
|
probj( arg ) |
|
for key, val in sorted( kwargs.items() ): |
|
# sorted, else random not caller's order |
|
probj( val, key ) |
|
# print "" |
|
|
|
#............................................................................... |
|
def probj( x, nm="" ): |
|
""" print any ? obj: |
|
string scalar list tuple dict: 1 summary line "nm: list len 42" |
|
pandas head() tail() with pd.set_option |
|
numpy ndarray with user's np.set_printoptions |
|
""" |
|
# sure to be buggy |
|
if nm: |
|
print "-- %s:" % nm , |
|
|
|
if x is None or _isstr( x ): |
|
print x |
|
return |
|
if np.isscalar( x ) \ |
|
or (hasattr( x, "ndim" ) and x.ndim == 0): # np.array( 3 ) |
|
print "%.6g" % x |
|
return |
|
|
|
if hasattr( x, "name" ) and x.name is not None: # pandas |
|
print x.name , |
|
|
|
# type / classname, shape / len -- |
|
t = getattr( x, "__class__", type(x) ) |
|
print t.__name__ , |
|
if hasattr( x, "shape" ): |
|
print x.shape |
|
n = x.shape[0] |
|
else: |
|
try: |
|
n = len(x) |
|
print "len %d" % n |
|
except (TypeError, AttributeError): # len() of unsized object ? |
|
n = np.NaN |
|
print "" |
|
|
|
if isinstance( x, (tuple, list) ): |
|
return |
|
if isinstance( x, dict ): |
|
print " keys:", sorted( x.keys() [:10] ) |
|
return |
|
|
|
# pandas DataFrame, Series etc. |
|
# (to print as ndarray, pr( df.values )) |
|
if hasattr( x, "head" ): |
|
if n <= 10: |
|
print x.head( n ) # with user's pd.set_option( max_rows max_cols ... ) |
|
else: |
|
print x.head( 3 ) |
|
print "..." |
|
print x.tail( 3 ) |
|
print "" |
|
return |
|
|
|
if hasattr( x, "values" ): |
|
x = x.values |
|
if hasattr( x, "dtype" ): # np array kind O ? |
|
print x # with user's np.set_printoptions |
|
print "" |
|
|
|
|
|
def _isstr( x ): |
|
""" basestring or np.array( "str" ) """ |
|
return isinstance( x, basestring ) \ |
|
or np.issubdtype( getattr( x, "dtype", "i4"), np.string_ ) # ? |
|
|
|
|
|
#............................................................................... |
|
if __name__ == "__main__": |
|
from collections import namedtuple |
|
|
|
np.set_printoptions( threshold=100, edgeitems=10, linewidth=140, |
|
formatter = dict( float = lambda x: "%.2g" % x )) # float arrays %.2g |
|
# pd.set_option( "display.width", 140, "display.precision", 2 ) |
|
|
|
class C: |
|
pass |
|
c = C() |
|
Namedtuple = namedtuple( "Namedtuple", "x y" ) |
|
|
|
pr( |
|
adict = { 1:2, 3:4 }, |
|
alist = [1, 2], |
|
array0d = np.array( 3 ), |
|
none = None, |
|
arraynone = [None], |
|
arraystr = np.array("string"), |
|
array2 = np.array([ "string", 3 ]), |
|
eye = np.eye( 3 ) * np.pi, |
|
pi = np.pi, |
|
arraypi = np.array( np.pi ), |
|
s = "string", |
|
|
|
C=C, |
|
c=c, |
|
|
|
Namedtuple = Namedtuple, |
|
anamedtuple = Namedtuple( 1, 2 ), |
|
) |
|
|