Archive for the ‘Python’ Category

Keeping Track of Elapsed Time in Python

Tuesday, May 20th, 2008

This article shows how to put a few lines of python code into your python script so you can tell how long the script has been running, or how long a certain part of the task took to run.

The Quick Answer: For the most accurrate time elapsed, use the time module and make 2 time.time() objects.  The difference between these objects is the time elapsed. Do not use time.clock().

Read the rest of this article »


Get rid of WordPress messing with your quotes while writing code

Thursday, May 1st, 2008

Problem: Recently I was writing an article where I posted some python code.  Using WordPress, I was switching between HTML/Code view and “Visual” view.  Every time that I switched, the WordPress editor would change some of my code, namely the quotes.  I ended up with something that looked like this:

Solution: Not exactly sure, but it seems to have stopped.

Read the rest of this article »


Get attributes and filter out methods a private attributes

Thursday, May 1st, 2008

This articles covers how I was able to get the managed attributes from an object, and then get the public and managed attributes from an object.  There used to be an __members__ method, but that has been depreciated according to http://docs.python.org/lib/specialattrs.html. Really, I hope there is a better way to do this, but I can’t seem to find it (let me know).

Quick Answer (for managed attributes only):

class dumb:
    def dummy():
        pass

def attributes(o):
    print "object:", o
    print "Initialized attribute values:"
    step=0
    for var in dir(o):
        vartype=-1
        exec("vartype = type(o."+str(var)+")")
        if not(var in o.__dict__) and not(vartype == type( dumb.dummy)) and not("__" == var[:2]):
            value = -30
            exec("value = o."+str(dir(o)[step]))
            print '%40s' % var  +": "+ str(value)
        step += 1

Read the rest of this article »