<?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>sean{e}lavelle.com</title>
	<atom:link href="http://www.seanelavelle.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.seanelavelle.com</link>
	<description>Coding, Photography, Travel and whatever else is exciting to me these days</description>
	<lastBuildDate>Fri, 04 May 2012 12:00:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Make os.stat times readable in Python</title>
		<link>http://www.seanelavelle.com/2012/05/04/make-os-stat-times-readable-in-python/</link>
		<comments>http://www.seanelavelle.com/2012/05/04/make-os-stat-times-readable-in-python/#comments</comments>
		<pubDate>Fri, 04 May 2012 12:00:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=124</guid>
		<description><![CDATA[I find myself needing to get file creation or modification times a lot. This is pretty trivial with using Python&#8217;s built in os.stat function. The problem is that it returns the file modification time in seconds since the epoch, which &#8230; <a href="http://www.seanelavelle.com/2012/05/04/make-os-stat-times-readable-in-python/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I find myself needing to get file creation or modification times a lot.  This is pretty trivial with using Python&#8217;s built in os.stat function.  The problem is that it returns the file modification time in seconds since the <a href="http://en.wikipedia.org/wiki/Unix_epoch">epoch</a>, which is not exactly the friendliest format to read.  Here is an example:</p>
<pre class="prettyprint">
>>> import os
>>> file = "H:\SSDC\common_functions\common_functions.py"
>>> modTime = os.stat(file).st_mtime
>>> print modTime
1334325015.57
</pre>
<p>Now more power to you if you can look at that and get the date out of there, but I prefer a little more standard notation.</p>
<p>Luckily, this is doable.  We need to first convert the epoch seconds that os.stat returned into a time_struct and then we can print that time_struct in a more readable manner.</p>
<p>Here is an example using the same file above:</p>
<pre class="prettyprint">
>>> import time
>>> modTime2 = time.gmtime(os.stat(file).st_mtime)
>>> print modTime2
time.struct_time(tm_year=2012, tm_mon=4, tm_mday=13, tm_hour=13, tm_min=50, tm_sec=15, tm_wday=4, tm_yday=104, tm_isdst=0)
>>> modTime2_hr = time.strftime("%m/%d/%Y %H:%M:%S", modTime2)
>>> print modTime2_hr
04/13/2012 13:50:15
</pre>
<p>References:<br />
<a href="http://docs.python.org/library/os.html#os.stat">Python Standard Library &#8211; os.stat</a><br />
<a href="http://docs.python.org/library/time.html">Python Standard Library &#8211; time module</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/05/04/make-os-stat-times-readable-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading a file N lines at a time with Python (using Generators!)</title>
		<link>http://www.seanelavelle.com/2012/04/30/reading-a-file-n-lines-at-a-time-with-python-using-generators/</link>
		<comments>http://www.seanelavelle.com/2012/04/30/reading-a-file-n-lines-at-a-time-with-python-using-generators/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 12:00:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=117</guid>
		<description><![CDATA[Recently found myself needing to read in a file 4 lines at a time. The solution to this problem turned out to nicely illustrate a concept that I&#8217;d read about but never really understood &#8211; generators. Let&#8217;s take a look &#8230; <a href="http://www.seanelavelle.com/2012/04/30/reading-a-file-n-lines-at-a-time-with-python-using-generators/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently found myself needing to read in a file 4 lines at a time.  The solution to this problem turned out to nicely illustrate a concept that I&#8217;d read about but never really understood &#8211; generators. Let&#8217;s take a look at the problem:</p>
<p>Say we have some data in a file like this:</p>
<pre>
entry1:name
entry1:description
entry1:reference_number
---
entry2:name
entry2:description
entry2:reference_number
---
entry3:name
entry3:description
entry3:reference_number
---
</pre>
<p>So the easiest way to deal with this would be to read it in 4 lines at a time and deal with each entry as a unique entity with 3 attributes.</p>
<p>Now we could just read in until we hit a record separator (&#8216;&#8212;&#8217;) but using generators we can have a much more general purpose solution.</p>
<p>First we&#8217;ll need to define our generator:</p>
<pre class="prettyprint">
def chunks(l, n):
    #Generator to yield n sized chunks from l
    for i in xrange(0, len(l), n):
        yield l[i: i + n]
</pre>
<p>What this little bit of code does is return an iterator object that iterates over our list object &#8216;l&#8217; (in this case a file) and returns &#8216;n&#8217; chunks from it at a time.</p>
<p>We&#8217;d use it to read our example like so:</p>
<pre class="prettyprint">
items = []
fileContents = open(file, 'r').readlines()
for entry in chunks(fileContents, 4):
    items.append(entry[0], entry[1], entry[2])
</pre>
<p>What we did here is to first read our file into memory as one big list separated by newlines. We then used our generator to return the file in 4 line blocks and shoved the relevant information (ie everything beside the record separator) into a list called items.  Now we have a list where each item is a tuple of name, description and reference_number.</p>
<p>Hopefully this helps clear up generators and provides a concrete usage for them.</p>
<p>Link to the Stack Overflow answer that gave me the generator code: <a href="http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python/312464#312464"> http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python/312464#312464</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/04/30/reading-a-file-n-lines-at-a-time-with-python-using-generators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking For a Valid IP in Python</title>
		<link>http://www.seanelavelle.com/2012/04/16/checking-for-a-valid-ip-in-python/</link>
		<comments>http://www.seanelavelle.com/2012/04/16/checking-for-a-valid-ip-in-python/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 09:32:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=113</guid>
		<description><![CDATA[I&#8217;ve been working to clean up some utility functions that I use a lot and one thing that I do is check for string to see if it is a valid IP v4 address. I have a simple function for &#8230; <a href="http://www.seanelavelle.com/2012/04/16/checking-for-a-valid-ip-in-python/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working to clean up some utility functions that I use a lot and one thing that I do is check for string to see if it is a valid IP v4 address.  I have a simple function for this which is by no means exhaustive, but it covers my needs most of the time:</p>
<pre class="prettyprint">
def isIP(address):
    #Takes a string and returns a status if it matches
    #a ipv4 address
    # 0 = false / 1 = true
    ip = False
    try:
        if address[0].isdigit():
            octets = address.split('.')
            if len(octets) == 4:
                ipAddr = "".join(octets)
                if ipAddr.isdigit():
                #correct format
                    if (int(octets[0]) >= 0) and (int(octets[0]) <= 255):
                        if (int(octets[1]) >= 0) and (int(octets[1]) <= 255):
                            if (int(octets[2]) >= 0) and (int(octets[2]) <= 255):
                                if (int(octets[3]) >= 0) and (int(octets[3]) <= 255):
                                    ip = True
    except IndexError:
        pass
    return ip
</pre>
<p>As you can see, it just takes a string and checks if it meets the form of X.X.X.X and then checks to see if the values fall in the legal (0-255) range and returns True or False.  As I said, by no means a complete check on the IPv4 spec, but it covered my needs.</p>
<p>Just out of curiosity, I also wrote up a function to do the same check except using the socket library in Python:</p>
<pre class="prettyprint">
import socket

def isIP_v2(address):
    try:
        socket.inet_aton(address)
        ip = True
    except socket.error:
        ip = False

    return ip
</pre>
<p>I then wrote up a quick script to check how fast each one was.  I assumed mine would be faster since I am doing a simpler check, but wanted to see by how much.  I ran it against a list of IP address and non IP address (41,715 real IPs, 10,905 bad addresses) and I was pretty surprised by the results:</p>
<p>PS H:\SSDC\common_functions> C:\Python27\python.exe .\ip_checker.py<br />
<strong>Custom Function</strong><br />
Found 41715 Good IPs<br />
Found 10905 Bad IPs<br />
<strong>Time: 0.318000078201</strong><br />
<strong>Socket Function</strong><br />
Found 41715 Good IPs<br />
Found 10905 Bad IPs<br />
<strong>Time: 0.069000005722</strong><br />
PS H:\SSDC\common_functions> C:\Python27\python.exe .\ip_checker.py<br />
<strong>Custom Function</strong><br />
Found 41715 Good IPs<br />
Found 10905 Bad IPs<br />
<strong>Time: 0.31299996376</strong><br />
<strong>Socket Function</strong><br />
Found 41715 Good IPs<br />
Found 10905 Bad IPs<br />
<strong>Time: 0.069000005722</strong><br />
PS H:\SSDC\common_functions> C:\Python27\python.exe .\ip_checker.py<br />
<strong>Custom Function</strong><br />
Found 41715 Good IPs<br />
Found 10905 Bad IPs<br />
<strong>Time: 0.311000108719</strong><br />
<strong>Socket Function</strong><br />
Found 41715 Good IPs<br />
Found 10905 Bad IPs<br />
<strong>Time: 0.0700001716614</strong></p>
<p>You can see from this (admittedly non-scientific) test that the built in socket test was more then 4 times faster in each test!</p>
<p>So not only is the built in socket function much easier to read, it is significatly faster!  I guess it goes back to the old adage, Use built in functions whenever possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/04/16/checking-for-a-valid-ip-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scripting Splunk Alerts with Python</title>
		<link>http://www.seanelavelle.com/2012/04/11/scripting-splunk-alerts-with-python/</link>
		<comments>http://www.seanelavelle.com/2012/04/11/scripting-splunk-alerts-with-python/#comments</comments>
		<pubDate>Wed, 11 Apr 2012 11:35:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=105</guid>
		<description><![CDATA[Occasionally for one reason or another you may need to call a script when an alert triggers within Splunk. This is pretty easy with Python as long as you keep a couple things in mind. First, is that one the &#8230; <a href="http://www.seanelavelle.com/2012/04/11/scripting-splunk-alerts-with-python/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Occasionally for one reason or another you may need to call a script when an alert triggers within Splunk.  This is pretty easy with Python as long as you keep a couple things in mind.  First, is that one the script is called via the Splunk alert, all the important data is passed from via environmental variables.</p>
<p>Reading these is fairly straight forward using the os module:</p>
<pre class="prettyprint">
import os

#Read the environment variables that Splunk has passed to us
scriptName = os.environ['SPLUNK_ARG_0']
numberEventsReturned = os.environ['SPLUNK_ARG_1']
searchTerms = os.environ['SPLUNK_ARG_2']
queryString = os.environ['SPLUNK_ARG_3']
searchName = os.environ['SPLUNK_ARG_4']
triggerReason = os.environ['SPLUNK_ARG_5']
browserUrl = os.environ['SPLUNK_ARG_6']
rawEventsFile = os.environ['SPLUNK_ARG_8']
</pre>
<p>You can find more information on what the environmental variable contain here : <a href="http://docs.splunk.com/Documentation/Splunk/latest/Admin/Configurescriptedalerts">http://docs.splunk.com/Documentation/Splunk/latest/Admin/Configurescriptedalerts</a></p>
<p>The other thing to keep in mind is that Splunk does not actually pass the actual data that generated the alert.  That data is stored in an file (a gzipped csv (at least on unix)) and the location of the file is passed to us.  So if we need to action the actual alert data within our script we will need to read the events out of the file.  Luckily again, this is pretty straight forward with the csv and gzip modules in the standard library:</p>
<pre class="prettyprint">
import gzip
import csv

logFile = open('/tmp/splunk_alert_events', 'a')

#We got the file name from the envioenment vars
eventFile = csv.reader(gzip.open(rawEventsFile, 'rb'))
for line in eventFile:
logFile.write(line)

logFile.close()
</pre>
<p>Putting it all together, we get this:</p>
<pre class="prettyprint">
import os
import csv
import gzip

if __name__ == "__main__":

#Read the environment variables that Splunk has passed to us
scriptName = os.environ['SPLUNK_ARG_0']
numberEventsReturned = os.environ['SPLUNK_ARG_1']
searchTerms = os.environ['SPLUNK_ARG_2']
queryString = os.environ['SPLUNK_ARG_3']
searchName = os.environ['SPLUNK_ARG_4']
triggerReason = os.environ['SPLUNK_ARG_5']
browserUrl = os.environ['SPLUNK_ARG_6']
rawEventsFile = os.environ['SPLUNK_ARG_8']

logFile = open('/tmp/splunk_alert_events', 'a')

#We got the file name from the envioenment vars
eventFile = csv.reader(gzip.open(rawEventsFile, 'rb'))
for line in eventFile:
logFile.write(line)

logFile.close()
</pre>
<p>Saving this script in $SPLUNK_HOME/bin/scripts and set the alert to call the script (without the path; all scripts are assumed to run from $SPLUNK_HOME/bin/scripts) and you now have a script that will right the events that caused the alert to a file in /tmp.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/04/11/scripting-splunk-alerts-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find a String in Windows Files</title>
		<link>http://www.seanelavelle.com/2012/04/09/find-a-string-in-windows-files/</link>
		<comments>http://www.seanelavelle.com/2012/04/09/find-a-string-in-windows-files/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 09:35:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=99</guid>
		<description><![CDATA[I&#8217;ve recently found myself having to find certain strings in files on Windows. Other then making me long for grep, this has really pointed out how poor the explorer search implementation on windows is. The easiest and most useful way &#8230; <a href="http://www.seanelavelle.com/2012/04/09/find-a-string-in-windows-files/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently found myself having to find certain strings in files on Windows.  Other then making me long for grep, this has really pointed out how poor the explorer search implementation on windows is.  The easiest and most useful way I&#8217;ve run across to accomplish this is to use findstr from the command line.  Findstr is sort of like grep in that it will allow you to search a file (or more importantly recursively search files and directories) for a fixed string or a regular expression.</p>
<p>Here is an example:<br />
<code><br />
c:\Temp&gt;findstr /s /p /C:"&lt;TITLE&gt;" *.htm<br />
readme.htm:&lt;TITLE&gt;Readme for Visual Studio 2010&lt;/TITLE&gt;...<br />
Setup\readme.htm:&lt;TITLE&gt;Readme for Visual Studio 2010&lt;/TITLE&gt;...<br />
c:\Temp&gt;<br />
</code><br />
The flags are a little confusing, but basically what we are saying is seach all files and subirectories recursively (/s) for the string <code>&lt;TITLE&gt; (/C:"&lt;TITLE&gt;")</code> on any file named [something].htm and ignore files with non-printable characters (/p).</p>
<p>Sort of ugly, but it gets the job done.  </p>
<p>You can use regular expression in your search, but it is a subset of what grep can use.</p>
<p>Details and all flags can be found on the MSDN page here: <a href="http://msdn.microsoft.com/en-us/library/bb490907.aspx">http://msdn.microsoft.com/en-us/library/bb490907.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/04/09/find-a-string-in-windows-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Query MSSQL Linked Server Credential Information</title>
		<link>http://www.seanelavelle.com/2012/03/28/query-mssql-linked-server-credential-information/</link>
		<comments>http://www.seanelavelle.com/2012/03/28/query-mssql-linked-server-credential-information/#comments</comments>
		<pubDate>Wed, 28 Mar 2012 12:00:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Database]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=89</guid>
		<description><![CDATA[We recently had a team member go to a different job and one of the tasks that I had was to verify that none of our linked servers in Microsoft SQL server were using his credentials for creating the link. &#8230; <a href="http://www.seanelavelle.com/2012/03/28/query-mssql-linked-server-credential-information/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We recently had a team member go to a different job and one of the tasks that I had was to verify that none of our linked servers in Microsoft SQL server were using his credentials for creating the link.  After a little while of opening the linked server and checking the credentials in the Security tab, I thought that there had to be a better way to do this.  I googled around a bit and poking through the system tables, I came up with this query:</p>
<pre class="prettyprint">
SELECT
    serv.NAME,
    serv.product,
    serv.provider,
    serv.data_source,
    serv.catalog,
    prin.name,
    ls_logins.uses_self_credential,
    ls_logins.remote_name
FROM
    sys.servers AS serv
    LEFT JOIN sys.linked_logins AS ls_logins
    ON serv.server_id = ls_logins.server_id
    LEFT JOIN sys.server_principals AS prin
    ON ls_logins.local_principal_id = prin.principal_id
</pre>
<p>Run it on [master] and you get a list of the linked server usernames.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/03/28/query-mssql-linked-server-credential-information/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started With Python List Comprehensions</title>
		<link>http://www.seanelavelle.com/2012/03/26/getting-started-with-python-list-comprehensions/</link>
		<comments>http://www.seanelavelle.com/2012/03/26/getting-started-with-python-list-comprehensions/#comments</comments>
		<pubDate>Mon, 26 Mar 2012 12:00:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=84</guid>
		<description><![CDATA[I&#8217;ve been hearing people talk about how useful list comprehensions in Python are for a while now, but it never really seemed to click until recently. When it did, I find myself using them all the time. Basically list comprehnsion &#8230; <a href="http://www.seanelavelle.com/2012/03/26/getting-started-with-python-list-comprehensions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been hearing people talk about how useful list comprehensions in Python are for a while now, but it never really seemed to click until recently.  When it did, I find myself using them all the time.  Basically list comprehnsion is a way to create a list from a list.  It&#8217;s fast and makes a lot of sense once you see it in action.</p>
<p>One thing I find myself doing a lot is checking if any member of a list of strings is within another string.  We should easily write this as a loop:</p>
<pre class="prettyprint">
>>> myList = ['a.html', 'b.html', 'c.html']
>>> myString = "www.someserver.com/dir1/b.html"
>>> for item in myList:
    if item in myString:
        print "Found %s in %s" % (item, myString)

Found b.html in www.someserver.com/dir1/b.html
>>>
</pre>
<p>Not too bad, but we can write it as a list comprehension even more succinctly:</p>
<pre class="prettyprint">
>>> myList = ['a.html', 'b.html', 'c.html']
>>> myString = "www.someserver.com/dir1/b.html"
>>> myHits = [x for x in myList if x in myString]
>>> myHits
['b.html']
>>>
</pre>
<p>We can even use the whole list comprehension as a test:</p>
<pre class="prettyprint">
>>> myString2 = 'www.someserver.com/dir2/d.html'

>>> if [x for x in myList if x in myString]:
    print "Got a hit on %s" % myString

Got a hit on www.someserver.com/dir1/b.html

>>> if [x for x in myList if x in myString2]:
    print "Got a hit on %s" % myString2
else:
    print "No hits on %s" % myString2

No hits on www.someserver.com/dir2/d.html
</pre>
<p>I know this seems like a somewhat trivial example, but once you start replacing loops with list comprehensions you can see the uses all over the place.  Here are some more good examples of using list comprehensions:</p>
<p><a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" title="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">http://docs.python.org/tutorial/datastructures.html#list-comprehensions</a><br />
<a href="http://docs.python.org/howto/functional.html#generator-expressions-and-list-comprehensions">http://docs.python.org/howto/functional.html#generator-expressions-and-list-comprehensions</a><br />
<a href="http://www.siafoo.net/article/52">http://www.siafoo.net/article/52</a> (Section 2 deals with list comprehensions)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/03/26/getting-started-with-python-list-comprehensions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python IDE Setup</title>
		<link>http://www.seanelavelle.com/2012/01/08/python-ide-setup/</link>
		<comments>http://www.seanelavelle.com/2012/01/08/python-ide-setup/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 14:14:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=71</guid>
		<description><![CDATA[I occasionally get some questions about how I do my python development and though I would share it. &#160; For my main IDE on my windows box I use Eclipse with the PyDev module installed: http://marketplace.eclipse.org/content/pydev-python-ide-eclipse &#160; Since most of &#8230; <a href="http://www.seanelavelle.com/2012/01/08/python-ide-setup/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I occasionally get some questions about how I do my python development and though I would share it.</p>
<p>&nbsp;</p>
<p>For my main IDE on my windows box I use Eclipse with the PyDev module installed:</p>
<p><a href="http://marketplace.eclipse.org/content/pydev-python-ide-eclipse" target="_blank">http://marketplace.eclipse.org/content/pydev-python-ide-eclipse</a></p>
<p><a href="http://www.seanelavelle.com/wp-content/uploads/2012/01/image002.jpg"><img class="alignnone size-medium wp-image-72" title="image002" src="http://www.seanelavelle.com/wp-content/uploads/2012/01/image002-300x225.jpg" alt="" width="300" height="225" /></a></p>
<p>&nbsp;</p>
<p>Since most of my development is on Linux, I have the Remote System Explorer plugin installed which lets me create and edit files on our linux server over sftp like they were local to my workstation:</p>
<p><a href="http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.rse.doc.user/gettingstarted/g1installing.html" target="_blank">http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.rse.doc.user/gettingstarted/g1installing.html</a></p>
<p><a href="http://www.seanelavelle.com/wp-content/uploads/2012/01/image004.png"><img class="alignnone size-medium wp-image-73" title="image004" src="http://www.seanelavelle.com/wp-content/uploads/2012/01/image004-154x300.png" alt="" width="154" height="300" /></a></p>
<p>&nbsp;</p>
<p>Last but not least, I installed Eclipse Color Theme which is an easy way to install themes for eclipse:</p>
<p><a href="http://marketplace.eclipse.org/content/eclipse-color-theme" target="_blank">http://marketplace.eclipse.org/content/eclipse-color-theme</a></p>
<p>I even when an made a theme (kind of Borland blue, not shown above) you can download here:</p>
<p><a href="http://www.eclipsecolorthemes.org/?view=theme&amp;id=4514" target="_blank">http://www.eclipsecolorthemes.org/?view=theme&amp;id=4514</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/01/08/python-ide-setup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Speeding Python Up With Pypy</title>
		<link>http://www.seanelavelle.com/2012/01/06/66/</link>
		<comments>http://www.seanelavelle.com/2012/01/06/66/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 14:02:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=66</guid>
		<description><![CDATA[I&#8217;ve been working on some code that will use some supplied regular expressions to search through log files (I know, regex isn&#8217;t that efficient, yadda, yadda, yadda, but these were the requirements). The issue I was running into was that &#8230; <a href="http://www.seanelavelle.com/2012/01/06/66/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on some code that will use some supplied regular expressions to search through log files (I know, regex isn&#8217;t that efficient, yadda, yadda, yadda, but these were the requirements). The issue I was running into was that there was a lot of data. For example, I had 10 regexes that would search 36 gzipped files averaging 1.2 million lines each. The real issue was that these logs came in hourly, so if it couldn&#8217;t finish searching them all within an hour it was going to get backed up.</p>
<p>Being a good Pythonista, I followed the cardinal rules of:<br />
Get it right.<br />
Test it&#8217;s right.<br />
Profile if slow.<br />
Optimize.<br />
Repeat from 2.<br />
<a href="http://wiki.python.org/moin/PythonSpeed/PerformanceTips">http://wiki.python.org/moin/PythonSpeed/PerformanceTips</a></p>
<p>The problem was, after a while I sort of hit a wall. Nothing I did could make this code appreciably faster (of course this was with my limited knowlegde. I&#8217;m sure that more experienced Python programmers could optimize this code a lot better then I can, rewrite the regex bottleneck in C, etc) but I was at the end of my rope.</p>
<p>On thing led to another and I remembered reading about Pypy. Pypy is implementation of Python using a JIT (Just In Time Compiler) and other things that have lost there meaning for me since I did systems programming in college. &#8220;What the heck&#8221;, I thought, &#8220;I&#8217;ll give it a try&#8221;. Pypy is supposed to be highly compatible with CPython (the regular python implementation) and my code didn&#8217;t use any exotic libraries.</p>
<p>So I dumped the tarball on my linux machine, unzipped and ran my unmodified code against it, and DAMN was it fast.</p>
<p>CPython run:</p>
<pre>sean@linux1:~/code/python/hourly_alerts$ python alerter.py
Loaded regexes
Processing known_bad.log.gz
Searched 817051 lines in 119.398156166 seconds using filter</pre>
<p>Pypy Run</p>
<pre>sean@linux1:~/code/python/hourly_alerts$ /home/sean/bin/pypy-1.7/bin/pypy alerter.py
Loaded regexes
Processing known_bad.log.gz
Searched 817051 lines in 51.1275110245 seconds using filter</pre>
<p>More then twice as fast! Now I know that it was a totally unscientific test and all, but its great to see such an improvement right away.</p>
<p>I my also try Cython, but that looks like it doesn&#8217;t have quite the drop-in functionality of Pypy.</p>
<p>Here are the link to Pypy : <a href="http://pypy.org">http://pypy.org/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/01/06/66/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python : using filter and regular expressions to search a file</title>
		<link>http://www.seanelavelle.com/2012/01/03/python-using-filter-and-regular-expressions-to-search-a-file/</link>
		<comments>http://www.seanelavelle.com/2012/01/03/python-using-filter-and-regular-expressions-to-search-a-file/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 13:57:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.seanelavelle.com/?p=60</guid>
		<description><![CDATA[I&#8217;ve been working on a script that searches files based on regular expressions and came up with a pretty neat way to do this  &#8211; using filter! (this may be old hat to more experienced Python programers, but I am &#8230; <a href="http://www.seanelavelle.com/2012/01/03/python-using-filter-and-regular-expressions-to-search-a-file/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a script that searches files based on regular expressions and came up with a pretty neat way to do this  &#8211; using filter! (this may be old hat to more experienced Python programers, but I am just starting to get comfortable with map/filter/etc&#8230;). In this example we compile our regex and filter the gzip file that we have read into memory and display the lines that match and the type of regex that they match on.</p>
<pre class="prettyprint">import re
import gzip

searchName = "Example Regex"
compiledRegex = re.compile(r'example' re.I)

dataFile = gzip.open(fileName, "rb")
#read in the file to memory
fullData = dataFile.readlines()
regexName = searchName
regexSearch = compiledRegex .search
#Filter for just the lines matching the regex
hitList = filter(regexSearch, fullData)
#hitList now is just a list of our matches
for item in hitList:
    print "MATCH:%s" % regexName
    print "ENTRY:%s" % item</pre>
<p>Obviously this code needs more to be at all usable, but this is the general idea and a pretty cool use of the filter function.</p>
<p>If you know of a better way, let me know in the comments.</p>
<p>References:<br />
Python Regular Expressions (re) : <a href="http://docs.python.org/library/re.html">http://docs.python.org/library/re.html</a><br />
Filter Function : <a href="http://docs.python.org/library/functions.html#filter">http://docs.python.org/library/functions.html#filter</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.seanelavelle.com/2012/01/03/python-using-filter-and-regular-expressions-to-search-a-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

