Get attributes and filter out methods a private attributes

May 1, 2008 – 3:59 pm
by Jake D

Attribute Summary

This function takes an object for an input, and prints out a summary:

class dumb:
    def dummy():
        pass

def func():
    pass

def attributes(o):
    print " "
    print " ++++ Starting attributes checking ++++"
    print "object:", o
    #print "Initialized attribute values:"
    step=0
    publics=[]
    privates=[]
    system = []
    readable=0
    for var in dir(o):
        vartype=-1
        exec("vartype = type(o."+str(var)+")")
        class_name_length = len(str(o.__class__)) - 19
        compare1 = "<class '__main__."+var[1:class_name_length + 1]+"'>"
        compare2 = str(o.__class__)
        #print compare1
        #print compare2
        if not(compare1 == compare2) and not(vartype == type( dumb.dummy)) and not("__" == var[:2] and "__" == var[-2:]) and not(vartype == type( func)):
            publics.append(var)
        elif compare1 == compare2 and not(vartype == type( dumb.dummy)) and not(vartype == type( func)):
            privates.append(var)
        elif "__" == var[:2] and "__" == var[-2:] and not(vartype == type( dumb.dummy)) and not(vartype == type( func)):
            system.append(var)
        step += 1
    #print " "
    #print "Results from attempt to assign a value:"
    print "Results from attempt to read a value:"
    readable=[]
    for var in publics:
        #exec("o."+ str(var) +" = 100")
        result="Error!"
        value="error..."
        try:
            exec("value = o."+str(var))
            result = "Ok"
            print '%40s' % var  +": "+ "("+result + ") "+ str(value)
            readable.append(var)
        except:
            result = "Cannot Read!!!"
            print '%40s' % var  +": "+ str(result)

    print ""
    writable=[]
    for var in publics:
        #exec("o."+ str(var) +" = 100")
        result="Error!"
        try:
            exec("o."+ str(var)+" = 100")
            result = "Can Write"
            writable.append(var)
        except:
            result = "Protected (can't write)"
        #print '%40s' % var  +": "+ str(result)

    #print ""

    #print "Results from attempt to delete a value:"
    deleteable=[]
    for var in publics:
        #exec("o."+ str(var) +" = 100")
        result="Error!"
        try:
            exec("del o."+ str(var))
            result = "Deleteable"
            deleteable.append(var)
        except:
            result = "Cannot Delete"
        #print '%40s' % var  +": "+ str(result)

    print "===== ATTRIBUTE SUMMARY FOR OBJECT OF TYPE "+str(type(o))+" ============"
    print "Total public attributes:", len(publics)
    print len(readable), "are readable", readable
    print len(writable), "are writable", writable
    print len(deleteable), "are deleteable", deleteable
    print ""
    print "Total private attributes:", len(privates), privates
    print ""
    print "Total system attributes:", len(system), system

    return {"public": publics, "private" : privates, "system": system}

Getting methods

This code gets the methods and returns (and prints) a dictionary of public, private, and system methods. (not most thoroughly tested, and probably not that efficient):

def methods(o):
    print " "
    print " ++++ Starting methods checking ++++"
    print "object:", o
    #print "Initialized attribute values:"
    step=0
    publics=[]
    privates=[]
    system=[]
    readable=0
    for var in dir(o):
        vartype=-1
        exec("vartype = type(o."+str(var)+")")
        class_name_length = len(str(o.__class__)) - 19
        compare1 = "<class '__main__."+var[1:class_name_length + 1]+"'>"
        compare2 = str(o.__class__)
        #print compare1
        #print compare2
        if not(compare1 == compare2) and vartype == type( dumb.dummy) and not("__" == var[:2] and "__" == var[-2:]):
            publics.append(var)
        elif compare1 == compare2 and vartype == type( dumb.dummy):
            privates.append(var)
        elif "__" == var[:2] and "__" == var[-2:] and vartype == type( dumb.dummy):
            system.append(var)
        step += 1

    print "===== Method SUMMARY FOR OBJECT OF TYPE "+str(type(o))+" ============"
    print "Total public methods:", len(publics), publics
    print "Total private methods:", len(privates), privates
    print "Total system methods:", len(system), system

    return {"public": publics, "private" : privates, "system": system}

The next page shows an object that I used, and the results of these functions.

Pages: 1 2 3 4



Rate this article:


(No Ratings Yet)
Loading ... Loading ...

Advertisement:




Post a Comment