<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Zero Mu Tech Articles &#187; Python</title>
	<atom:link href="http://www.techarticles.zeromu.net/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techarticles.zeromu.net</link>
	<description>Solutions to computer problems that were in my way.</description>
	<lastBuildDate>Sun, 09 May 2010 03:48:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Keeping Track of Elapsed Time in Python</title>
		<link>http://www.techarticles.zeromu.net/programming/keeping-track-of-elapsed-time-in-python/</link>
		<comments>http://www.techarticles.zeromu.net/programming/keeping-track-of-elapsed-time-in-python/#comments</comments>
		<pubDate>Wed, 21 May 2008 03:20:49 +0000</pubDate>
		<dc:creator>Jake D</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[MySQLdb]]></category>

		<guid isPermaLink="false">http://www.techarticles.zeromu.net/?p=32</guid>
		<description><![CDATA[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<br/><br/><a href="http://www.techarticles.zeromu.net/programming/keeping-track-of-elapsed-time-in-python/">Read more...</a>]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p><strong>The Quick Answer</strong>: For the most accurrate time elapsed, use the <code>time</code> module and make 2 <code>time.time()</code> objects.  The difference between these objects is the time elapsed. Do not use <code>time.clock()</code>.</p>
<h1>The Whole Story</h1>
<p>Its pretty basic: I have certain parts of a large python script (that happen to access a MySQL database) that I would like to keep track of how long it took them to execute.</p>
<h2>Wrong Answers</h2>
<p>Initially, I read on a <a href="http://pleac.sourceforge.net/pleac_python/datesandtimes.html" target="_blank">PLEAC-Python article, Dates and Times</a> (which really is a <strong>great</strong> overview of Python&#8217;s <code>time</code> module), about some ways to use Python&#8217;s <code>time</code> module.  That article suggests that all you need to do is:</p>
<blockquote><pre class="brush: python;">
#-----------------------------
# High Resolution Timers

t1 = time.clock()
# Do Stuff Here
t2 = time.clock()
print t2 - t1

# 2.27236813618
# Accuracy will depend on platform and OS,
# but time.clock() uses the most accurate timer it can
</pre>
</blockquote>
<p>For one of my projects, that worked fine.  But then I had a bigger script that used a lot of MySQL via the Python module <a href="http://mysql-python.sourceforge.net/MySQLdb.html" target="_blank">MySQLdb</a>, and I would look at my script&#8217;s run time after leaving, and the times looked short&#8230; but not too short. Eventually after running a script that took a 7 hour sleep and then some to run &#8212; but the script reported only taking an hour or so &#8212; I knew something was wrong.</p>
<h2>Only timing the work of Python?</h2>
<p>It seemed as if using the <code>time.clock()</code> approach was only timing what Python (as opposed to MySQL?) was doing.  I created this test script</p>
<pre class="brush: python;">
import time

yearstart = time.clock()
print yearstart

for x in range(0, 1000000):
    z = x + 6

yearend = time.clock()
print yearend
elapsed= yearend-yearstart
min = elapsed/60

print elapsed, min
</pre>
<p>And I didn&#8217;t find a problem, but my script still did.  I was wanting to time an event of a known length, and so I found out about the <code>time.sleep()</code> function from the aforementioned article. I incorporated the <code>sleep()</code> function:</p>
<pre class="brush: python;">
import time

yearstart = time.clock()
print yearstart

time.sleep(3)

yearend = time.clock()
print yearend
elapsed= yearend-yearstart
min = elapsed/60

print elapsed, min
</pre>
<p>and ran the script.  You would expect to see 3 seconds as a result, or at least something close, but my output was:</p>
<pre>0.03
0.03
0.0 0.0</pre>
<p>That was funny (although I didn&#8217;t laugh) because <code>yearend</code> was not supposed to sample the <code>clock()</code> until after the sleep.  I still don&#8217;t know why this does this, but it does.  I am sure this has its uses, but this was not the use I was wanting.</p>
<h2>Use <code>time.time()</code></h2>
<p>Still referencing the PLEAC-Python article, I tried using <code>time.time()</code>, which is supposed to just be the seconds since that day in 1970 and compared it to the <code>time.clock()</code> approach:</p>
<pre class="brush: python;">
import time

yearstart = time.clock()
print yearstart

time.sleep(3)

yearend = time.clock()
print yearend
elapsed= yearend-yearstart
min = elapsed/60

print elapsed, min

yearstart = time.time()
print yearstart

time.sleep(3)

yearend = time.time()
print yearend

elapsed= yearend-yearstart

min = elapsed/60

print elapsed, min
</pre>
<p>And got the output:</p>
<pre>0.03
0.03
0.0 0.0
1211338788.69
1211338791.69
3.00004386902 0.0500007311503</pre>
<h1>No Fluff Answer:</h1>
<p>Read nothing else (on this page). This code will get you going:</p>
<pre class="brush: python;">
import time

start = time.time()

# whatever you want to time, put between these two statements

end = time.time()

elapsed= end - start

#if you want to convert to minutes, just divide
min = elapsed/60

print &quot;Your stuff took&quot;, elapsed, &quot;seconds to run, which is the same as&quot;, min, &quot;minutes&quot;
</pre>
<p>So, this works for me.  I supose that <code>time.clock()</code> does not reference absolute time, which was a problem for me. Hope this helps, or at least saves you some head-scratching.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techarticles.zeromu.net/programming/keeping-track-of-elapsed-time-in-python/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Get rid of WordPress messing with your quotes while writing code</title>
		<link>http://www.techarticles.zeromu.net/programming/get-rid-of-wordpress-messing-with-your-quotes-while-writing-code/</link>
		<comments>http://www.techarticles.zeromu.net/programming/get-rid-of-wordpress-messing-with-your-quotes-while-writing-code/#comments</comments>
		<pubDate>Fri, 02 May 2008 06:45:15 +0000</pubDate>
		<dc:creator>Jake D</dc:creator>
				<category><![CDATA[Fixes/Hacks]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.techarticles.zeromu.net/?p=28</guid>
		<description><![CDATA[<a href="http://www.techarticles.zeromu.net/programming/get-rid-of-wordpress-messing-with-your-quotes-while-writing-code/"><img align="right" hspace="5" width="96" src="http://www.techarticles.zeromu.net/wp-content/uploads/2008/05/picture-2281.png" class="alignright wp-post-image tfe" alt="" title="badcode" /></a>Problem: Recently I was writing an article where I posted some python code.  Using WordPress, I was switching between HTML/Code view and &#8220;Visual&#8221; 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<br/><br/><a href="http://www.techarticles.zeromu.net/programming/get-rid-of-wordpress-messing-with-your-quotes-while-writing-code/">Read more...</a>]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong>: Recently I was writing <a href="http://www.techarticles.zeromu.net/programming/get-attributes-and-filter-out-methods-a-private-attributes/">an article where I posted some python code</a>.  Using WordPress, I was switching between HTML/Code view and &#8220;Visual&#8221; 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:</p>
<p><a href="http://www.techarticles.zeromu.net/wp-content/uploads/2008/05/picture-2281.png"><img class="alignnone size-full wp-image-31" title="badcode" src="http://www.techarticles.zeromu.net/wp-content/uploads/2008/05/picture-2281.png" alt="" width="500" height="30" /></a></p>
<p><strong>Solution</strong>: Not exactly sure, but it seems to have stopped.</p>
<p>There are <a href="http://en.forums.wordpress.com/tags.php?tag=sourcecode">several WordPress posts</a> that go over an issue of using the</p>
<pre class="brush: plain;">[/sourcecode]

tag to write sourcecode.

Personally, I was very frustrated because while writing in Python, where whitespace matters, my indents would all be lost when switching between the visual and HTML editor in WordPress.

My solution was to wrap my

[sourcecode]</pre>
<p>tags in &lt;pre&gt; tags.</p>
<h2>Check the plugin?</h2>
<p>I originally was using the</p>
<pre class="brush: plain;">[/sourcecode]

tag from &lt;a href=&quot;http://code.google.com/p/syntaxhighlighter/&quot;&gt;SyntaxHighlighter&lt;/a&gt;, then found another one, called SyntaxHilighter Plus.

I don't have time to go over all of this, but I disabled the Plus version, re-enabled the regular SyntaxHighlighter, and made sure to wrap my

[sourcecode]</pre>
<p>s in &lt;pre&gt; tags.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techarticles.zeromu.net/programming/get-rid-of-wordpress-messing-with-your-quotes-while-writing-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get attributes and filter out methods a private attributes</title>
		<link>http://www.techarticles.zeromu.net/programming/get-attributes-and-filter-out-methods-a-private-attributes/</link>
		<comments>http://www.techarticles.zeromu.net/programming/get-attributes-and-filter-out-methods-a-private-attributes/#comments</comments>
		<pubDate>Thu, 01 May 2008 23:59:32 +0000</pubDate>
		<dc:creator>Jake D</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.techarticles.zeromu.net/?p=26</guid>
		<description><![CDATA[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&#8217;t<br/><br/><a href="http://www.techarticles.zeromu.net/programming/get-attributes-and-filter-out-methods-a-private-attributes/">Read more...</a>]]></description>
			<content:encoded><![CDATA[<p>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 <code>__members__ </code>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&#8217;t seem to find it (let me know).</p>
<p><strong>Quick Answer (for managed attributes only):</strong></p>
<pre>
<pre class="brush: python;">
class dumb:
    def dummy():
        pass

def attributes(o):
    print &quot;object:&quot;, o
    print &quot;Initialized attribute values:&quot;
    step=0
    for var in dir(o):
        vartype=-1
        exec(&quot;vartype = type(o.&quot;+str(var)+&quot;)&quot;)
        if not(var in o.__dict__) and not(vartype == type( dumb.dummy)) and not(&quot;__&quot; == var[:2]):
            value = -30
            exec(&quot;value = o.&quot;+str(dir(o)[step]))
            print '%40s' % var  +&quot;: &quot;+ str(value)
        step += 1
</pre>
<p><span id="more-26"></span></pre>
<h2>The whole story</h2>
<p>Either I am just really new to python (I am) or it is just really hard when you have an object and you just want to get the public attributes.</p>
<p>The code above uses the <a href="http://diveintopython.org/power_of_introspection/built_in_functions.html"><code>dir()</code></a> function to get everything from the object.  Besides both public and private attributes, this also gets user defined and inherited methods.  Then, i crop out those that are not public attributes.</p>
<h2>Code Walkthrough (Get managed attributes only)</h2>
<p>Managed attributes are those that are defined in an object&#8217;s definition.  Here is an example:</p>
<pre>
<pre class="brush: python;">
class Widget(object):
    &quot;&quot;&quot;The Widget represents an object of work.  it can be processed by a worker. it is assigned to a worker by an organization.&quot;&quot;&quot;
        self.__id=id
        self.test=35

    ### MANAGED ATTRIBUTES ###

    #id
    def id():
        &quot;&quot;&quot;id is readonly&quot;&quot;&quot;
            return self.__id
        return locals()
    id = property(**id())
    #END id

    ### END MANAGED ATTRIBUTES ###

    def spoil_step(self):
        &quot;&quot;&quot;Increases the steps that this widget has been spoiling&quot;&quot;&quot;
        old = self.__time_spoiling
        self.__time_spoiling += 1
        #check for sure
        if old + 1 == self.__time_spoiling:
            return self.__time_spoiling
        else:
            return False

    ####END Widget OBJECT ####
</pre>
</pre>
<p><em>The Following list references the code at the top of the page</em></p>
<ol>
<li>I created a dumb object with a dummy function.  This will be used to identify &#8220;instance methods&#8221;. The <code>pass</code> instruction is just a filler.
<pre>
<pre class="brush: python;">
class dumb:
    def dummy():
        pass
</pre>
</pre>
</li>
<li>I created a function that takes an object, <code>o</code>, as input.  It then just prints out the object and lets the user know that it is about to go through it:
<pre>
<pre class="brush: python;">
def attributes(o):
    print &quot;object:&quot;, o
    print &quot;Initialized attribute values:&quot;
</pre>
</pre>
</li>
<li>I created a variable to keep track of which thing we are looking at, starting at the 0-index:
<pre>
<pre class="brush: python;">
     step=0
</pre>
</pre>
</li>
<li>Then I go through each member of the object:
<pre>
<pre class="brush: python;">
     for var in dir(o):
</pre>
</pre>
</li>
<li>I then create a variable, and assign the type of the object to this variable:
<pre>
<pre class="brush: python;">
          vartype=-1
          exec(&quot;vartype = type(o.&quot;+str(var)+&quot;)&quot;)
</pre>
</pre>
</li>
<li>Next comes the key conditional statement:
<pre>
<pre class="brush: python;">
          if not(var in o.__dict__) and not(vartype == type( dumb.dummy)) and not(&quot;__&quot; == var[:2]):
</pre>
</pre>
<p>The first part says exclude if you find it in <code>o.__dict__</code>, which contains the private attributes from the <code>__init__()</code> function.  The second part excludes things that are of the same type as an instance method; so this gets rid of the object&#8217;s methods.  The last part looks at the first to characters of the thing, and if it is equal to <code>__,</code> it is excluded.</li>
<li>Within the conditional, we just name and value of that thing, which should be only managed attributes:
<pre>
<pre class="brush: python;">
               value = -30
               exec(&quot;value = o.&quot;+str(dir(o)[step]))
               print '%40s' % var  +&quot;: &quot;+ str(value)
</pre>
</pre>
</li>
</ol>
<p>The next page goes over how to get both the managed and public (not just the managed) attributes from an object</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techarticles.zeromu.net/programming/get-attributes-and-filter-out-methods-a-private-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
