Anyone has a nice "view_var" procedure ?
Stef Mientki
S.Mientki-nospam at mailbox.kun.nl
Tue Jan 16 18:13:37 EST 2007
Adam wrote:
> Stef Mientki wrote:
>
>> hello,
>>
>> Is there some handy/ nice manner to view the properties of some variable ?
>> As a newbie, I often want to see want all the properties of a var,
>> and also some corner values (large arrays) etc.
>>
>> Probably it's not so difficult,
>> but I don't see how to distinguish for example between a string and an
>> array. An array has a shape, a string not etc.
>>
>>
>> thanks,
>> Stef Mientki
>
> I am also a newbie so if this is not what you want I can't give much
> more as of yet.
>
Ok, here's my current solution,
probably not very Pythonian, (so suggestions are welcome)
but it works.
(Too bad I can get the name of the var into a function ;-)
cheers,
Stef Mientki
from scipy import *
#
#############################################################################
def prn ( V ):
# print a single row of an array
def prn_array_row ( R, row ):
if len(R) < 4: print ' Row', row, ':', R
else:
print ' Row', row, ': [', R[0],R[1], '...', R[-2], R[-1], ']'
# print a single element of a list
def prn_list_element ( E ):
if type(E) == int: return ' ' + str(E)
elif type(E) == float: return ' ' + str(E)
elif type(E) == complex: return ' ' + str(E)
elif type(E) == list: return ' [..]'
elif type(E) == tuple: return ' (..)'
elif type(E) == array: return ' ([..])'
else: return ' ??'
print ''
if type(V) == int:
print 'int32 =', V
elif type(V) == float:
print 'float64 =', V
elif type(V) == complex:
print 'complex64 =', V
# LIST
elif (type(V) == list) | (type(V) == tuple):
# count the occurances of the different types
N_int,N_float,N_complex,N_list,N_tuple,N_array,N_unknown =
0,0,0,0,0,0,0
for i in range(len(V)):
if type(V[i]) == int: N_int += 1
elif type(V[i]) == float: N_float += 1
elif type(V[i]) == complex: N_complex += 1
elif type(V[i]) == list: N_list += 1
elif type(V[i]) == tuple: N_tuple += 1
elif type(V[i]) == ndarray: N_array += 1
else: N_unknown += 1
# print the occurances of the different types
# and count the number of types that can easily be displayed
if type(V)==list: line = 'list:'
else: line = 'tuple:'
N = 0
if N_int > 0: line = line + ' N_Int=' + str(N_int) ; N
+= 1
if N_float > 0: line = line + ' N_Float=' + str(N_float) ; N
+= 1
if N_complex > 0: line = line + ' N_Complex=' + str(N_complex) ; N
+= 1
if N_list > 0: line = line + ' N_List=' + str(N_list) ; N
+= 1
if N_tuple > 0: line = line + ' N_Tuple=' + str(N_tuple) ; N
+= 1
if N_array > 0: line = line + ' N_Array=' + str(N_array) ; N
+= 1
if N_unknown > 0: line = line + ' N_Unknown=' + str(N_unknown) ; N
+= 1
if N == 1: line += ' == Homogeneous =='
print line
# check if all elements have the same type
if len(V) < 4:
line = ''
for i in range(len(V)): line += prn_list_element (V[i])
print line
else:
print prn_list_element (V[0]),\
prn_list_element (V[1]),\
' .....',\
prn_list_element (V[-2]),\
prn_list_element (V[-1])
# ARRAY
elif type(V) == ndarray:
print 'Array', V.shape, V.dtype.name
if V.ndim == 1:
prn_array_row ( V, 0 )
elif V.ndim == 2:
if V.shape[1] < 4:
for i in range(V.ndim): prn_array_row ( V[i,:], i )
else:
prn_array_row ( V[0,:], 0 )
prn_array_row ( V[1,:], 1 )
print ' ......'
prn_array_row ( V[-2,:], V.shape[1]-2 )
prn_array_row ( V[-1,:], V.shape[1]-1 )
# MAIN TEST PROGRAM
##########################################################
V1 = 3
L1 = [ 11, 12, 33 ]
L2 = [ 11, 12, ['aap', 34] ]
T1 = ( 11, 12, ('aap', 34) )
T2 = ( 11, ('aap', 34), 5,5,5,5,5,5,8 )
A1 = array ( [1,2,7] )
A2 = array ( [ [1,2,4,4,4,4,4,8], [4,5,5,5,5,5,5,2] ] )
A3 = zeros ((10,10))
prn ( V1 )
prn ( L1 )
prn ( A1 )
prn ( A2 )
prn ( A3 )
prn ( L2 )
prn ( T1 )
prn ( T2 )
More information about the Python-list
mailing list