Welcome, guest | Sign In | My Account | Store | Cart

Small debug utility will help to detect unwonted changes of user's class during program execution.

Python, 125 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#  Copyright (C)  Boris Kats 2011 - 2012.
#  Inspired and based on  Yaniv Aknin "objwalk" http://code.activestate.com/recipes/577982/
#  The small debug utility will help to detect unwonted changes to content of user's class
#  or containers during the program execution. Just call it before and after some part 
#  of program and compare the results. Also, one can dump the contents of the class into
#  files and investigate details. If argument "withValues" of functon "objview" is set to False,
#  just types of members will be printed, instead of values.
#  Any additional type of collection  can be embodied with small helper functions.

import collections
from collections import Mapping, Set, Sequence, namedtuple, deque 
from datetime import datetime
from datetime import date as date
import array

__string_types__ = (str, unicode) if str is bytes else (str, bytes)
__primitiveTypes__ =['int', 'float','bool','date'] + [str(m)[8:-2] for m in __string_types__]

def Round(input,digits):
    if digits and isinstance(input,float):
       return round(input,digits)
    else:
       return input

def class__view(obj):
   ''' helper function for classes'''
   internaldict = dict()
   if hasattr(obj,'_asdict'):
      internaldict = obj._asdict()
   else:
      internaldict  = { member:getattr(obj, member) for member in dir(obj) \
                        if not member.startswith('__') and not callable(getattr(obj, member)) }
   keylist = list(internaldict.keys())
   keylist.sort(key=str.upper)
   res = collections.OrderedDict([(key,internaldict[key]) for key in keylist])
   return res.items()                          

iteritems = lambda mapping: getattr(mapping, 'iteritems', mapping.items)()

def objview(obj,withValues,path = str()):
    ''' That function will iterate recursivlly accross members of class
        or collections, until all primitives are found '''
    if not len(path): path = type(obj).__name__ 
    iterator = None
    if isinstance(obj, Mapping):
         iterator = iteritems
    else: 
         if isinstance(obj, (Sequence,Set,array.array,deque)) \
            and not isinstance(obj,__string_types__)  and not hasattr(obj,'_asdict'):
            iterator = enumerate
         else: 
            if not type(obj).__name__ in __primitiveTypes__:
               iterator = class__view

    if iterator:
            for path_component, value in iterator(obj):
                valuetype = type(value).__name__ 
                nextpath = path + ('[%s]' % str(path_component)) 
                if (not withValues): yield nextpath, valuetype
                for result in objview(value,withValues,nextpath):               
                    if (withValues or (valuetype not in __primitiveTypes__)):  yield result
    else:
        yield path, obj 

def objsignature(obj, *,file = None, precision = 10):
   res = objview(obj,True)
   r_str = '%s %s%s'
   for x in res :
     r_str = r_str %(x[0],str(Round(x[1],precision)), '\n%s %s%s')
   r_str = r_str[:-7]
   if (file):
      debugFile = open(file,'w')
      debugFile.write(r_str)
      debugFile.close()
   return hash(r_str)

   
if __name__ =='__main__':
   myList = [1,2,3,[4,5,6]]
   res = objview(myList,True)
   for x in res :
     print(x[0],' => ',Round(x[1],4))

   class MyRandomClass(object):
       def __init__(self,limit):
        super()
        import random
        self.numbers = [random.random() for i in range(limit)]

   class MyClass(object):
     def __init__(self,name,rd):
        super() 
        self.name = '"MyClass"'
        self.desription = (name,datetime.strptime('01/12/2012','%m/%d/%Y').date(),25)
        self.myList = [1.0,2.0,3.0,[5.0,6.0,7.0]]
        self.myDict = dict(a=1, b=2, c=3, d=set(['one','two','three']))
        Temp = collections.namedtuple('namedtuple','first second third')
        self.myNamedTuple = Temp('red','blue','green')
        self.myTuple = (('white','black'),'yellow')
        self.myFrosenSet = frozenset(['four','five','six'])
        self.myDeque = deque(iter([8.0,9.0,10.0]))
        self.Randoms = rd
        self.MyArray = array.array('f', [3,5,7])

   myrd = MyRandomClass(3)
   mycl = MyClass('The one',myrd) 
   print('Members of %s are:' % type(mycl).__name__)
   res = objview(mycl,True)
   for x in res :
     print(x[0],' => ',Round(x[1],4))

   sig1 = objsignature(mycl,file='before.txt',precision=4)
   print('class signature before',sig1)

   z = myrd.numbers[0]
   myrd.numbers[0] = 1.0
   sig2 = objsignature(mycl,file='after.txt',precision=4)
   print('class signature after',sig2)
   if sig1 != sig2:
      print('The content of class has been changed\n', \
            'compare two files before.txt and after.txt')   

   myrd.numbers[0] = z
   sig3 = objsignature(mycl,precision=4)
   assert(sig1 == sig3)

This is useful utility for debugging and other generic purposes.