Justin Marney

Installing Postgresql 9.3 Beta using Homebrew

Recently, I've been working on a project that relies heavily on Postgresql and I've come to appreciate its flexibility. New features like hstore, json and plv8js allow for rapid application development. Index-only scans and native streaming replication make Postgresql even more appealing in a traditional context. After reading about the forthcoming features of 9.3, I went in search of a way to install 9.3 beta 1 along side my existing 9.2 installation.

The magic begins with petere's homebrew-postgresql formulae. The first step is to follow the README and brew tap the repository.

brew tap petere/postgresql

Next, install postgresql-9.3.

brew install postgresql-9.3

This will install postgresql into /usr/local/opt/postgres-9.3. It will not symlink any of the executables in /usr/local/opt/postgres-9.3/bin to /usr/local/bin because the formula was added via brew tap. This means that the existing brew-based postgres executables won't be overwritten by 9.3. It also means that there's a little more legwork involved in getting everything setup. Currently, I'm just calling the executables directly via their full path, but you could add /usr/local/opt/postgresql-9.3/bin to your PATH and avoid lots of typing.

At this point, we need to initialize the database.

/usr/local/opt/postgresql-9.3/bin/initdb /usr/local/var/postgres-9.3 -E utf8

Once that finishes you can start postgres manually using the following command.

/usr/local/opt/postgresql-9.3/bin/pg_ctl -D /usr/local/var/postgres-9.3 -l /usr/local/var/postgres-9.3/server.log start

If you want postgres to automatically start on boot, use the following plist to create a LaunchAgent.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>homebrew.mxcl.postgresql</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/opt/postgresql-9.3/bin/postgres</string>
    <string>-D</string>
    <string>/usr/local/var/postgres-9.3</string>
    <string>-r</string>
    <string>/usr/local/var/postgres-9.3/server.log</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>WorkingDirectory</key>
  <string>/usr/local</string>
  <key>StandardErrorPath</key>
  <string>/usr/local/var/postgres-9.3/server.log</string>
</dict>
</plist>

Save this plist into /usr/local/opt/postgresql-9.3/homebrew.mxcl.postgresql.plist. Then symlink this file into ~/Library/LaunchAgents using the following command.

ln -sfv /usr/local/opt/postgresql-9.3/*.plist ~/Library/LaunchAgents

Lastly, start up postgres immediately using launchctl.

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Check out the 9.3 beta 1 release notes or Postgres 9.3 feature highlight: JSON data generation for more information on some interesting new features.

read more

and && or

Here are two resourses that explain the operators "and" and "or" in Ruby. The first is by Avdi Grimm, Using “and” and “or” in Ruby. The second details the origins of the operators, Ruby: On The Perl Origins Of “&&” And “||” Versus “and” And “or”.

Github Pages & Jekyll

I finally finished migrating this site from mephisto to github pages & jekyll. I lost the comments feature but gained the ability to manage content in a git repository. I experimented with disqus and intense debate but both services annoyed me with too many features and complex markup.

Handlebars.js & Mustache.js Benchmarks

Yehuda Katz just released Handlebars.js, a compiled javascript Mustache implementation, similar to Mustache.js with some useful additional features. The README mentions,

"In a rough performance test, precompiled Handlebars.js templates rendered in about half the time of Mustache templates. It would be a shame if it were any other way, since they were precompiled, but the difference in architecture does have some big performance advantages."

Interested in the specifics, I did some benchmarking based on Brian Landau's Benchmarking Javascript Templating Libraries and came up with the following results.

I made some tweaks to the overall benchmarking approach, isolating the actual template rendering from any DOM manipulation and removing the setTimeout mentioned in PPK’s benchmarking methodology as it was no longer necessary. The time it took to render the template 5k times was recorded and averaged out over 10 tries. Not terribly scientific, but hopefully good enough to show that in both simple and iterative cases Handlebars.js is about 2x as fast. The code I used to achieve these results is in my js-templates-benchmark repo.

read more

Bypass Featured on TV

I'm currently the lead developer on a mobile web application that was just featured on KVUE in Austin, Texas. The app has been amazing to work on as it's provided a host of challenges I've never faced before. The service the app provides is even more exciting as I'm a die-hard Redskins fan and have spent my fair share of time standing in line at FedEx Field. You can check out the details on the official Bypass Lane Website or you can check out the Bypass TV clip on the KVUE website.

Distributing Your Data

This weekend I gave a presentation at Devnation Portland on some of the concepts behind distributed data stores. You can checkout the slides on my talks page or you can download the PDF. Thanks to all the speakers and to everyone who came out! I had a great time and I'll definitely be attending, and hopefully speaking, at another Devnation event in the near future.

Language.rdoc

While programing I usually have a few different rdocs open for the various libraries I am working with. I also have a dictionary and a thesaurus on hand and have often noticed I reference them more than the rdocs. I've always felt that there is a literary art form to abstraction naming but I have never been able to succinctly state why I felt this was so important. It existed as a strong intuition, something I knew was worth focusing on.

Ruby Hacknight to the Rescue

In order to prepare for the next Ruby Hacknight I have been reading Refactoring, Ruby Ed. and recently came across this quote which states precisely why proper naming is important.

So much of object-oriented design depends on the effectiveness of the abstractions that you create. Let’s say you have object A that delegates to object B, which in turn delegates to object C. It is much easier to understand the algorithm if each object can be synthesized in isolation by the reader. To provide for this, the clarity of the public interface of the object being delegated to is important. If object B’s public interface represents a cohesive piece of behavior with a well-named class, well- named methods, and parameter lists that make sense given the name of the method, a reader is less likely to have to delve into the details of object B to understand object A. Without this clear abstraction around the behavior of object B, the reader will have to move back and forth between object A and object B (and perhaps object C as well), and understanding of the algorithm will be much more difficult.

Fields, Jay, Shane Harvie, Martin Fowler and Kent Beck. Refactoring. Ruby ed. Upper Saddle River: Addison-Wesley, 2010.

Rails 3 Generators: Hooks

After a cold and dismal winter which has left this blog as barren as the icy tundra, I have emerged from a state of deep hibernation with an informative blog post on Rails 3 generator hooks. Watch out because a follow up post that details how you can use Rails 3 generator hooks in your own custom generators is hot on the heels of this post. In addition, let it be known that I have officially broken the vow of winter silence I took 3 months ago.

Hackday: Intro to Ruby Meta-Programming

Me and my esteemed colleague, Matt Swasey, just wrapped up the third Hackday at Viget Labs. For this Hackday we led a discussion surrounding the mind-altering world of meta-programming. You can read a detailed summary of the event on the Viget Extend blog. You can also take a look at the code and notes we put together on GitHub.

Pygrack, Pygments Highlighting Middleware

Rack, the ruby web server interface, has a powerful feature called rack middleware that allows you to filter requests and responses in your application. In an effort to explore the possible uses of middleware Lunar Logic Polska created CodeRack, a rack middleware coding contest. When Matt Swasey, a fellow Viget Labs developer, saw that Trevor Turk had created a pygments API he suggested that we create a CodeRack entry that uses the API to provide syntax highlighting to your rack application.

Pygments

Pygments provides syntax highlighting via a Python library and command-line interface. Trevor Turk created a Google App Engine that provides an HTTP interface to the library. Basically, you send the engine some HTML via an HTTP request and it sends you the pygmentized version of the code you passed in. This allows systems and applications that don't have python available to use pygments. If you do have python installed you can add syntax highlighting to your ruby application via the albino library, a small wrapper around the pygments command-line interface.

Pygrack

Pygrack uses the built-in net-http library and nokogiri, an impressive markup parser, to inject pygment syntax highlighting into your rack application. In order to use Pygrack you'll need to add it to your rackup config.

require 'pygrack'

map "/" do
  use Pygrack
  run Rack::Directory.new("./public")
end

Next, wrap your code block in a pre tag with a class attribute that contains the language you want to apply to the block.

# make sure you dry all your glue

def sticking
 @gloo = 'pasting'
 puts @gloo
end
puts "glue is good for #{sticking}"

At this point you'll need to include one of the pygments css files. You can grab the default one from Trevor's pygments API repository or choose one from the list here. Start your rack application, using shotgun perhaps, and you should see some sweet pygments syntax highlighting. You can get the pygments code and see a simple rack application example at the pygrack github repository.

read more

simple_importer, a csv processing gem

I just released a tiny gem that helps in creating CSV import tasks. Check out the introductory blog post on the Viget Labs developer blog.

Getting XML Into Your Processing Sketch

During one of the recent Learning Processing sessions, Keith asked how one would go about getting data from a web service, such as YQL, into a Processing sketch. After poking around the Processing library reference we were able to create an example of how to pull in data from a web service and use it to control the parameters of a sketch.

It turns out getting XML data into Processing from a web service is pretty easy. The first thing we need to do is import the built-in XML library.

import processing.xml.*;

Next we construct our query string using a YQL query that returns the most interesting flickr photos. You can take a look at the result here select * from flickr.photos.interestingness(10). One of the cooler aspects of XMLElement is that you can initialize an object with a url and it will take care of making the http request and fetching the XML.

String query = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20flickr.photos.interestingness(" + count + ")&format=xml";
XMLElement xml = new XMLElement(this, query);

There are two children returned in the XML: diagnostics and results. We want to examine the data in results, so we get that block of XML via the getChild method. Children are indexed starting at 0, so the second child is at index 1.

XMLElement results = xml.getChild(1);

At this point we can stop and party for a while because we have technically achieved our goal. We have pulled some data into our sketch and it is sitting inside our XMLElement object. The next step is to iterate through the data and do something with it.

Before we started to code we took some time to analyze the data and figure out a few different ways to display it. I noticed that with each record flickr returned a server id. We assumed this identified the server that contains the picture. I was curious as to how distributed the results were and we decided to write a sketch based on this aspect of the data. One of the most interesting things to do in Processing is to find new ways of looking at data in order to expose hidden characteristics.

The first thing we'll do is iterate through the records using a for loop. The getChildCount method allows us to define the for loop conditions and we use getChild inside the loop to access individual results. The getStringAttribute lets us get specific attribute values from a particular record so we'll use that to get back the server id.

for(int i = 0; i < results.getChildCount(); i++) {
  XMLElement result = results.getChild(i);
  String server = result.getStringAttribute("server");
}

We tie all together by first checking to see if we've created an entry in our HashMap. If we haven't, we create one and put a 1 in the counter. If we find an entry already exists, we increment the counter stored at that key and save it.

HashMap hm = new HashMap();
for(int i = 0; i < results.getChildCount(); i++) {
  if(hm.get(server) == null) {
    hm.put(server, 1);
  } else {
    Integer sum = (Integer)hm.get(server);
    sum += 1;
    hm.put(server, sum);
  }
}

When the loop finishes running we'll have a HashMap with keys that correspond to server ids and values that contain the number of images that server holds. We took that data and made a simple processing sketch that creates ellipses which are larger and more red if they are serving more images. In the end, the sketch turned out to be a strange sort of load monitoring visualization for flickr. You can get the code for this sketch here. Here is the full code for our HashMap creation method.

import processing.xml.*;

HashMap serverCounts(int count) {
  String query = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20flickr.photos.interestingness(" + count + ")&format=xml";
  XMLElement xml = new XMLElement(this, query);
  XMLElement results = xml.getChild(1);

  HashMap hm = new HashMap();
  for(int i = 0; i < results.getChildCount(); i++) {
    XMLElement result = results.getChild(i);
    String server = result.getStringAttribute("server");
    if(hm.get(server) == null) {
      hm.put(server, 1);
    } else {
      Integer sum = (Integer)hm.get(server);
      sum += 1;
      hm.put(server, sum);
    }
  }
  return hm;
}

read more

Learning Processing, Lesson 1

"Over the next few weeks months I will be teaching a small introduction to programming course at Viget Labs. I'll be using Processing as a means to explore some basic programming concepts and I'll be following the excellent book Learning Processing: A Beginner's Guide to Programming Images, Animation, and Interaction by Daniel Shiffman. Each lesson will have an accompanying blog post that outlines the topics we'll try to cover and links to any resources we may need. Here are some notes on Lesson 1."

Points

  • Explained the difference between the Cartesian coordinate system and the Processing coordinate system.
point(x, y);
line(x1, y1, x2, y2);

Shapes

rect(x, y, w, h);
rectMode(CORNER); // default

rectMode(CENTER);

rectMode(CORNERS);
rect(x1, y1, x2, y2);

ellipseMode(CENTER); //default

ellipse(x, y, w, h);

Color

  • Covered grayscale, RGB, HSB and alpha colors in Processing.
  • Walked through some examples in order to explain how color funcations apply to shapes.
noStroke();
stroke();
noFill();
fill();

Zoog!

http://www.learningprocessing.com/examples/chapter-1/example-1-5/

The Processing Application

  • Serves as the Processing IDE, Integrated Development Envronment.
  • IDE is a fancy acronym that refers to the app you write code with.
  • Talked about the idea of "sketches" & the sketchbook.

Syntax

  • Reviewed the three general statement types of function calls, assignments, and control structures.
  • Explained that in the beginning, almost every line will be a function call.
  • Dissected a function call into its constituent parts of name & arguments.
println("omg sheuz!");
// single line comment

/*
multi
line
comment
*/

The Reference

http://processing.org/reference/

Interaction

  • Discussed setup() and draw().
  • Covered the mouseX, mouseY, pmouseX, pmouseY constants.
  • Showed that draw() redraws on each frame by placing background() in setup().
  • Defined some mousePressed(), mouseReleased(), and mouseClicked() event handlers.
void draw() {
  background(208);
  // a rect that follows the mouse around

  rectMode(CENTER);
  rect(mouseX, mouseY, 20, 20);    
}

void draw() {
  // draw on the screen with a pen

  line(mouseX, mouseY, pmouseX, pmouseY);
}

read more

Learning Processing, Introduction

Over the next few weeks months I will be teaching a small introduction to programming course at Viget Labs. I'll be using Processing as a means to explore some basic programming concepts and I'll be following the excellent book Learning Processing: A Beginner's Guide to Programming Images, Animation, and Interaction by Daniel Shiffman. Each lesson will have an accompanying blog post that outlines the topics we'll try to cover and links to any resources we may need. Here is the first installment!

What is this book?

This book tells a story. It is a story of liberation, of taking the first steps toward understanding the foundations of computing, writing your own code, and creating your own media without the bonds of existing software tools. This story is not reserved for computer scientists and engineers. This story is for you.

Who is this book for?

Because this book uses Processing (more on Processing in a moment), it is especially good for someone studying or working in a visual field, such as graphic design, painting, sculpture, architecture, film, video, illustration, web design, and so on. If you are in one of these fields (at least one that involves using a computer), you are probably well versed in a particular software package, possibly more than one, such as Photoshop, Illustrator, AutoCAD, Maya, After Effects, and so on. The point of this book is to release you, at least in part, from the confines of existing tools. What can you make, what can you design if, instead of using someone else's tools, you write your own? If this question interests you, you are in the right place.

What is Processing?

Old: Text In -> Text Out -> Text Interaction
New: Text In -> Visuals Out -> Mouse Interaction (and more)
Not novel idea: Logo, Design by Numbers
Novel implementation: Java, Open Source, Community Driven

Write in this book! (or on some paper)

Let's say you are a novelist. Or a screenwriter. Is the only time you spend writing the time spent sitting and typing at a computer? Or (gasp) a typewriter? Most likely, this is not the case. Perhaps ideas swirl in your mind as you lie in bed at night. Or maybe you like to sit on a bench in the park, feed the pigeons, and play out dialogue in your head. And one late night, at the local pub, you find yourself scrawling out a brilliant plot twist on a napkin.

Well, writing software, programming, and creating code is no different. It is really easy to forget this since the work itself is so inherently tied to the computer. But you must find time to let your mind wander, think about logic, and brainstorm ideas away from the chair, the desk, and the computer. Personally, I do all my best programming while jogging.

Course Outline

Lesson 1: Chapters 1-3
Lesson 2: Chapters 4-6
Lesson 3: Chapters 7-8
Lesson 4: Chapter 9
Lesson 5: Chapter 10–11
Midterm! (Also, continue Lesson 5: Chapter 12)
Lesson 6: Chapter 13–14
Lesson 7: Chapter 15–16
Lesson 8: Chapters 17-19
Lesson 9: Chapters 20-21
Lesson 10: Chapters 22-23
Final Project Workshop
Final Project Presentations

Learning Processing, the website!

You will find many resources here such as tutorials, examples, & exercises. Use this website, it is your friend.

Incremental Development

You will have bugs. Working in small increments and verifying the work you've done at each increment will help you catch bugs as soon as they are created. It will help you fail fast, which is a good thing in the software development world. In addition to bug squashing, working incrementally lends itself to object-oriented programming, a powerful software development methodology that we'll cover in later lessons.

Algorithms

Programming is the practice of getting a computer to do what you want. It is about communicating with a computer. Computers communicate in only one way: algorithms. A computer will always succeed in performing a precise set of well-defined instructions that dictates what you want. Ever taken a programming course in college? Was it tedious as fuck? In general, algorithms are quite tedious and it is a fact we'll learn to accept and love. Thinking incrementally has the benefit of failing fast, which has the benefit of allowing us to fix the failure fast, which has the benefit of succeeding fast. The tedious nature you may have experienced in past programming courses was most likely a result of slow or negligible feedback. When you start thinking incrementally, what you want the computer to do becomes incremental. You start to see the feedback you want and see it often. Processing has the added benefit of giving you visual feedback and interactivity with a minimal amount of tedium. In a short time you will be incrementally developing algorithms that do exactly what you want!

read more

Excess Poultry Waste Contamination

While this post may not fit the strictly technical nature of my blog, the impact of unregulated poultry waste is an issue that directly affects me, and most likely you as well (especially if you live in or near Northern Virginia). There is a very important bit of legislation coming up regarding poultry waste regulation that you have an opportunity to voice your opinion about! Here is an explanation of why you should care and information on how you can help.

The Issue

I am an avid fly fisherman and I enjoy fishing the waters from the Shenandoah National Park all the way to the Chesapeake Bay. Unfortunately, these waters are subject to a wide variety of pollutants as they run through a gamut of industrial and developmental regions. The Shenandoah Valley is home to one of the largest poultry producing areas in the country. In addition, just east of the valley, suburban growth radiating outwards from the Washington, D.C. has reached the banks of the Shenandoah River. Further east, all of the urban population and its associated waste end up reaching our rivers through drainage systems. While the effort to keep our rivers and streams clean and healthy is clearly a multi-front battle there is a very important bit of legislation regarding poultry waste regulation that you have an opportunity to voice your opinion about. Poultry waste in our water has all sorts of heinous environmental impacts, mainly surrounding the extremely high levels of phosphorous and nitrogen that act as an algae fertilizer. Algal blooms deplete the oxygen from the water creating dead zones where no life can be sustained. This in turn causes widespread, long term stress on the fish population damaging their immune system and leaving them susceptible to normally innocuous aquatic bacteria. In addition, growth hormones present in poultry waste have a devastating effect on the endocrine system of fish leading to fish inter-sex (male fish producing female eggs). This combined degradation has led to widespread fish kills in addition to rendering many areas of waterfront unusable for recreational purposes. The regulation itself aims to close a legal loophole that has existed in the Shenandoah Valley (one of the top ten poultry producing regions in the country) for some time. Large scale industrial poultry farming operations are already regulated, however they are allowed to give their excess waste to neighboring farms that do not meet the production requirements for regulation. In turn, these farms do not dispose of the waste properly and it is making its way back into the Shenandoah River.

How You Can Help!

The legislation in question is 9VAC25-630 Virginia Pollution Abatement General Permit Regulation for Poultry Waste Management and can be found starting on page 101 (or 3867 according to the TOC) to page 120. If you wish to offer your support, leave a short comment here on the petition. Provide your name, how you use the waterways, and that you support this regulation. You can take a look at the comment I left for inspiration.

The Catch!

THE PETITION CLOSES ON FRIDAY AUGUST 21, 2009! Short notice, I know. Please, if you wish to show your support sign it ASAP.

Resources

9VAC25-630, start on page 101 (or 3867 according to the TOC) - 120
The Petition
The Health of the Shenandoah River in Clarke County
Intersex in Smallmouth Bass Coincident with Population and Agriculture ...
Study warns of pitfalls to poultry waste as fertilizer substitute
Hormones found in field runoff can disrupt fishes’ endocrine systems (PDF) Poisoned Waters, Frontline report on Chesapeake Bay pollution

read more

Monome & Lily

I recently gave a presentation at Developer Day on the Monome and Lily. Here is a brief synopsis of the talk as well as some resources to help you get started making patches in Lily.

An Introduction

I am Justin Marney.
I work at Viget Labs building websites using Ruby on Rails.

Goals

Introduce the concept of visual programming via a live demo.
Dig into a unique browser-based visual programming language named Lily.
Learn how, when, and why to use a visual programming language.
Show off the power of Lily via a DOM-driven sound generator.

Game of Life Sequencer Performance

Explained how my Game of Life sequencer works and played a short improvised set. Nothing broke, so I was pleased.

The Monome

A physical computing interface with an open-source hardware platform.
Built by Brian Crabtree and Kelli Cain. The list of applications that run on the Monome.
The various models and information on how to order one for yourself.

Visual Programming Languages

Explained the basic concept behind visual programming languages.
Reviewed the basic language components and discussed common usage scenarios.
Showed how to create a basic Lily patch, which can be found here.

Lily

Lily is an open-source Firefox Add-On developed by Bill Orcutt.
Showed off Lily's ability to manipulate the DOM using a demo patch that scrambles the links on craigslist.
Created a custom external named cheerio using basic Javascript.

Open Sound Control

Briefly explained the technology behind Open Sound Control.
Described the Monome OSC protocol.
Showed a small example of how to use oscsend and oscreceive in Lily.

DOM-Driven Sound Generator Performance

Showed off a patch that turns DOM elements into sound bytes and allows me to sequence them using the Monome. For some reason the Monome wasn't getting the /led messages which was frustrating. Fortunately, the cheerio external allowed me to create a sequence via the mouse and we got to listen to the sounds of Star Trek, my twitter profile, and the Viget Labs website. It sounded industrial as hell. Trent Reznor would have been proud.

Further Reading

The pdf version of the slides.
All of the example patches and the keynote presentation.
The DOM Sound Generator
Monome
Lily
PureData
Max/MSP, the licensed version of PureData.
Ableton Live, the sound generator.

read more