buntin.org

OS X, Hombrew, MacVim and Python 2.7.1 troubles

Recently I decided to switch over to Vim from Emacs. It was just a test to see what a Python development environment would be like and I must say I am quite pleased. I ran into a couple of problems along the way. This article got me started. The process I am about to detail wasn’t what I really wanted but it accomplishes my end goal which was to have MacVim set up to use my “homebrewed” Python install and work well with the virtualenv settings discussed in the article.

Python needs to be installed with the “framework” option:

$ brew install python --framework

Next you need to update the “Current” OS X symbolic link:

$ cd /System/Library/Frameworks/Python.framework/Versions/
$ sudo rm Current
$ sudo ln -s /usr/local/Cellar/python/2.7.1/Frameworks/Python.framework/Versions/Current

If you have to remove your current python installation (installed with homebrew) you may need to reinstall setuptools, pip, virtualenv, virtuelenvwrapper, etc.

For my virtualenvwrapper to work correctly I needed to take these steps and set up my .bashrc file accordingly:

$ cd /usr/local/share
$ ln -s ../Cellar/python/2.7.1/bin python

Update .bashrc:

$ export PATH=/usr/local/share/python:$PATH

Next you must install MacVim from source. Download the latest package, untar and configure:

$ cd b4winckler-macvim-6e6fac5
$ ./configure --enable-gui=macvim --with-features=huge --enable-rubyinterp --enable-pythoninterp --enable-perlinterp --with-python-config-dir=/usr/local/Cellar/python/2.7.1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config
$ make
$ cp -R src/MacVim/build/Release/MacVim.app /Application

Once this is done everything seemed to work perfect! On my Ubuntu VM I didn’t have to do anything special like this so I mark it up with how Mac OS X handles Frameworks differently from other Unix/Linux systems.

Hope this helps!

Update:

To use this compiled version in your terminal I found this alias helpful:

alias vim='/Applications/MacVim.app/Contents/MacOS/Vim'

django-celery for non-blocking Django signals.

So I am working on a project now that requires sending a SMS message when a certain task is performed on a model. So my first thought was, “Okay, easy. I can just perform those tasks with signals.” Well, kinda. Signals in Django are blocking and that just wasn’t going to work. I didn’t want my users to have to wait for HTTP responses to return from sending the SMS messages. That is where Celery and django-celery came into the picture.

What is Celery?

“Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well.”

Celery’s default message broker is RabbitMQ. Since I have had a little bit of experience with RabbitMQ from a Clojure project I worked on over a year ago that is what I chose. To install RabbitMQ on Ubuntu:

sudo apt-get install rabbitmq-server

You can read all about how to setup django-celery in the documentation. It is somewhat brief but along with celery’s documentation it will get you all the way there.

So what did I do to get non-blocking signals? Well I used Django’s signals to call my celery tasks defined in tasks.py in my application.

@receiver(post_save, sender=Reading)
def alert_delay(sender, **kwargs):
    if kwargs["created"]:
        send_alerts.delay(kwargs["instance"])

This delegates send_alerts to be called “at a later time”. Celery handles this by pickling the arguments, queuing the task in RabbitMQ and handling the execution of the tasks “later”.

As I said, I did a little bit of work with Clojure and RabbitMQ. It sure wasn’t as easy as this. I am quite impressed, impressed enough to blog about it.

Flask routing tricks

While working on a project today I wondered how I might complete this particular task.  I needed user profiles. Each profile was going to require an extra form field or two and I wanted to have different urls but the same “view”.  So I am going to show what I did to solve my problem.

Since Flask routing is just a decorator, you can add as many decorators as you want to the view function:

users = Module(__name__, name="users", url_prefix="")

@users.route("/affiliates/new")
@users.route("/users/new")
def new():
    form = RegistrationForm()
    return render_template("users/new.html", form=form)

The route() function just passes keyword args onto werkzeug.routing.Rule which accepts a dictionary called defaults. As you would think, defaults sets default values to variables accepted by the view function. So I decided to use the defaults dictionary to set the value for profile_type. This will never be overwritten since I am not accepting profile_type as a url parameter.

users = Module(__name__, name="users", url_prefix="")

@users.route("/affiliates/new",
             defaults={'profile_type': 'affiliates'})
@users.route("/users/new",
             defaults={'profile_type': 'sales'})
def new(profile_type=None):
    # Handle form with profile_type variable...
    return render_template("users/new.html", form=form)

This might be a little hackish but it works like I thought it would.

Leiningen + Clojure + Google App Engine = Interesting…

So today I started messing around with Google App Engine for Java today.  I thought it would be interesting to see how I might get this done with Leiningen.  You can take a peek at what I did in the github repo.

Basically all you do is:

$ lein deps
$ lein compile
$ dev_appserver.sh war

And that serves up the project via the dev server.

Introducing Humongous

Yesterday I released a simple web interface to mongoDB named Humongous, and I mean simple.  It is written in Clojure and should be really simple to get up and running given you have leiningen installed.  I really don’t have any idea where it will go from here but I want to maintain its simplicity and usefulness.  Props to Wilkes for his a really good mongoDB clojure library.

Moved to Wordpress

I got so sick and tired of dealing with comment spam I have moved my blog to Wordpress.  I plan on migrating all of the content (that was actually looked at) in the next day or two.  Stay tuned…

Remote Pair Programming

I know I haven’t always been the biggest fan of pair programming but I don’t think [in my past life](http://www.cabedge.com) it wasn’t a feasible solution.  At [work](http://www.notifymd.com) the other day we came up with a solution for pair programming while we are working from home.  We knocked it out and man it is pretty sweet.
Here are the things you are going to need
1) Server/Computer (with ssh access) that is accessible by both parties.
2) The computer must have screen.
You will need to run:
:::bash
$ sudo chmod u+s /usr/bin/screen
so that screen will allow for multiple users accessing the same executable.
Here is the [gist](http://gist.github.com/71662) of .screenrc located in your home directory.
Once you have all of this setup you are ready to go.
1) User 1 should log into the computer and if a screen session isn’t already started run:
:::bash
$ screen
If a screen session is already started then run:
:::bash
$ screen -r
2) User 1 should allow User 2 to access their screen session.  This is done by pressing Ctrl-A : then:
:::bash
acladd *user2-ssh-username*
3) User 2 should ssh into the computer and run:
:::bash
screen -x *user1-username*/
That should be it, both users should be able to see each others work.  This functionality only works well with command line editors but Vim and Emacs are great and powerful tools that should be a part of every programmers arsenal.
**Note:**  This assumes that all network settings are correct for each individual to access the box accordingly.

I know I haven’t always been the biggest fan of pair programming but I don’t think [in my past life](http://www.cabedge.com) it wasn’t a feasible solution. At work the other day we came up with a solution for pair programming while we are working from home.  We knocked it out and man it is pretty sweet.

Here are the things you are going to need

  • Server/Computer (with ssh access) that is accessible by both parties.
  • The computer must have screen.

You will need to run:

$ sudo chmod u+s /usr/bin/screen

so that screen will allow for multiple users accessing the same executable.

Here is the gist of .screenrc located in your home directory.

Once you have all of this setup you are ready to go.

  • User 1 should log into the computer and if a screen session isn’t already started run:
$ screen

If a screen session is already started then run:

$ screen -r
  • User 1 should allow User 2 to access their screen session.  This is done by pressing Ctrl-A : then:
acladd *user2-ssh-username*
  • User 2 should ssh into the computer and run:
screen -x *user1-username*/

That should be it, both users should be able to see each others work.  This functionality only works well with command line editors but Vim and Emacs are great and powerful tools that should be a part of every programmers arsenal.

**Note:**  This assumes that all network settings are correct for each individual to access the box accordingly.

Authorize.net Clojure library

Here is my first stab at building a quick clojure library http://sethtrain.github.com/clojure-authorize/

Clojure

Website: http://www.clojure.org

A buddy of mine told me about a new Lisp dialect called Clojure. I have never used Lisp before (other than setting up my Emacs environment) but this looks cool, if you can get over looking at all the parentheses. The language is built on top of the JVM so you have access to any Java packages from Clojure.

I think this is really huge especially from the standpoint of “enterprise” development. I develop every day in Django and Django is awesome. One problem that I have experienced while working with a web development framework that isn’t built in .NET or Java is that you basically give up working with “enterprise” companies because they tend to not trust anything other than those two languages, which is a problem in and of itself. Since clojure runs on JVM anything written in clojure will run on any machine running the JVM, kinda side stepping the idea that developing for “enterprise” has to be .NET or Java.

So I am going to look at Clojure and see what it has to offer. I like learning new things especially when they make me more of a geek.

jQuery Polling plugin

I wanted to create a chatting application and decided to create a jQuery polling plugin specifically for that purpose. I don’t know for sure if this is useful to anyone else but it works for me.

(function($) {
    $.fn.poll = function(options){
       var $this = $(this);
       // extend our default options with those provided
       var opts = $.extend({}, $.fn.poll.defaults, options);
       setInterval(update, opts.interval);

       // method used to update element html
       function update(){
           $.ajax({
               type: opts.type,
               url: opts.url,
               success: opts.success
           });
       };
    };

    // default options
    $.fn.poll.defaults = {
       type: "POST",
       url: ".",
       success: '',
       interval: 2000
    };
})(jQuery);

*Usage*

$("#chat").poll({
    url: "/chat/ajax/1/messages/",
    interval: 3000,
    type: "GET",
    success: function(data){
        $("#chat").append(data);
    }
});