Anyone has a nice "view_var" procedure ?

Stef Mientki S.Mientki-nospam at mailbox.kun.nl
Wed Jan 17 23:46:49 CET 2007


hi Gabriel,

> It appears that you're a number crunching guy - you absolutely ignore 
> all types except numeric ones! :)
Is there anything else than numbers ? ;-)
But indeed I forgot the else statement, which should look like this:
   else:
     line = 'UNKNOWN: ' + type(V)
     print line
     line = '  ' + V.__repr__()
     # do something to prevent too long output sequences
     print line

> 
> 
>>      # 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
> 
> Ouch, seven counters... your guts should be saying that this is 
> candidate for a more structured type... what about a dictionary indexed 
> by the element type?
> Using a dict:
> 
>      for item in V:
>         t = type(item)
>         try: count[t] = count[t]+1
>         except IndexError: count[t] = 1
Thanks very much, that's almost perfect, I only had to change IndexError into KeyError.
> 
> 
> We'll replace all of this with:
> 
>      for key,value in count:
>         line += ' %s=%d' % (key, value)
> 
I didn't succeed to use value as a enumerator, and I had to typecast key into a string (and skipping 
some redundant information, but then it works great, thanks !!
here the complete listing:
     count = {}
     for item in V:
       t = type(item)
       try: count[t] += 1
       except KeyError: count[t] = 1
     if type(V)==list: line = 'list:'
     else: line = 'tuple:'
     for key in count: line += '  %s=%d' %('N_'+str(key)[7:-2],count[key])
     print line

cheers,
Stef Mientki



More information about the Python-list mailing list