<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>dlamotte</title>
 <link href="http://dlamotte.github.com/atom.xml" rel="self"/>
 <link href="http://dlamotte.github.com"/>
 <updated>2013-05-23T09:47:19-07:00</updated>
 <id>http://dlamotte.github.com</id>
 <author>
   <name>Dan LaMotte</name>
   <email>lamotte85@gmail.com</email>
 </author>

 
 <entry>
   <title>Custom SQL with Django ORM</title>
   <link href="http://dlamotte.github.com/2013/custom-sql-with-django-orm/"/>
   <updated>2013-05-23T00:00:00-07:00</updated>
   <id>http://dlamotte.github.com/2013/custom-sql-with-django-orm</id>
   <content type="html">&lt;p&gt;When faced with the need to do more complex things with the ORM in Django, it can be pretty frustrating. I happened upon a pretty interesting solution I&amp;#8217;d like to share.&lt;/p&gt;

&lt;p&gt;I needed to do some simple &lt;code&gt;GROUP BY&lt;/code&gt;, but this trick can apply to all kinds of complicated SQL. The beauty of the ORM is that you can effectively pass around a SQL query and extend it. It encapsulates building a SQL String and saving associated params with that query. So, now, you want to do something more complicated, you need to use the &amp;#8220;Escape Hatch&amp;#8221; and use custom SQL, but you still need to begin with a &lt;code&gt;QuerySet&lt;/code&gt; as a base. Here&amp;#8217;s how:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;from django.db import connections, router
from myproject.models import MyModel

qs = MyModel.objects.filter(...) # feel free to use the QuerySet API however

def do_custom_sql(qs):
    conn = connections[router.db_for_read(qs.model)]
    cursor = conn.cursor()

    # build the SQL and extract the params
    sql, params = qs.query.get_compiler(router.db_for_read(qs.model)).as_sql()

    # extract the WHERE clause
    where = sql[sql.index(&amp;#39;WHERE&amp;#39;) + 5:]

    cursor.execute(&amp;#39;&amp;#39;&amp;#39;
        SELECT
            column1
            , count(column2) as hits
        FROM
            %(table)s
        WHERE
            %(where)s
        GROUP BY
            column1
        HAVING
            hits &amp;gt; 100
    &amp;#39;&amp;#39;&amp;#39; % {
        &amp;#39;table&amp;#39;: conn.ops.quote_name(qs.model._meta.db_table),
        &amp;#39;where&amp;#39;: where,
    }, params)

    for column1, hits in cursor.fetchall():
        ...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can use the power of the ORM with the power of custom SQL. Of course there are caveats, but at least this is in your toolbox now and you&amp;#8217;ll probably be able to figure it out from there.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>javascript promises (taming async)</title>
   <link href="http://dlamotte.github.com/2012/javascript-promises-taming-async/"/>
   <updated>2012-11-14T00:00:00-08:00</updated>
   <id>http://dlamotte.github.com/2012/javascript-promises-taming-async</id>
   <content type="html">&lt;p&gt;I recently discovered the need for a real promise library (jQuery Deferreds needed a little extra). I found a library called &lt;a href='https://github.com/kriskowal/q'&gt;q.js&lt;/a&gt; which I&amp;#8217;ve spent the last few days attempting to wrap my head around. This all started culminating after reading &lt;a href='https://gist.github.com/3889970'&gt;this excellent post&lt;/a&gt; about &lt;em&gt;Missing the Point of Promises&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Indeed, I was missing the point.&lt;/p&gt;

&lt;p&gt;After dealing with jQuery deferreds inside of more jQuery deferreds, I began looking around for a different way. I thought back to the above post and decided to start looking into using it.&lt;/p&gt;

&lt;p&gt;I started by reading the main tutorial and found myself confused about why all these methods were necessary AND how they all worked together. I happened to stumble upon a file in the repository called &lt;a href='https://github.com/kriskowal/q/blob/master/design/README.js'&gt;README.js&lt;/a&gt;. I suggest going through and reading that for anyone that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;wonders why they would want/need promises&lt;/li&gt;

&lt;li&gt;is confused after reading the main tutorial for the library&lt;/li&gt;

&lt;li&gt;wants a good understanding of the source of the library&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I noticed quite quickly that not all the methods are documented in the documentation and that you will need to peek at the source to find/understand some (which is unfortunate).&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s a quick demo of something you can do that helped me make sense of promises:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Q.when($.get(&amp;#39;/data/&amp;#39;))  // convert jQuery Deferred &amp;quot;thenable&amp;quot; into Q promise
.then(function(data) {
  console.log(&amp;#39;success&amp;#39;, data);
  data.key = &amp;#39;something else&amp;#39;;
  return Q.when($.post(&amp;#39;/data/&amp;#39;, data)); // convert to Q promise
})
.then(function(postdata) {
  // do something with postdata
})
.fail(function() {
  // handles failure of all above jQuery Deferred&amp;#39;s (get/post)
  console.log(&amp;#39;failed&amp;#39;, arguments);
})
.done();&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice how the flow is linear down the page instead of nested inside of lower scopes. jQuery Deferreds get you pretty far, but it doesn&amp;#8217;t chain like Q promises do which make things feel very linear instead of async (which is the point of promises).&lt;/p&gt;

&lt;p&gt;Another piece of magic above is the &lt;code&gt;.done()&lt;/code&gt; method which can be demonstrated by the following.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Q.fcall(function() {
  var obj = {};
  if (obj.attr.oops) {
    // the above blows up because &amp;quot;attr&amp;quot; doesnt exist and &amp;quot;oops&amp;quot;
    // is not a method of &amp;quot;undefined&amp;quot;
  }
})
.done();&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this case, the uncaught Exception is caught inside of the promise and reraised if not handled by fail handler if &lt;code&gt;.done()&lt;/code&gt; is at the end of the chain. It means its very easy to construct something like the following python coders would be familiar with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;try:
    something()
except Exception, e:
    handle_exception(e)
finally:
    cleanup()&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With q.js, it looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Q.fcall(function() {
  throw new Error(&amp;#39;boom&amp;#39;);
})
.fail(function(e) {
  // e is Error(&amp;#39;boom&amp;#39;)
})
.fin(function() {
  // here is your cleanup() method
})
.done();&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Python has it as part of the syntax, but promises make the same function possible but also adding handling of async easily.&lt;/p&gt;

&lt;p&gt;The q.js library also has a deferred similar in function to the jQuery Deferred but with all the guarantees/features of Q.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var defer = Q.defer();
var promise = defer.promise; // principle of least authority

// a promise here is exactly the same as what was accomplished above
promise.then(function(value) {
  console.log(value);
}, function(fail_value) {
  console.log(fail_value);
})
.done();

// in this case, console.log(value) will print 10
setTimeout(function() {
  defer.resolve(10);
}, 5000); // wait 5 seconds, or any async method

// OR

// in this case, console.log(fail_value) will print Error(&amp;#39;boom&amp;#39;)
setTimeout(function() {
  defer.reject(new Error(&amp;#39;boom&amp;#39;));
}, 5000);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Also with deferreds, progress bars become trivial:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var defer = Q.defer();
var promise = defer.promise;

promise.progress(function(mesg) {
  console.log(mesg);
})
.then(function(value) {
  console.log(value);
})
.done();

setTimeout(function() {
  defer.notify(&amp;#39;25% complete&amp;#39;);
}, 1000);

setTimeout(function() {
  defer.notify(&amp;#39;50% complete&amp;#39;);
}, 2000);

setTimeout(function() {
  defer.notify(&amp;#39;75% complete&amp;#39;);
}, 3000);

setTimeout(function() {
  defer.notify(&amp;#39;100% complete&amp;#39;);
  defer.resolve(&amp;#39;done&amp;#39;);
}, 4000);

// the above prints:
// &amp;gt; 25% complete
// &amp;gt; 50% complete
// &amp;gt; 75% complete
// &amp;gt; 100% complete
// &amp;gt; done&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are plenty of other interesting and exciting ways to use the library. The above got me started to the point where I could start exploring other uses and I wouldn&amp;#8217;t get bogged down in details and overly confused.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>introducing statsd-ostools</title>
   <link href="http://dlamotte.github.com/2012/introducing-statsd-ostools/"/>
   <updated>2012-10-05T00:00:00-07:00</updated>
   <id>http://dlamotte.github.com/2012/introducing-statsd-ostools</id>
   <content type="html">&lt;p&gt;I recently deployed &lt;a href='https://github.com/etsy/statsd'&gt;statsd&lt;/a&gt; and &lt;a href='http://graphite.wikidot.com/'&gt;graphite&lt;/a&gt; after reading through etsy&amp;#8217;s blog post &lt;a href='http://codeascraft.etsy.com/2011/02/15/measure-anything-measure-everything/'&gt;measure anything, measure everything&lt;/a&gt;. Finally, I found &lt;a href='https://github.com/andymckay/django-statsd'&gt;django-statsd&lt;/a&gt; (pypi package is actually &lt;em&gt;django-statsd-mozilla&lt;/em&gt;) for some quick and easy integration with Django. However, I was unable to find a way to easily monitor common unix OS information like &lt;code&gt;iostat&lt;/code&gt;, &lt;code&gt;mpstat&lt;/code&gt; and &lt;code&gt;vmstat&lt;/code&gt; for instance.&lt;/p&gt;

&lt;p&gt;So I set out to write something simple to do it and created &lt;strong&gt;statsd-ostools&lt;/strong&gt; as a result. At the moment, it really only supports linux&amp;#8217;s &lt;code&gt;iostat&lt;/code&gt;, &lt;code&gt;mpstat&lt;/code&gt;, and &lt;code&gt;vmstat&lt;/code&gt;, but I plan to add support for other platforms in the future.&lt;/p&gt;

&lt;p&gt;So take a look at &lt;a href='https://github.com/dlamotte/statsd-ostools'&gt;statsd-ostools&lt;/a&gt; and let me know what you think. And/or install it via &lt;code&gt;pip&lt;/code&gt; or &lt;code&gt;easy_install&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;easy_install http://pypi.python.org/pypi/statsd-ostools
pip install http://pypi.python.org/pypi/statsd-ostools&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;statsd-ostools is written in python and only depends on &lt;code&gt;setproctitle&lt;/code&gt; and &lt;code&gt;statsd&lt;/code&gt; so use it on any of your linux servers to monitor them.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>postgres performance</title>
   <link href="http://dlamotte.github.com/2012/postgres-performance/"/>
   <updated>2012-10-02T00:00:00-07:00</updated>
   <id>http://dlamotte.github.com/2012/postgres-performance</id>
   <content type="html">&lt;p&gt;Lately, I&amp;#8217;ve been very interested in tuning and understanding database performance. Specifically, I use postgres and find that it has a number of ways to track how it&amp;#8217;s performing and finding exactly what you&amp;#8217;re looking for is complicated.&lt;/p&gt;

&lt;p&gt;I follow &lt;strong&gt;@craigkerstiens&lt;/strong&gt; on twitter who recently posted this excellent &lt;a href='https://gist.github.com/a0d9038fc5f86312ac9e'&gt;gist on github&lt;/a&gt;. It&amp;#8217;s a few queries which help you better understand how well you&amp;#8217;re using your indexes and caches.&lt;/p&gt;

&lt;p&gt;I also use &lt;a href='http://dalibo.github.com/pgbadger/'&gt;pgbadger&lt;/a&gt; to track many things from the postgres logs. Things like: what queries are the slowest, what queries are taking up the most time, what queries are executed the most, &amp;#8230; the list goes on and pgbadger is a great tool you should definitely check out. I&amp;#8217;d used pgfouine in the past, but pgbadger has won me over now as it&amp;#8217;s csvlog parsing actually works and it has many more reports.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>djtemplate an opinionated django template</title>
   <link href="http://dlamotte.github.com/2012/djtemplate-an-opinionated-django-template/"/>
   <updated>2012-09-22T00:00:00-07:00</updated>
   <id>http://dlamotte.github.com/2012/djtemplate-an-opinionated-django-template</id>
   <content type="html">&lt;p&gt;I&amp;#8217;ve created a number of Django projects now and the most frustrating thing jumping between projects is inconsistencies in the project layout. So, finally I set out to solve this problem.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s called &lt;strong&gt;djtemplate&lt;/strong&gt; and its located here:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;https://github.com/dlamotte/djtemplate&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&amp;#8217;s README contains instructions on how to use it, but basically, you do something like this (make sure you&amp;#8217;re using Django 1.4+):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;django-admin.py startproject --template=https://github.com/dlamotte/djtemplate/tarball/master myprojectname&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I call it a &lt;em&gt;very opinionated template&lt;/em&gt; because it&amp;#8217;s aimed at sites with heavier javascript and chooses Backbone.js with Rivets.js among other small libraries I&amp;#8217;ve found useful. The layout is also derived from David Cramer&amp;#8217;s &lt;a href='http://justcramer.com/2011/01/13/settings-in-django/'&gt;Settings in Django&lt;/a&gt; which I&amp;#8217;ve found very useful. The basic design is a typical &lt;a href='http://twitter.github.com/bootstrap/'&gt;Twitter Bootstrap&lt;/a&gt; site along with &lt;a href='http://html5boilerplate.com/'&gt;HTML5 Boilerplate&lt;/a&gt; baked in.&lt;/p&gt;

&lt;p&gt;I didn&amp;#8217;t suspect it&amp;#8217;d take me very long to setup this template honestly, but after doing it and spending the better part of a day plus a few hours, I noticed that even a basic setup like this actually takes quite a bit of time. I&amp;#8217;m hoping this template makes my projects much more consistent along with saving me such time in setting up new projects. I have to say, I find putting the pieces together for a new project is very tedious. Now, it&amp;#8217;ll be a breeze.&lt;/p&gt;

&lt;p&gt;Maybe others will find it useful (or at least as a base for your own personal template). Let me know if you do!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>uploaded my dotfiles again</title>
   <link href="http://dlamotte.github.com/2012/uploaded-my-dotfiles-again/"/>
   <updated>2012-09-09T00:00:00-07:00</updated>
   <id>http://dlamotte.github.com/2012/uploaded-my-dotfiles-again</id>
   <content type="html">&lt;p&gt;I&amp;#8217;ve played this game before. I uploaded my dotfiles to github and failed.&lt;/p&gt;

&lt;p&gt;This time is different. You can find them &lt;a href='https://github.com/dlamotte/dotfiles'&gt;here&lt;/a&gt;. This time, instead of a horribly hacked Makefile installing the files, I&amp;#8217;ve gone with symlinks which seems that others have been doing successfully.&lt;/p&gt;

&lt;p&gt;But I am not using &lt;code&gt;lndir&lt;/code&gt; because it doesn&amp;#8217;t quite work the way I&amp;#8217;d like. In some cases, I want the whole directory to be symlinked. In others, I want subfiles symlinked and I want fine grained control of that (but I want good defaults so adding new files isn&amp;#8217;t a hassle). On top of this, I hate the idea of symlinking the dotfiles straight across. How ugly. I don&amp;#8217;t want to have to &lt;code&gt;ls -a&lt;/code&gt; in my sandbox of dotfiles all the time. The &amp;#8220;install script&amp;#8221; should be smart enough to prefix symlinked files with a &lt;code&gt;.&lt;/code&gt; so that I can look at the files in the dotfiles dir &amp;#8220;normally&amp;#8221;.&lt;/p&gt;

&lt;p&gt;These two factors made me decide to write the script you&amp;#8217;ll find in there called &lt;code&gt;lndotfiles.py&lt;/code&gt;. This script has some global variables that act like configuration and also does the magic explained above.&lt;/p&gt;

&lt;p&gt;I also have the distinct problem of having separate dotfiles between my work computer and my home computer. I&amp;#8217;ve (hopefully) solved this problem this time around by adding a &lt;code&gt;~/.home/&lt;/code&gt; and &lt;code&gt;~/.work/&lt;/code&gt; directories which will contain location specific config files that will get sourced by the associated config file via &lt;code&gt;~/.current/...&lt;/code&gt; where &lt;code&gt;~/.current&lt;/code&gt; is a symlink to the appropriate directory.&lt;/p&gt;

&lt;p&gt;Another interesting extension is the &lt;code&gt;alias&lt;/code&gt; I&amp;#8217;ve added to my &lt;code&gt;~/.current/zshrc&lt;/code&gt;. If I&amp;#8217;m editting dotfiles in my home directory, I shouldn&amp;#8217;t need to &lt;code&gt;cd&lt;/code&gt; into my dotfiles sandbox. Instead, I should have direct access to the dotfiles sandbox. The &lt;code&gt;alias&lt;/code&gt; looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;alias gitdot=&amp;quot;git --git-dir=$HOME/sandbox/dotfiles/.git --work-tree=$HOME/sandbox/dotfiles/&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now I can just use &lt;code&gt;gitdot&lt;/code&gt; in place of &lt;code&gt;git&lt;/code&gt; and work on my dotfiles from wherever I like.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>jekyll blogging first glance</title>
   <link href="http://dlamotte.github.com/2012/jekyll-blogging-first-glance/"/>
   <updated>2012-09-08T00:00:00-07:00</updated>
   <id>http://dlamotte.github.com/2012/jekyll-blogging-first-glance</id>
   <content type="html">&lt;p&gt;I finally sat down to try out blogging with Github and Jekyll. I have to say, I&amp;#8217;m very happy with it so far. I stripped down the Jekyll-Bootstrap template to my liking and have ended up with a very simple blogging system based on Git and Jekyll (which is pretty convenient for this type of thing).&lt;/p&gt;

&lt;p&gt;I get to use my favorite editor (vim) and don&amp;#8217;t have to deal with a web interface to post.&lt;/p&gt;

&lt;p&gt;Best of all, the hosting is free and relatively still &amp;#8220;branded&amp;#8221; for my username.&lt;/p&gt;

&lt;p&gt;Can&amp;#8217;t believe I didn&amp;#8217;t do this sooner. If you&amp;#8217;re considering blogging and want something lightweight with minimal setup, this is it.&lt;/p&gt;</content>
 </entry>
 

</feed>
