<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Tech Rantings from a Michigan Techie &#187; Tech</title>
	<atom:link href="http://blog.mitechie.com/category/tech/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mitechie.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Wed, 25 Aug 2010 01:31:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.mitechie.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/5b8fff6c0a7af41e1d53211719f0bf89?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Tech Rantings from a Michigan Techie &#187; Tech</title>
		<link>http://blog.mitechie.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.mitechie.com/osd.xml" title="Tech Rantings from a Michigan Techie" />
	<atom:link rel='hub' href='http://blog.mitechie.com/?pushpress=hub'/>
		<item>
		<title>Insert Giant PyOhio 2010 Recap Here</title>
		<link>http://blog.mitechie.com/2010/08/02/insert-giant-pyohio-2010-recap-here/</link>
		<comments>http://blog.mitechie.com/2010/08/02/insert-giant-pyohio-2010-recap-here/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 21:20:21 +0000</pubDate>
		<dc:creator>mitechie</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://blog.mitechie.com/?p=98</guid>
		<description><![CDATA[Ugggg, zombie day today as I attempt to get work done after a crazy PyOhio this year. Just like the last two this was a great regional conference I&#8217;m so glad that people put on. So congrats to the team for a 3rd year and some great stuff. The nice thing this year is that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=98&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ugggg, zombie day today as I attempt to get work done after a crazy <a href="http://pyohio.org" target="_blank">PyOhio</a> this year. Just like the last two this was a great regional conference I&#8217;m so glad that people put on. So congrats to the team for a 3rd year and some great stuff. The nice thing this year is that I managed to finally reach a goal of mine to get a job doing Python. After being a wanna-be the last two years, I got a chance to give back a bit more than I took for the first time.</p>
<p>So below is a brain dump of my view of the conference. I want to thank all the awesome people that came to the open spaces, provided great insight, and to the speakers I attended for showing me cool stuff I&#8217;ve yet to see. If you&#8217;ve got any questions or corrections from my dump please let me know. And if you were the one giving the talk, or want to add let me know and I&#8217;ll toss an link/edit in here going forward.</p>
<p>The full schedule is up at: <a href="http://www.pyohio.org/2010/Schedule" target="_blank">http://www.pyohio.org/2010/Schedule</a></p>
<h2>Day 1</h2>
<h3>Python Koans &#8211; Tutorial</h3>
<p>I started out with the Koans tutorial track. You can get the koans from:<br />
<a href="http://bitbucket.org/mcrute/python_koans/src/tip/python%202_6/koans/" target="_blank"> http://bitbucket.org/mcrute/python_koans/src/tip/python%202_6/</a></p>
<p>The idea is that you get the source code which is setup as a series of unit tests. Each test is broken and you need to fix it in order to understand some aspect of Python. They&#8217;ve got a ton of work in here and it&#8217;s a really fun way to learn about Python.</p>
<p>For instance there&#8217;s a whole test file for tuples. You have to go in and correct the unit tests so they pass and as you correct them you learn how tuples work and some of the ways you can use them.</p>
<p>You can see all the koans in their directory:</p>
<p>http://bitbucket.org/mcrute/python_koans/src/tip/python%202_6/koans/</p>
<p>So one example:</p>
<pre class="brush: python;">
def test_tuples_are_immutable_so_item_assignment_is_not_possible(self):
    count_of_three =  (1, 2, 5)
    try:
        count_of_three[2] = &quot;three&quot;
    except TypeError as ex:
       self.assertMatch(__, ex[0])
</pre>
<p>Here it&#8217;s demonstrating that tuples are immutable. You&#8217;ll get an exception if you try to edit the tuple in place like this. Your job is to replace the <em>&#8216;__&#8217; </em>with the name of the exception you&#8217;ll end up getting.</p>
<p>The koans are small and you can easily test these in a python terminal to help you figure things out. Lots of fun.</p>
<h3>Log Analysis with Python</h3>
<p>This turned out to not be what I was looking for. It seemed like a chance for the writer of this petit tool to show off his tool. I was hoping for more about how to analyze, generate trends, etc from log files. More on the math/methods. His library seems cool and all, but unless a lot of regular time going over log files I&#8217;m not sure how handy it would be.</p>
<p><a href="http://crunchtools.com/software/petit/" target="_blank">http://crunchtools.com/software/petit/</a></p>
<h3>Open Space &#8211; Message Queues</h3>
<div>I attended an open space on Message Queues. There were only a couple of people there and we did some chatting, one guy was a heavy ZeroMQ user, but wasn&#8217;t that happy and was looking at rabbitmq. So not a lot of new notes, but seems the rabbitmq route is the most popular.</div>
<h3>Wrangling the bits, standardizing how apps get built &#8211; My Talk</h3>
<div>I gave my talk at the end of the day. It was about my work&#8217;s venv.py setup we use and how it enables us to easily create new apps/projects in a hurry in a standard way so that we all know what to expect. It seemed to go over really well. I had a number of people comment after the talk how they thought it was really good.  It&#8217;s basically notes on how to customize virtualenv.py with your own actions/features making life a bit easier.</div>
<div>I&#8217;ve put my talk slides up in case you&#8217;re interested, but there&#8217;s not a lot of meat to them unfortunately:</div>
<div id="_mcePaste"><a href="http://uploads.mitechie.com/pyohio2010/pyohio_2010.html" target="_blank">http://uploads.mitechie.com/pyohio2010/pyohio_2010.html</a></div>
<h3>Sprints</h3>
<div id="_mcePaste">That night we met up at Subway to do some sprints on projects. We didn&#8217;t get a whole lot done. I did start to checkout the source code for Pypi, but didn&#8217;t do much with it.</div>
<p>On a side note, all hail the awesome subway lady that got serving us all. I think we might have been some 25 people strong showing up at once and hammered here running the store all on her own. She didn&#8217;t complain once and was just pure awesome.</p>
<h2>Day 2</h2>
<h3>Vim open space</h3>
<p>The talks were so-so and I decided to instead lead an open space on vim and using it for python work.  There were a bunch of people in there and it went over really well. Everyone learned a few new tricks in there. One thing I&#8217;ve updated is to use the plugin pylint.vim so that I can get in-line syntax error checking.</p>
<p>Some of the plugins brought up:</p>
<ul>
<li><a href="http://www.vim.org/scripts/script.php?script_id=1697" target="_blank">http://www.vim.org/scripts/script.php?script_id=1697</a></li>
<li><a href="http://www.vim.org/scripts/script.php?script_id=2050" target="_blank">http://www.vim.org/scripts/script.php?script_id=2050</a></li>
<li><a href="http://christianrobinson.name/vim/BufOnly.vim" target="_blank">http://christianrobinson.name/vim/BufOnly.vim</a></li>
<li><a href="http://www.vim.org/scripts/script.php?script_id=2771" target="_blank">http://www.vim.org/scripts/script.php?script_id=2771</a></li>
<li><a href="http://www.vim.org/scripts/script.php?script_id=2332" target="_blank">http://www.vim.org/scripts/script.php?script_id=2332</a></li>
</ul>
<p>And another link to my vim config: <a href="http://github.com/mitechie/pyvim" target="_blank">http://github.com/mitechie/pyvim</a></p>
<h3>Controlling Unix Processes with Supervisord</h3>
<p>This was a pretty good talk. There were several things I didn&#8217;t realize that supervisor could do such as writing your own custom event handlers. For instance they have a custom thing that checks for some message that causes them to spin up more web servers to handle an increased server load.</p>
<p>Some of the tricks like tying all your services together under supervisor was pretty neat. For instance they tie in their mysql servers, web app servers, solr full text servers into one supervisor control setup. So they all startup/work together.</p>
<p>Some of the things they do are because they can provide a user control interface for clients. They create something like a cPanel-like interface for users on their network which allows them to restart/stop servers at will.</p>
<h3>Code with Style</h3>
<p>This talk basically a walk through Pep8. I try hard to keep to Pep8 and use a vim plugin to check my files as I work on them. The two places I have a hard time with it is the line length when you have long single line messages and with complicated list comprehensions.</p>
<p>He demonstrated a couple of tricks:</p>
<ul>
<li>for list comprehensions that are complicated you can turn it into a function that you then run across your list/etc via the filter() function.</li>
<li>for long strings you can avoid the \ at the end of line by using ()</li>
</ul>
<pre class="brush: python;">
s = (&quot;my one long&quot;
    &quot; line of text&quot;
    &quot; is this thing here&quot;)
</pre>
<p>will come out as: <em>&#8220;my one long line of text is this thing here&#8221;</em></p>
<p>So now I have some work to do to clean up some things I&#8217;ve left behind.</p>
<h3>Fabric &#8211; Open Space</h3>
<p>I wasn&#8217;t into the next set of talks so I again hosted an open space on fabric.  This one got interesting as many people in the room handle large server farms of hundreds of machines and have very separate ops/dev people. So a developer is separated from some of the things I was showing in fabric like stopping/restarting celery, resetting the web app, etc. There was also some question as to the advantage of using something like Fabric over just bash/shell scripts.</p>
<p>So it was interesting to see where fabric fits in, where it falls short, and while it&#8217;s cool in some aspects, there are a number of limitations and concerns in several use cases.</p>
<p>The one regret was that with the discussion going on we never got other people up front showing off their fabric setups which I would have liked to see. I guess we had a hard time keeping the discussion going in line and I&#8217;d have liked to see more stuff from other fabric fans.</p>
<h3>Making it go faster</h3>
<p>This was the last talk I went to. It started out looking at how to profile your code using cProfile and pstats. I had messed with this, but then he covered using runsnakerun in order to do the profiling which gives you pretty graphs that represent the time spend in functions as a series of shells like this:</p>
<p><a href="http://www.vrplumber.com/programming/runsnakerun/screenshot-2.0.png" target="_blank">http://www.vrplumber.com/programming/runsnakerun/screenshot-2.0.png</a></p>
<p>You can see the site there:</p>
<p><a href="http://www.vrplumber.com/programming/runsnakerun/" target="_blank">http://www.vrplumber.com/programming/runsnakerun/</a></p>
<p>So it&#8217;s a decent wrapper around pstats for some more visual/easy to view performance stats. I&#8217;ll have to try that out in the future.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mitechie.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mitechie.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mitechie.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mitechie.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mitechie.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mitechie.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mitechie.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mitechie.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mitechie.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mitechie.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mitechie.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mitechie.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mitechie.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mitechie.wordpress.com/98/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=98&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mitechie.com/2010/08/02/insert-giant-pyohio-2010-recap-here/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1641c0f988b844f44de596fcef3adc62?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mitechie</media:title>
		</media:content>
	</item>
		<item>
		<title>From Software Center to App Store</title>
		<link>http://blog.mitechie.com/2010/07/07/from-software-center-to-app-store/</link>
		<comments>http://blog.mitechie.com/2010/07/07/from-software-center-to-app-store/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 12:33:55 +0000</pubDate>
		<dc:creator>mitechie</dc:creator>
				<category><![CDATA[Lococast]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://blog.mitechie.com/?p=91</guid>
		<description><![CDATA[Yay, Craig and I managed to get back on the lococast.net podcast train once I got back from my Vacation. It&#8217;s still a work in progress as we fight to try to keep our normal extended discussions down to a 30min podcast. This time I wanted to discuss the idea of the desktop os app [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=91&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Yay, <a href="http://decafbad.net/">Craig</a> and I managed to get back on the <a href="http://lococast.net">lococast.net</a> podcast train once I got back from my Vacation. It&#8217;s still a work in progress as we fight to try to keep our normal extended discussions down to a 30min podcast. This time I wanted to discuss the idea of the desktop os app store and there were a few things we didn&#8217;t get to <a href="http://lococast.net/archives/52">in the podcast</a>. I had originally wanted to try to go through the things I felt were missing from the Software Center in Ubuntu that prevented me from counting it as an &#8220;App Store&#8221;. </p>
<p>The Software Center is coming along since I checked it out during the original blueprint phase. I think it could definitely get to an app store look/feel. So first my rants on things I&#8217;m not a fan of. </p>
<ul>
<li>The universal access icon is white which set upon a light background makes it nearly impossible to read/see. </li>
<li>In the &#8220;Get Software&#8221; section, is that really the new Canonical logo? A purple dot? Sorry, but it looks horrible. </li>
<li>In the &#8220;Installed Software&#8221; link on the left side I completely missed it had any logo since it&#8217;s again, white logo on white background. </li>
<li>When viewing the details on a software package I love the link for the Website, but the fact that there&#8217;s no hover/other indication this is clickable it looks more like a heading vs a link. </li>
</ul>
<p>Anyway, that&#8217;s just my off the cuff nit picking. What I really wanted to go through is the list of features I&#8217;d love to see to have the app store concept take off in Ubuntu. </p>
<p>First up, only show the software. Forget the libraries. Anything that starts with lib should be hidden by default. I also don&#8217;t think there should be any view of all packages. It&#8217;s just scary. I think search and going through categories/simplified interfaces are the only way to go. Does anyone honestly think users are going to go through the entire list? I think there&#8217;s a bunch of things that can be done to clean up the lists of package in order to make things approachable to users. </p>
<p>Next up, when I think of app stores I think of paid apps. Now I know Canonical has some software in their online canonical store, but that&#8217;s not where I go to install software. I should be able to purchase software right in the Software Center. Along these lines, the Canonical partners repo is the place to put this stuff. Beyond these few things from the Canonical store, I&#8217;d love to see this opened up for other software to be submitted for purchase or maybe donation. How cool would it be to be able to support your favorite apps right through the software center?</p>
<p>While we&#8217;re talking about the parters repo, how is it that just about none of those packages have icons? Not only that, a quick test of a few shows no info in the &#8220;More Info&#8221; section. You&#8217;d think these packages would have help from Canonical getting into place and these should be the gold standards of user experience for packages in the Software Center. </p>
<p>Finally, and according to a recent <a href="http://shotofjaq.org/2010/07/ubuntu-software-center-review/">shotofjaq.org podcast episode</a> there&#8217;s already work going on to allow users to write reviews and ratings of software in there.  This is great news and will open up a bunch of user interface enhancements for users looking for software. </p>
<p>So discovery, purchase, and reputation. These are the big things I think need help in the Ubuntu Software Center. What things do you think are missing?</p>
<p>Edit:<br />
And I should have started here, but definitely looks like much of the paid/donations stuff is something they&#8217;re looking to do currently. Check out <a href="https://blueprints.launchpad.net/ubuntu/+spec/foundations-m-software-center-roadmap">their roadmap</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mitechie.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mitechie.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mitechie.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mitechie.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mitechie.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mitechie.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mitechie.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mitechie.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mitechie.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mitechie.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mitechie.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mitechie.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mitechie.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mitechie.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=91&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mitechie.com/2010/07/07/from-software-center-to-app-store/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1641c0f988b844f44de596fcef3adc62?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mitechie</media:title>
		</media:content>
	</item>
		<item>
		<title>My shot at radio, lococast.net</title>
		<link>http://blog.mitechie.com/2010/06/22/my-shot-at-radio-lococast-net/</link>
		<comments>http://blog.mitechie.com/2010/06/22/my-shot-at-radio-lococast-net/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 01:36:38 +0000</pubDate>
		<dc:creator>mitechie</dc:creator>
				<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://blog.mitechie.com/?p=84</guid>
		<description><![CDATA[I&#8217;ve always had a secret love affair with DJs on the radio. It seems like a pretty cool job until you realize that awful hours, pay, and crap you have to do to start out. In an effort to try to have some of that fun without changing careers I&#8217;ve joined forces with Craig Maloney [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=84&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always had a secret love affair with DJs on the radio. It seems like a pretty cool job until you realize that awful hours, pay, and crap you have to do to start out.</p>
<p>In an effort to try to have some of that fun without changing careers I&#8217;ve joined forces with <a href="http://decafbad.net">Craig Maloney</a> and we&#8217;ve started a podcast of our own with the idea of putting out extremely techie point of view onto things we have interests in. So we&#8217;ll talk about things that come up in the Ubuntu community, represent the Michigan Loco, and just talk tech. If you get a second, check out our first episode over at<br />
<a title="Lococast.net" href="http://lococast.net" target="_blank"> http://lococast.net</a>.</p>
<p>It&#8217;s definitely a work in progress, but check it out and let us know what you think in the comments at <a title="Lococast.net" href="http://lococast.net" target="_blank">lococast.net</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mitechie.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mitechie.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mitechie.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mitechie.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mitechie.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mitechie.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mitechie.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mitechie.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mitechie.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mitechie.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mitechie.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mitechie.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mitechie.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mitechie.wordpress.com/84/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=84&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mitechie.com/2010/06/22/my-shot-at-radio-lococast-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1641c0f988b844f44de596fcef3adc62?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mitechie</media:title>
		</media:content>
	</item>
		<item>
		<title>Pylons controller with both html and json output</title>
		<link>http://blog.mitechie.com/2010/04/06/pylons-controller-with-both-html-and-json-output/</link>
		<comments>http://blog.mitechie.com/2010/04/06/pylons-controller-with-both-html-and-json-output/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 00:48:49 +0000</pubDate>
		<dc:creator>mitechie</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pylons]]></category>

		<guid isPermaLink="false">http://blog.mitechie.com/?p=73</guid>
		<description><![CDATA[We&#8217;re all using ajax these days and one of the simple uses of ajax is to update or add some HTML content to a page. What often happens is that the same data is often display in a url/page of its own. So you might have a url /job/list and then you might want to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=73&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re all using ajax these days and one of the simple uses of ajax is to update or add some HTML content to a page. What often happens is that the same data is often display in a url/page of its own. So you might have a url <strong>/job/list</strong> and then you might want to pull a list of jobs onto a page via ajax. </p>
<p>So my goal is to be able to  reuse controllers to provide details for ajax calls, calls from automated scripts, and whole pages. The trouble with this is that the <strong>@jsonify</strong> decorator in Pylons is pretty basic. It just sets the content type header to application/json and takes whatever you return and tries to jsonify it for you. </p>
<p>That&#8217;s great, but I can&#8217;t reuse that controller to send HTML output any more. So I set out to figure out how the decorator works and create one that works more like I wish. </p>
<p>The first thing in setting this up was to look at how to structure any ajax output. I can&#8217;t stand the urls you hit via ajax that just dump out some content, outputs some string, and makes you have to look up every controller in order to figure out just what you&#8217;re getting back. </p>
<p>I prefer to use a structured format back. So what parts do we need. Really, just a few things. Your ajax library will tell you if there&#8217;s an error such as a timeout, 404, etc. It won&#8217;t tell you if you make a call to a controller and don&#8217;t have perimission, or maybe the controller couldn&#8217;t complete the requested action. So the first thing we need is some value of success in our request. </p>
<p>The second component is feedback as to why the success came back. If the controller returns a lack of success we&#8217;ll want to know why. Maybe it is successful, but we need some note about the process along the way. We need a standard message we can send back. </p>
<p>Finally, we might want to return some sort of data back. This could be anything from a json dump of the object requested to actual html output we want to use.</p>
<p>So that leaves us with a definition </p>
<pre class="brush: jscript;">
{'success': true, 'message': 'Yay, we did it', 'payload': {'id': 10, 'name': 'Bob'}}
</pre>
<p>I want to enforace that any ajax controller will output something in format. It makes it much easier to write effective DRY javascript that can handle this and really leaves us open to handle about anything we need. </p>
<p>So my json decorator is going to have to make sure that if the user requests a json response, that it gets all this info. If the user requests an html response, it&#8217;ll just return the generated template html.</p>
<p>By copying parts of the <strong>@jsonify</strong> and the <strong>@validate</strong> decorators I came up with something that adds a <strong>self.json</strong> to the controller method. In here we setup our json response parts. </p>
<p>Finally, we catch if this is a json request. If so, return our dumped self.json instance. Otherwise, return the html the controller sends back. If the controller is returning rendered html and is a json request, then we stick that into the payload as <strong>payload.html</strong></p>
<p>So take a peek at my decorator code and the JSONResponse object that it uses.  Let me know what you think and any suggestions. It&#8217;s my first venture into the world of Python decorators. </p>
<p><a href="http://gist.github.com/357062">@mijson decorator Gist</a></p>
<p>Sample Controller</p>
<pre class="brush: python;">

@myjson()
def pause(self, id):
    result = SomeObj.pause()

    if self.accepts_json():
        if result:
            self.json.success = True
            self.json.message = 'Paused'
        else:
            self.json.success = False
            self.json.message = 'Failed'

        self.json.payload['job_id'] = id

    return '&lt;h1&gt;Result was: %s&lt;/h1&gt;' % message

#Response:
#    {'success': true,
#        'message': 'Paused',
#        'payload': {'html': '&lt;h1&gt;Result was: Paused&lt;/h1&gt;'}}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mitechie.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mitechie.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mitechie.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mitechie.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mitechie.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mitechie.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mitechie.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mitechie.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mitechie.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mitechie.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mitechie.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mitechie.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mitechie.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mitechie.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=73&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mitechie.com/2010/04/06/pylons-controller-with-both-html-and-json-output/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1641c0f988b844f44de596fcef3adc62?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mitechie</media:title>
		</media:content>
	</item>
		<item>
		<title>SqlAlchemy Migrations for mysql and sqlite and testing</title>
		<link>http://blog.mitechie.com/2010/04/05/sqlalchemy-migrations-for-mysql-and-sqlite-and-testing/</link>
		<comments>http://blog.mitechie.com/2010/04/05/sqlalchemy-migrations-for-mysql-and-sqlite-and-testing/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 13:00:10 +0000</pubDate>
		<dc:creator>mitechie</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sqlalchemy]]></category>
		<category><![CDATA[sqlalchemy-migrate]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.mitechie.com/?p=62</guid>
		<description><![CDATA[I really want to be a unit tester, I really do. Unfortunately it&#8217;s one of those things I can&#8217;t seem to get going. I start and end up falling short before I get over the initial setup hurdle. Or I get a couple of tests working, but then as I have a hard time trying [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=62&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I really want to be a unit tester, I really do. Unfortunately it&#8217;s one of those things I can&#8217;t seem to get going. I start and end up falling short before I get over the initial setup hurdle. Or I get a couple of tests working, but then as I have a hard time trying to test parts of things I fade. </p>
<p>So here goes my latest attempt. It&#8217;s for a web app I&#8217;m working on at work and I REALLY want to have this under at least basic unit tests. Since it&#8217;s a database talking web application my first step is to get a test db up and runnging to run my tests against. At least with that up I can start some actual web tests that add and alter objects via some ajax api calls. </p>
<p>In order to get a test db I first had to figure out how to setup a database for the tests. For speed and ease purposes I&#8217;d rather be able to use sqlite. This way I don&#8217;t need to setup/reset a mysql db on each host I end up trying to run tests on. </p>
<p>Of course this is complicated because I&#8217;m using <a href="http://code.google.com/p/sqlalchemy-migrate/">sqllchemy-migrate</a> for my application. This means part of the testing should be to init a new sqlite db and then bring it up to the latest version. In order to do this I had to convert my existing migrations to work in both MySQL and Sqlite. </p>
<p>Step 1: I need a way to tell the migrations code to use the sqlite db vs the live mysql db. I&#8217;ve setup a <strong><a href="http://packages.python.org/sqlalchemy-migrate/versioning.html#project-management-script">manage.py</a></strong> script in my project root so I hacked it up to check for a &#8211;sqlite flag. Not that great, but it works. </p>
<pre class="brush: python;">
&quot;&quot;&quot;
    In order to support unit testing with sqlite we need to add a flag for
    specifying that db

    python manage.py version_control --sqlite
    python manage.py upgrade --sqlite

    Otherwise it will default to using the mysql connection string

&quot;&quot;&quot;
from migrate.versioning.shell import main

import sys
if '--sqlite' in sys.argv:
    main(url='sqlite:///apptesting.db',repository='app/migrations')
else:
    main(url='mysql://connection_stringl',repository='app/migrations')
</pre>
<p>Step 2: Not all of my existing migrations were sqlite friendly. I had cheated and added some columns by straight sql like</p>
<pre class="brush: python;">
from sqlalchemy import *
from migrate import *

def upgrade():
    sql = &quot;ALTER TABLE jobs ADD COLUMN created TIMESTAMP DEFAULT CURRENT_TIMESTAMP;&quot;
    migrate_engine.execute(sql);

def downgrade():
    sql = = &quot;ALTER TABLE jobs DROP created;&quot;
    migrate_engine.execute(sql);
</pre>
<p>This worked great with MySQL, but sqlite didn&#8217;t like it. In order to get things to work both ways I moved to using the changeset tools to make these more SA happy. </p>
<pre class="brush: python;">
from sqlalchemy import *
from migrate import *
from migrate.changeset import *
from datetime import datetime

meta = MetaData(migrate_engine)
jobs_table = Table('jobs', meta)

def upgrade():
    col = Column('created', DateTime, default=datetime.now)
    col.create(jobs_table)

def downgrade():
    sql = &quot;ALTER TABLE jobs DROP created;&quot;
    migrate_engine.execute(sql);
</pre>
<p>A couple of notes. This abstracted the column creation so that sqlite and mysql would take it. Notice I did <strong>NOT</strong> update the drop command. Sqlite won&#8217;t drop columns, and I honestly didn&#8217;t care because the goal is for my unit tests to be able to bring up a database instance for testing, I&#8217;m not going to run through the downgrade commands in the sqlite database. </p>
<p>Step 3. So with all of the migrations moved to do everything via SA abstracted code vs SQL strings, I was in business. My final problem was one migration in particular. I had changed a field from a varchar to a int field. Sqlite won&#8217;t let you do simple &#8216;ALTER TABLE&#8230;&#8217; and even when I had the command turned into SA based changeset code my db upgrade failed due to sqlite tossing an exception. </p>
<p>What did I do? I cheated. First, I updated the migration with the original column definition to an Integer field. I mean, any new installs could walk that migration up just fine. I happen to know that the two deployments right now have already made the change from varchar to int. So for them, the change won&#8217;t break anything for upgrade/downgrade. </p>
<p>I then kept the migration with the change, but tossed it in a try/except block so that I could trap it nice and just output a message &#8220;If this fails, it&#8217;s probably sqlite choking.&#8221;. It&#8217;s hackish, but works for all my use cases I need. </p>
<p>Now I can create a new test database with the commands</p>
<pre class="brush: plain;">
python manage.py version_control --sqlite
python manage.py upgrade --sqlite
</pre>
<p>Now I can start building my test suite to use this as the bootstrap to create a test db. I&#8217;ll have to them remove the file on teardown so that I don&#8217;t get any errors, but that&#8217;ll be part of the testing setup. Not in memory, but oh well&#8230;it works. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mitechie.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mitechie.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mitechie.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mitechie.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mitechie.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mitechie.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mitechie.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mitechie.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mitechie.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mitechie.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mitechie.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mitechie.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mitechie.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mitechie.wordpress.com/62/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=62&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mitechie.com/2010/04/05/sqlalchemy-migrations-for-mysql-and-sqlite-and-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1641c0f988b844f44de596fcef3adc62?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mitechie</media:title>
		</media:content>
	</item>
		<item>
		<title>A follow up, more dict to SqlAlchemy fun</title>
		<link>http://blog.mitechie.com/2010/04/03/a-follow-up-more-dict-to-sqlalchemy-fun/</link>
		<comments>http://blog.mitechie.com/2010/04/03/a-follow-up-more-dict-to-sqlalchemy-fun/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 14:30:46 +0000</pubDate>
		<dc:creator>mitechie</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sqlalchemy]]></category>

		<guid isPermaLink="false">http://blog.mitechie.com/?p=52</guid>
		<description><![CDATA[Just a quick follow up to my last post on adding the ability to add some tools to help serialize SqlAlchemy instances. I needed to do the reverse. I want to take the POST&#8217;d values from a form submission and tack them onto one of my models. So I now also add a fromdict() method [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=52&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just a quick follow up to my last post on adding the ability to add some tools to help serialize SqlAlchemy instances. I needed to do the reverse. I want to take the POST&#8217;d values from a form submission and tack them onto one of my models. So I now also add a <strong>fromdict()</strong> method onto base that looks like.</p>
<pre class="brush: python;">
def fromdict(self, values):
    &quot;&quot;&quot;Merge in items in the values dict into our object if it's one of our columns

    &quot;&quot;&quot;
    for c in self.__table__.columns:
        if c.name in values:
            setattr(self, c.name, values[c.name])

Base.fromdict = fromdict
</pre>
<p>So in my controllers I can start doing</p>
<pre class="brush: python;">
def controller(self):
    obj = SomeObj()
    obj.fromdict(request.POST)
    Session.add(obj)
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mitechie.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mitechie.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mitechie.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mitechie.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mitechie.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mitechie.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mitechie.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mitechie.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mitechie.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mitechie.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mitechie.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mitechie.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mitechie.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mitechie.wordpress.com/52/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=52&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mitechie.com/2010/04/03/a-follow-up-more-dict-to-sqlalchemy-fun/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1641c0f988b844f44de596fcef3adc62?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mitechie</media:title>
		</media:content>
	</item>
		<item>
		<title>Hacking the SqlAlchemy Base class</title>
		<link>http://blog.mitechie.com/2010/04/01/hacking-the-sqlalchemy-base-class/</link>
		<comments>http://blog.mitechie.com/2010/04/01/hacking-the-sqlalchemy-base-class/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 00:19:58 +0000</pubDate>
		<dc:creator>mitechie</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sqlalchemy]]></category>

		<guid isPermaLink="false">http://blog.mitechie.com/?p=37</guid>
		<description><![CDATA[I&#8217;m not a month into my new job. I&#8217;ve started working for a market research company here locally. Definitely new since I don&#8217;t know I&#8217;ve ever found myself really reading or talking about &#8216;market research&#8217; before. In a way it&#8217;s a lot like my last job in advertising. You get in and there&#8217;s a whole [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=37&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not a month into my new job. I&#8217;ve started working for a market research company here locally. Definitely new since I don&#8217;t know I&#8217;ve ever found myself really reading or talking about &#8216;market research&#8217; before. In a way it&#8217;s a lot like my last job in advertising. You get in and there&#8217;s a whole world of new terms, processes, etc you need to get your head around.</p>
<p>The great thing about my new position is that it&#8217;s a Python web development gig.  I&#8217;m finally able to concentrate on learning the ins and outs of the things I&#8217;ve been trying to learn and gain experience with in my spare time.</p>
<p>So hopefully as I figure things out I&#8217;ll be posting updates to put it down to &#8216;paper&#8217; so I can think it through one last time.</p>
<p>So started with some more SqlAlchemy hacking. At my new place we use Pylons, SqlAlchemy (SA), and Mako as the standard web stack. I&#8217;ve started working on my own first &#8216;ground up&#8217; project and I&#8217;ve been trying to make SqlAlchemy work and get into the way I like doing things.</p>
<p>So first up, I like the instances of my models to be serializable. I like to have json output available for most of my controllers. We all want pretty ajax functionality right? But the json library can&#8217;t serialize and SqlAlchemy model by default. And if you just try to iterate over <strong>sa_obj.__dict__</strong> it won&#8217;t work since you&#8217;ve got all kinds of SA specific fields and related objects in there.</p>
<p>So what&#8217;s a guy to do? Run to the mapper. I&#8217;ve not spent my time I pouring over the details of SA parts and the mapper is something I need to read up on more.</p>
<p>Side Notes: all these examples are from code using declarative base style SA definitions.</p>
<p>The mapper does this great magic of tying a Python object and a SA table definition. So in the end you get a nice combo you do all your work with. In the declarative syntax case you normally have all your models extend the declarative base. So the trick is to add a method of serializing SA objects to the declarative base and boom, magic happens.</p>
<p>The model has a <strong>__table__</strong> instance in it that contains the list of columns.  Those are the hard columns of data in my table. These are the things I want to pull out into a serialized version of my object.</p>
<p>My first try at this looked something like</p>
<pre class="brush: python;">
def todict(self):
    d = {}
    for c in self.__table__.columns:
        value = getattr(self, c.name)
        d[c.name] = value

    return d
</pre>
<p>This is great and all but I ran into a problem. The first object I ran into had a <strong>DateTime</strong> column in it that choked since the json library was trying to serialize a DateTime instance. So a quick hack to check if the column was DateTime and if so put it to string got me up and running again.</p>
<pre class="brush: python;">
if isinstance(c.type, sqlalchemy.DateTime):
    value = getattr(self, c.name).strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)
</pre>
<p>This was great and all. I attached this to the SA Base class and I was in business. Any model now had a <strong>todict()</strong> function I could call.</p>
<pre class="brush: python;">
Base = declarative_base(bind=engine)
metadata = Base.metadata
Base.todict = todict
</pre>
<p>This is great for my needs, but it does miss a few things. This just skips over any relations that are tied to this instance. It&#8217;s pretty basic. I&#8217;ll also run into more fields that need to be converted. I figure that whole part will need a refactor in the future.</p>
<p>Finally I got thinking, &#8220;You know, I can often do a log.debug(dict(some_obj)) and get a nice output of that object and its properties.&#8221; I wanted that as well.  It seems more pythonic to do</p>
<pre class="brush: python;">
dict(sa_instance_obj)
# vs
sa_instance_obj.todict()
</pre>
<p>After hitting up my Python reference book I found that the key to being able to cast something to a dict is to have it implement the iterable protocol. To do this you need to implement a <strong>__iter__</strong> method that returns something that implements a <strong>next()</strong> method.</p>
<p>What does this mean? It means my todict() method needs to return something I can iterate over. Then I can just return it from my object. So I turned <strong>todict</strong> into a generator that returns the list of columns, values needed to iterate through.</p>
<pre class="brush: python;">
def todict(self):
    def convert_datetime(value):
        return value.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)

    d = {}
    for c in self.__table__.columns:
        if isinstance(c.type, sa.DateTime):
            value = convert_datetime(getattr(self, c.name))
        else:
            value = getattr(self, c.name)

        yield(c.name, value)

def iterfunc(self):
    &quot;&quot;&quot;Returns an iterable that supports .next()
        so we can do dict(sa_instance)

    &quot;&quot;&quot;
    return self.todict()

Base = declarative_base(bind=engine)
metadata = Base.metadata
Base.todict = todict
Base.__iter__ = iterfunc
</pre>
<p>Now in my controllers I can do cool stuff like</p>
<pre class="brush: python;">
@jsonify
def controller(self, id):
    obj = Session.query(something).first()

    return dict(obj)
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mitechie.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mitechie.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mitechie.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mitechie.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mitechie.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mitechie.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mitechie.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mitechie.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mitechie.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mitechie.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mitechie.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mitechie.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mitechie.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mitechie.wordpress.com/37/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=37&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mitechie.com/2010/04/01/hacking-the-sqlalchemy-base-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1641c0f988b844f44de596fcef3adc62?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mitechie</media:title>
		</media:content>
	</item>
		<item>
		<title>Auto Logging to SqlAlchemy and Turbogears 2</title>
		<link>http://blog.mitechie.com/2010/01/18/auto-logging-to-sqlalchemy-and-turbogears-2/</link>
		<comments>http://blog.mitechie.com/2010/01/18/auto-logging-to-sqlalchemy-and-turbogears-2/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 01:07:16 +0000</pubDate>
		<dc:creator>mitechie</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[turbogears]]></category>
		<category><![CDATA[sqlalchemy]]></category>

		<guid isPermaLink="false">http://mitechie.wordpress.com/?p=9</guid>
		<description><![CDATA[I&#8217;ve been playing with Turbogear2 (TG2) for some personal tools that help me get work done. One of the things I&#8217;ve run into is an important missing feature that my work code has that isn&#8217;t in my TG2 application yet. In my PHP5 app for work, I use the Doctrine ORM and I have post [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=9&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing with Turbogear2 (TG2) for some personal tools that help me get work done. One of the things I&#8217;ve run into is an important missing feature that my work code has that isn&#8217;t in my TG2 application yet.</p>
<p>In my PHP5 app for work, I use the Doctrine ORM and I have post insert, update, delete hooks that will actually go in and log changes to the system. It works great and I can build up a history of an object over time to see who changes which fields and such.</p>
<p>With my TG2 app doing inserts and updates I initiall just faked the log events by manually saving Log() objects from within my controllers as I do the work that needs to be done.</p>
<p>This sucks though since the point is that I don&#8217;t have to think about things. Anytime code changes something it&#8217;s logged. So I had to start searching the SqlAlchemy (SA) docs to figure out how to duplicate this in TG2. I wanted something that&#8217;s pretty invisible. In my PHP5 code I have a custom method I can put onto my Models in case I want to override the default logged messages and such.</p>
<p>I found part of what I&#8217;m looking for in the SA MapperExtension. This <a href="http://beachcoder.wordpress.com/2007/05/02/adding-event-callbacks-to-sqlalchemyelixir-classes/">blog post</a> got me looking in the right direction. The MapperExtension providers a set of methods to hook a function into. The hooks I&#8217;m interested in are the &#8216;after_delete&#8217;, &#8216;after_insert&#8217;, &#8216;after_update&#8217; method. These are passed in the instance of the object and a connection object so I can generate an SQL query to manually save the log entry for the object.</p>
<p>So I have something that looks a little bit like this:</p>
<pre class="brush: python;">
from sqlalchemy.orm.interfaces import MapperExtension

class LogChanges(MapperExtension):

    def after_insert(self, mapper, connection, instance):
        query = &quot;INSERT INTO log ( username, \
            type, \
            client_ip, \
            application ) VALUES( '%s', %d, '%s', '%s')&quot; % ( u&quot;rick&quot;,
                4,
                u'127.0.0.1',
                u'my_app')

        connection.execute(query)
</pre>
<p>Then I pass that into my declarative model as:</p>
<pre class="brush: python;">
__mapper_args__ = {'extension': LogChanges()}
</pre>
<p>This is very cool and all, but it&#8217;s not all the way where I want to head. First, the manual SQL logging query kind of sucks. I have an AppLog() model that I just want to pass in some values to to create a log entry. I&#8217;m thinking what I really should do is find a different way to do the logging itself. I&#8217;m debating between actually doing a separate logging application that I would call with the objects details.</p>
<p>The problem with this is that one of the things I do in my current app is store the old values of the object. This way I can loop through them and see which values actually changed and generate that in the log message. This is pretty<br />
darn useful.</p>
<p>The other downside is that I don&#8217;t have a good way to have a custom logging message generator is I just call a Logging app API.</p>
<p>So I think I might try out the <a href="http://www.blog.pythonlibrary.org/2009/06/13/using-multiple-databases-in-turbogears-2/">double db connection methods</a> that SA and TG2 support. This way I could actually try to use the second db instance with a Logging() object to write out the changes without messing up the current session/unit of work.</p>
<p>The missing part here is that I&#8217;m still not really sure how to get the &#8216;old&#8217; object values in order to generate a list of fields that have been changed. Guess I have some more hacking to do.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mitechie.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mitechie.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mitechie.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mitechie.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mitechie.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mitechie.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mitechie.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mitechie.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mitechie.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mitechie.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mitechie.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mitechie.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mitechie.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mitechie.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=9&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mitechie.com/2010/01/18/auto-logging-to-sqlalchemy-and-turbogears-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1641c0f988b844f44de596fcef3adc62?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mitechie</media:title>
		</media:content>
	</item>
		<item>
		<title>Moving day, and hopefully a rededication</title>
		<link>http://blog.mitechie.com/2010/01/16/moving-day-and-hopefully-a-rededication/</link>
		<comments>http://blog.mitechie.com/2010/01/16/moving-day-and-hopefully-a-rededication/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 02:29:20 +0000</pubDate>
		<dc:creator>mitechie</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Woodwork]]></category>

		<guid isPermaLink="false">http://mitechie.wordpress.com/?p=3</guid>
		<description><![CDATA[Today is moving day. I&#8217;ve run my own blog for a long while. I had a s9y blog setup and for the last year I&#8217;ve managed something like 2 posts. I want to get back into blogging as I&#8217;ve actually been doing some fun stuff. I also want to bring together my tech posting and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=3&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today is moving day. I&#8217;ve run my own blog for a long while. I had a s9y blog setup and for the last year I&#8217;ve managed something like 2 posts. I want to get back into blogging as I&#8217;ve actually been doing some fun stuff. I also want to bring together my tech posting and my woodwork posting into one place. It&#8217;s just easier to manage and hopefully it&#8217;ll get me posting more often.</p>
<p>It looks like migrating my old posts is going to be a chore. We&#8217;ll see how that goes later on. If you want to follow along realize there will be more non-tech content in the blog so subscribe to the tech category if you don&#8217;t want the misc stuff.</p>
<p>Now to get working on some new posts about the various projects I&#8217;ve been hacking on lately.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mitechie.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mitechie.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mitechie.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mitechie.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mitechie.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mitechie.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mitechie.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mitechie.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mitechie.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mitechie.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mitechie.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mitechie.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mitechie.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mitechie.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mitechie.com&amp;blog=11465279&amp;post=3&amp;subd=mitechie&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mitechie.com/2010/01/16/moving-day-and-hopefully-a-rededication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1641c0f988b844f44de596fcef3adc62?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mitechie</media:title>
		</media:content>
	</item>
	</channel>
</rss>