z19r

go fish

Oct/13/2013

go fish

I've been playing with Go quite a bit lately, and found there to be quite a bit of boilerplate to get started with a new project. So I thew together gofish. It's a virtualenvwrapper inspired wrapper for working on Go projects in the Fish shell.

If you've used virtualenvwrapper (or virtualfish) the commands will be familiar to you.

To create a new go workspace:

$ mkgoworkspace project-name

This will created a project structure that looks like this:

/bin
/pkg
/src
    /zackkitzmiller
        /project-name
            main.go

The default namespace will be the currect user's username, but that can be overridded by setting the GOFISH_NAMESPACE environment variable:

$ set -x GOFISH_NAMESPACE github.com/zackkitzmiller; and mkgoworkspace project-name

To switch the GOPATH:

$ goon project-name

To deactive a workspace completely:

$ gooff

The various commands all emit events, so you can listen for them in your fish config. The available events to listen to are:

  • gofish_workspace_will_be_created
  • gofish_workspace_created
  • gofish_workspace_will_activate
  • gofish_workspace_activated
  • gofish_workspace_will_deactivate
  • gofish_workspace_deactivated

The following example would send a growl notification on workspace activation

function announce --on-event gofish_workspace_activated
    command growlnotify -m "Workspace: " (basename $GO_CURRENT_WORKSPACE) " activated" -n 'gofish'
end

The following global environment variables will be available after a successful workspace activation:

  • $GOPATH - as expected
  • $GOFISH_WORKSPACE
  • $GOFISH_NAMESPACE

This is still just a work in progress. Let me know what you think. And as always, pull requests welcome.

Todo:

  • multiple modules in same workspace
  • auto git?

git legit

Oct/10/2013

Seems that Kenneth Reitz nailed it again. It's been around for > 2 years, but this is such a neat little tool. Wish I would have found it earlier.

venuestagram

Sep/30/2013

Last week I was hanging out at the OnDeck Hackathon in NYC. After not doing much for a while, I decided I'd give myself an hour to build something, and unlike all of my previous hackathon projects, I wanted to actually deploy this to the internets, so I did.

So, what I built was venuestagram. It's a silly little app that pulls in all popular venues from the SeatGeek API and use the lat/lng I got to query the instagram API. I built it with Flask, and this python instagram wrapper I found.

Enjoy

did you just tell me to go fuck myself

Sep/25/2013

I'm putting this here because I can never find it when I google it.

did you just tell me to go fuck myself

Cute little WSGI error handling pattern

Aug/29/2013

Here's a cute little pattern I threw together when writing sixpack. I'm assuming you're using Werkzeug, but you could do the same thing with Flask, or pure WSGI.

First step is to write a little decorator that you're going to decorate your route handlers with. It's pretty straight forward. Here I used the decorator package to simplify things, but you can just do it in pure python if you don't need the additional dependency.

import decorator
from redis import ConnectionError

@decorator.decorator
def service_unavailable_on_connection_error(f, *args, **kwargs):
try:
    return f(*args, **kwargs)
except ConnectionError:
    return json_error({"message": "redis is not available"}, None, 503)

It's simply going to try to run the method you decorated, and catch a Redis ConnectionError. You could do this for any datastore, or anything else that's prone to complete failure. In this example, json_error() is a simple helper method that returns a JSON response with the correct Content-Type, status, etc. headers.

Then, in your server you can have something like this:

def __init__(self, redis_conn):
    self.redis = redis_conn

    self.url_map = Map([
        Rule('/_status', endpoint='status'),
    ])

... more werkzeug boilerplate

@service_unavailable_on_connection_error
def on_status(self, request):
    self.redis.ping()
    return json_success({'version': __version__}, request)

If self.redis.ping() throws ConnectionError the decorator will catch it, and return the failing JSON. So, like the title says, nothing groundbreaking, but cute and might save you a few lines.