<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4783128742254268789</id><updated>2011-04-21T18:35:35.501-07:00</updated><category term='exceptions'/><category term='KeyError'/><category term='sql beautifier sqlinform'/><category term='case-insensitive-madness'/><category term='what-the-deuce'/><category term='git'/><category term='python'/><category term='mac'/><title type='text'>Four Thirds Pi</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://fourthirdspi.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://fourthirdspi.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>shmeedogg</name><uri>http://www.blogger.com/profile/04210190944024639511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4783128742254268789.post-3759521295126909139</id><published>2008-08-27T16:49:00.000-07:00</published><updated>2008-08-27T17:17:43.073-07:00</updated><title type='text'>Wakeup, Facepalm, Yield, repeat...</title><content type='html'>Today I dove into threading in Python for the first time. Usually the best way to learn is by experimenting, so, I decided to write a small test program. To begin, I started with what I thought would be the absolute minimal "threading" program, a single threaded program that just imports the "threading" module. Not useful, but, lets just do a sanity check.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import threading&lt;br /&gt;print "hi"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To my utter amazement, I get back&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;hi&lt;br /&gt;hi&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Good thing I did this sanity check? Something is going on here I don't understand.&lt;br /&gt;&lt;br /&gt;So, I start probing. I flush stdout before importing threading-- it still prints twice. &lt;br /&gt;&lt;br /&gt;I import time and sleep for a second before importing threading-- it still prints twice.&lt;br /&gt;&lt;br /&gt;It gets worse. I fire up an interpreter and try to import threading and... wait for it... I still get 'hi'. WTF?!?&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Python 2.5.1 (r251:54863, Feb  4 2008, 21:48:13) &lt;br /&gt;[GCC 4.0.1 (Apple Inc. build 5465)] on darwin&lt;br /&gt;Type "help", "copyright", "credits" or "license" for more information.&lt;br /&gt;&gt;&gt;&gt; import threading&lt;br /&gt;hi&lt;br /&gt;&gt;&gt;&gt; &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;It takes me a few minutes after seeing this, but I finally get it.&lt;br /&gt;&lt;br /&gt;My test file was named "threading.py". D'oh!. I was importing myself, hence, the duplicate "hi"s. A simple but painful lesson in avoiding module name collisions.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4783128742254268789-3759521295126909139?l=fourthirdspi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fourthirdspi.blogspot.com/feeds/3759521295126909139/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4783128742254268789&amp;postID=3759521295126909139' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/3759521295126909139'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/3759521295126909139'/><link rel='alternate' type='text/html' href='http://fourthirdspi.blogspot.com/2008/08/wakeup-facepalm-yield-repeat.html' title='Wakeup, Facepalm, Yield, repeat...'/><author><name>shmeedogg</name><uri>http://www.blogger.com/profile/04210190944024639511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4783128742254268789.post-1421941859357027908</id><published>2008-04-16T13:12:00.000-07:00</published><updated>2008-04-23T09:51:01.262-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='what-the-deuce'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='KeyError'/><category scheme='http://www.blogger.com/atom/ns#' term='exceptions'/><title type='text'>KeyError breaks the unwritten rules</title><content type='html'>I ran across an interesting little "feature" in Python today; it cropped up as a bug in an old version of DocumentTemplate that I am dependent upon. It turns out that, unlike other &lt;a href="http://docs.python.org/api/standardExceptions.html"&gt;Exceptions in Python&lt;/a&gt;, strings within KeyErrors don't round-trip. In other words,&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;"hello" != str(KeyError("hello"))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;This is certainly unintuitive; I would have made the same mistake as the Zope guys. In fact, I have tried it for other Exception classes and most do round-trip.  To quoteth the &lt;a href="http://www.python.org/dev/peps/pep-0020/"&gt;Zen&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;Special cases aren't special enough to break the rules.&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;What is so special about KeyError that it gets to break the rules?&lt;br /&gt;&lt;br /&gt;Here is a little test I whipped up to explore this:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import unittest&lt;br /&gt;&lt;br /&gt;class TestExceptionStringRoundTrip(unittest.TestCase):&lt;br /&gt;    def assertStrCast(self, ExceptionClass):&lt;br /&gt;        s = "hello"&lt;br /&gt;        self.assertEqual(s, str(ExceptionClass(s)))&lt;br /&gt;&lt;br /&gt;for builtin in dir(__builtins__):&lt;br /&gt;    if builtin.endswith('Error') or builtin.endswith('Exception'):&lt;br /&gt;        def _test(self, ExceptionClass=builtin):&lt;br /&gt;            self.assertStrCast(getattr(__builtins__, ExceptionClass))&lt;br /&gt;        setattr(TestExceptionStringRoundTrip, "test%s" % builtin,&lt;br /&gt;                _test)&lt;br /&gt;&lt;br /&gt;if __name__ == "__main__":&lt;br /&gt;    unittest.main()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is only one failure: KeyError. There are three errors from UnicodeDecodeError, UnicodeEncodeError and UnicodeTranslateError because they don't accept just a string as an argument (they probably should!)&lt;br /&gt;&lt;br /&gt;Anyway, a fun little aside.&lt;br /&gt;&lt;br /&gt;UPDATE:&lt;br /&gt;Amaury Forgeot d'Arc has beaten me to the punch with a patch to fix this. I was kind of hoping I would get a crack at this, but this guy is just a coding machine :). Looks like he has a lot more experience with Python's source, so, I trust his patch is probably a little nicer than mine would have been. Here's the &lt;a href="http://bugs.python.org/issue2651"&gt;bug report&lt;/a&gt; for those who are interested (the patch is linked from the report).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4783128742254268789-1421941859357027908?l=fourthirdspi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fourthirdspi.blogspot.com/feeds/1421941859357027908/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4783128742254268789&amp;postID=1421941859357027908' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/1421941859357027908'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/1421941859357027908'/><link rel='alternate' type='text/html' href='http://fourthirdspi.blogspot.com/2008/04/keyerror-breaks-unwritten-rules.html' title='KeyError breaks the unwritten rules'/><author><name>shmeedogg</name><uri>http://www.blogger.com/profile/04210190944024639511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4783128742254268789.post-5762585426750909102</id><published>2008-04-16T12:03:00.000-07:00</published><updated>2008-04-16T12:59:24.039-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='git'/><category scheme='http://www.blogger.com/atom/ns#' term='case-insensitive-madness'/><category scheme='http://www.blogger.com/atom/ns#' term='mac'/><title type='text'>Mac OS X quirks</title><content type='html'>Today,  I roll into work around 10:30, free &lt;a href="http://www.starbucks.com/flash/pikeplaceroast/index.html"&gt;Starbucks Pike Place Blend&lt;/a&gt; in hand (yummy!), login to my workstation, and see the strangest thing. I do my customary git-status and see that I have made huge changes to one of our unit-tests, TestAccountant, last night. My checkout resides on my personal workstation, not a shared server, so how could one of my co-workers modified my local tree? I don't even have an ssh server enabled! The only logical conclusion is that I must have sleep-drove over, sleep-coded huge chunks of fairly logical looking tests, and sleep-drove back, all before I woke up this morning. And, I don't even feel tired!&lt;br /&gt;&lt;br /&gt;Well, that probably didn't happen. So, what-the-dilly-yo?&lt;br /&gt;&lt;br /&gt;Well, my first guess was that someone had accidentally cloned my repository and was now pushing their changes back to me.&lt;br /&gt;&lt;br /&gt;Background: We use &lt;a href="http://git.or.cz/"&gt;git&lt;/a&gt;, which is a distributed source control system like Bazaar or BitKeeper. This means that, if setup to do so, anybody can clone anybody else's repository, make changes and push those changes back-to 'origin'. This is very different from svn or cvs which are client-server based and require a single centralized master repository. Distributed source control is very powerful, but since I don't understand it all that well, I must admit, it makes me a little queasy to think of how many things could go wrong.&lt;br /&gt;&lt;br /&gt;This theory, as it turned out, was wrong. What happened, you say? Case-insensitve filenames, that's what happened.&lt;br /&gt;&lt;br /&gt;I've been using a Mac at work for the past couple of years now. I generally like it except that Macs tend to be so darn different:&lt;br /&gt;&lt;br /&gt;/Users instead of /home&lt;br /&gt;/Library instead of /usr/lib or /usr/local/lib.&lt;br /&gt;&lt;br /&gt;And now, I've discovered, case-insensitive file names.&lt;br /&gt;&lt;br /&gt;It turns out that we have two unit-tests, one named testAccountant and one named TestAccountant. You see where this is going. Only lower-case testAccountant is appearing in my local tree; my Mac has kindly decided to randomly pick which file I may work with :). However, this copy is being diffed against TestAccountant in my local repository. Two completely different files = massive differences.&lt;br /&gt;&lt;br /&gt;Not a huge deal, but, let this be a reminder: Never rely on case alone to distinguish files. It ain't guaranteed to be respected across all systems and the bugs that do crop up can manifest themselves in some pretty strange ways.&lt;br /&gt;&lt;br /&gt;Now, back to sleep-coding...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4783128742254268789-5762585426750909102?l=fourthirdspi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fourthirdspi.blogspot.com/feeds/5762585426750909102/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4783128742254268789&amp;postID=5762585426750909102' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/5762585426750909102'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/5762585426750909102'/><link rel='alternate' type='text/html' href='http://fourthirdspi.blogspot.com/2008/04/mac-os-x-quirks.html' title='Mac OS X quirks'/><author><name>shmeedogg</name><uri>http://www.blogger.com/profile/04210190944024639511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4783128742254268789.post-977555015609012950</id><published>2008-04-11T08:08:00.000-07:00</published><updated>2008-04-11T08:11:10.610-07:00</updated><title type='text'>Bash History (meme)</title><content type='html'>$ history|awk '{a[$2]++} END{for(i in a){printf "%5d\t%s\n",a[i],i}}'|sort -rn|head&lt;br /&gt;  177   ls&lt;br /&gt;  122   cd&lt;br /&gt;   91   git-status&lt;br /&gt;   62   vi&lt;br /&gt;   49   git-commit&lt;br /&gt;   44   git-pull&lt;br /&gt;   38   python&lt;br /&gt;   36   git-push&lt;br /&gt;   35   git-diff&lt;br /&gt;   33   matrix&lt;br /&gt;&lt;br /&gt;Wow, I compulsively type 'ls'. I get the feeling I type that in the brief moment when I don't know what I'm going to do next, even if I have no desire to see the contents of the current directory. Habit, really.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4783128742254268789-977555015609012950?l=fourthirdspi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fourthirdspi.blogspot.com/feeds/977555015609012950/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4783128742254268789&amp;postID=977555015609012950' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/977555015609012950'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/977555015609012950'/><link rel='alternate' type='text/html' href='http://fourthirdspi.blogspot.com/2008/04/bash-history-meme.html' title='Bash History (meme)'/><author><name>shmeedogg</name><uri>http://www.blogger.com/profile/04210190944024639511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4783128742254268789.post-621348416364167162</id><published>2008-04-10T20:34:00.000-07:00</published><updated>2008-04-10T20:39:39.171-07:00</updated><title type='text'>ASPN Linear Equation Solver In Three Lines!</title><content type='html'>Just ran across this &lt;a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/365013"&gt;recipe &lt;/a&gt;in the Cookbook. It's a linear equation solver in three lines of Python code. Really clever.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4783128742254268789-621348416364167162?l=fourthirdspi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fourthirdspi.blogspot.com/feeds/621348416364167162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4783128742254268789&amp;postID=621348416364167162' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/621348416364167162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/621348416364167162'/><link rel='alternate' type='text/html' href='http://fourthirdspi.blogspot.com/2008/04/aspn-linear-equation-solver-in-three.html' title='ASPN Linear Equation Solver In Three Lines!'/><author><name>shmeedogg</name><uri>http://www.blogger.com/profile/04210190944024639511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4783128742254268789.post-7951697881491840921</id><published>2008-04-09T21:35:00.000-07:00</published><updated>2008-04-09T21:57:45.700-07:00</updated><title type='text'>pycsvdiff Initial Release</title><content type='html'>I have just finished my first cut at a csv file differ, &lt;a href="http://code.google.com/p/pycsvdiff"&gt;pycsvdiff&lt;/a&gt;,  written in Python. I decided to write my own differ, when, after a cursory glance around, I saw only two promising utilities.  One was a cgi script that required uploading your files (not gonna happen!). The other was written in perl ('nuff said).&lt;br /&gt;&lt;br /&gt;Some notes:&lt;br /&gt;&lt;br /&gt;I have included a --run-tests option to verify things are working&lt;br /&gt;&lt;br /&gt;The csv differ is built on top of more generic Table diffing code which is, in turn, built on top of even more generic sequence diffing code. I looked at SequenceMatcher in difflib, but this wasn't quite what I was looking for.&lt;br /&gt;&lt;br /&gt;This was my first project written from the ground up using &lt;a href="http://en.wikipedia.org/wiki/Test_Driven_Development"&gt;TDD&lt;/a&gt;.  Needless to say, the ability to refactor with confidence was just, well, amazing!  Not to mention the satisfaction that comes from the parade of dots that bolts across the screen. (dots FTW!). Now I wish I got one of those nosetests shirts at PyCon.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4783128742254268789-7951697881491840921?l=fourthirdspi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fourthirdspi.blogspot.com/feeds/7951697881491840921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4783128742254268789&amp;postID=7951697881491840921' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/7951697881491840921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/7951697881491840921'/><link rel='alternate' type='text/html' href='http://fourthirdspi.blogspot.com/2008/04/pycsvdiff-initial-release.html' title='pycsvdiff Initial Release'/><author><name>shmeedogg</name><uri>http://www.blogger.com/profile/04210190944024639511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4783128742254268789.post-7429937411038016905</id><published>2008-02-06T16:27:00.000-08:00</published><updated>2008-02-06T17:11:31.016-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='sql beautifier sqlinform'/><title type='text'>SQLinForm Beautification</title><content type='html'>Thanks to the magic of ORMs, the number of SQL queries I have to write is pleasantly small. For those times when I do have to hack out a beast of a query, it's nice to know &lt;a href="http://www.sqlinform.com/"&gt;SQLInForm&lt;/a&gt; is out there.&lt;br /&gt;&lt;br /&gt;SQLinForm is a SQL beautifier–like &lt;a href="http://tidy.sourceforge.net/"&gt;tidy&lt;/a&gt; is for HTML: you feed it raw SQL, no matter how munged, cobbled or kludged, and it will produce a nicely formatted version of that query that you can then paste back into your code.&lt;br /&gt;&lt;br /&gt;Today, I was playing around with it with queries of varying degrees of complexity and it seemed to handle all of them quite nicely. It supports many common SQL dialects including MS-SQL, Oracle, and MySQL among others. There are quite a few configuration options available, from spacing between tokens, indentation, colorization, etc. It even has presets for "Large SQL", "Small SQL", and "1-Line SQL". Very nifty.&lt;br /&gt;&lt;br /&gt;Unfortunately, this app is currently only available via the Web, so no chance of automated code cleanup right now at least. Apparently there used to be a desktop version which is no longer available, though the developer says there will be new version out in March 2008. I really hope there is a nice CLI front-end for it. Heck, I'd even be willing to plunk down a few bucks for something as time saving as this.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4783128742254268789-7429937411038016905?l=fourthirdspi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fourthirdspi.blogspot.com/feeds/7429937411038016905/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4783128742254268789&amp;postID=7429937411038016905' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/7429937411038016905'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/7429937411038016905'/><link rel='alternate' type='text/html' href='http://fourthirdspi.blogspot.com/2008/02/sqlinform-beautification.html' title='SQLinForm Beautification'/><author><name>shmeedogg</name><uri>http://www.blogger.com/profile/04210190944024639511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4783128742254268789.post-550340700355757302</id><published>2007-01-30T18:44:00.000-08:00</published><updated>2007-01-30T19:00:29.354-08:00</updated><title type='text'>Script to Automatically Restart Wireless</title><content type='html'>First of all, welcome to "Four Thirds Pi", a blog I just created to hold musings on Python, Linux, OSS, etc.&lt;br /&gt;&lt;br /&gt;Well, now for the first post...&lt;br /&gt;&lt;br /&gt;I now have a script working that automatically restarts my network adapter if pings start to time out. I needed this because I have a flaky router and I want my Server (in my closet) to automatically recognize when it can connect again w/o having to hook up a monitor and type ifup -a.&lt;br /&gt;&lt;br /&gt;So the script is as follows:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;blockquote&gt;&lt;span style="font-family:courier new;"&gt;#! /bin/bash&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;# This script will ping a known host and restart the interface&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;# if the ping fails&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;# 9/6/06 RCH&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;# usage: keepwirelessup.sh [sleep_time]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;NAME=keepwirelessup&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;PING_HOST="www.yahoo.com"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if [ -n "$1" ]; then&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    SLEEP_TIME=$1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;else&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    SLEEP_TIME=3&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;fi&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;while true; do&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    ping -c 1 $PING_HOST &amp;&gt; /dev/null&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    if [ $? -ne "0" ]; then&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        ifdown -a&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        ifup -a&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        echo `date` Restarting Wireless... &gt;&gt; /var/log/$NAME.log&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    fi&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    sleep $SLEEP_TIME&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;done&lt;/span&gt;&lt;/blockquote&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;span style="font-family:times new roman;"&gt;I copied this script to /etc/init.d/, changed the permissions and ownership to root and then&lt;br /&gt;added these lines to my /etc/rc.local file:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;blockquote&gt;&lt;span style="font-family:courier new;"&gt;# Run my custom Keep Wireless Script Up&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;KEEP_WIRELESS_UP=/etc/init.d/keepwirelessup.sh&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;test -f $KEEP_WIRELESS_UP &amp;&amp;amp; nice -n -10 $KEEP_WIRELESS_UP &amp;amp;&lt;/span&gt;&lt;/blockquote&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:times new roman;"&gt;Now, theoretically, my server should automatically reconnect when the connection resumes to the router.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4783128742254268789-550340700355757302?l=fourthirdspi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fourthirdspi.blogspot.com/feeds/550340700355757302/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4783128742254268789&amp;postID=550340700355757302' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/550340700355757302'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4783128742254268789/posts/default/550340700355757302'/><link rel='alternate' type='text/html' href='http://fourthirdspi.blogspot.com/2007/01/script-to-automatically-restart.html' title='Script to Automatically Restart Wireless'/><author><name>shmeedogg</name><uri>http://www.blogger.com/profile/04210190944024639511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
