Saturday, March 30, 2013

Grasp: A Leap Motion mouse controller for OSX


Check out the code here: https://github.com/phyous/grasp

Developing for the Leap Motion controller in Scala


I just got a hold of a Leap Motion controller today and I have to admit it's a neat little device with a lot of potential. The first thing I did was figure out how to interface with the device using Scala. Here are some quick pointers on how to get setup.

Before reading further, entire process I outline below is summed up in the following github repo. You can clone it and run ./install then ./run to produce the following results:


Instructions

1- Download the SDK here.

2- Assuming you have maven installed, create a project using the following command
mvn org.apache.maven.plugins:maven-archetype-plugin:2.2:generate \
-DarchetypeGroupId=org.scala-tools.archetypes \
-DarchetypeArtifactId=scala-archetype-simple \
-DarchetypeVersion=1.3 -DgroupId=leap-scala \
-DartifactId=leap-scala -Dversion=1.0.0 -DinteractiveMode=false

3- Install the leap motion sdk jar in your local maven repository (found in the sdk package from step #1 under /LeapSDK/lib/LeapJava.jar). Use the following command:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
    -Dfile=PATH_TO_JAR \
    -DgroupId=com.leapmotion.leap -DartifactId=leapMotion \
    -Dversion=1.0.0 -Dpackaging=jar

4- Add a reference to this jar file in the leap-scala/pom.xml that was created in step #2.
    
    <dependencies>
        <dependency>
            <groupid>org.scala-lang</groupid>
            <artifactid>scala-library</artifactid>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupid>com.leapmotion.leap</groupid>
            <artifactid>leapMotion</artifactid>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

5- Write some code for capturing events from the device. Here are some snippets from the leap provided samples I ported to scala:


6- Once you have some code, you can bundle it along with the leap motion library in a "fat" jar using the maven-assembly-plugin. Check out a sample pom.xml that does this here. You can then bundle the app using the following maven command
 
mvn install

This will produce a jar file called "leap-scala-1.0.0-jar-with-dependencies.jar" under the target folder.

7- Once your code is compiling, it's time to run. You'll need to point the jvm runtime to the native leap motion libraries for interfacing with your OS. On OSX, this is a file called libLeapJava.dylib found in the same directory of the SDK as the jar file. Here is how you would run your app once it's compiled:
 
java -Djava.library.path=".:./libs" -jar target/leap-scala-1.0.0-jar-with-dependencies.jar

Using the code above, you should be able to see input directly form the device on your console.

These instructions were generated while developing on OSX. Things might need a little bit of tweaking if running on other platforms.

Thursday, March 7, 2013

How to pretty print JSON from the command line

JSON has become one of the main formats for exchanging data through HTTP APIs. Being able to request and properly print json in your console is useful from time to time. Here are three ways to easily "pretty print" your data.

Option 1 - Simple formatting using python 


If you have access to python on your system, simply add an alias to "python -mjson.tool" to pretty print any well formed json. Execute one of the following commands to add the alias directly (depending on whether you use bash or zsh):
See an example in action:

Option 2 - Prettyjson: a colorized option


prettyjson is a node.js package that you can call form the command line to get colorized and bracket stripped json printed in your CLI.

To install:
Once installed, you can try it out. Here is a before shot:

And after:

As you can see prettyjson does a good job of stripping superflous brackets and quotes. 

Note: If you can't execute prettyjson after installing it, make sure the node package manager bin folder is in your path. You can do so by adding the following to your .bashrc/zshrc:
export PATH=/usr/local/share/npm/bin/:$PATH

Option 3 - underscore-cli


Yet another node.js based option is the underscore-cli package. It doesn't strip some of the less useful json characters form the display like prettyjson, but it does colorize your text and it has other powerful features.

To install:
sudo npm install -g underscore

Underscore pretty formatting in action:

It's not just pretty printing that makes this package neat, you can extract specific portions of it as well:

Head on over to ddposon's github page for more details.

Sunday, March 3, 2013

Babel-hose: Real-time Twitter translations


"Now the whole world had one language and a common speech." -- Genesis 11:1

This was a weekend project of mine which combined Twitters streaming API and Bing translator allowing you to understand what's happening everywhere in the world in real time in your native language. This project leverages the recently released hosebird framework for easy access to the sample and filter streams.

Getting started with the Bing translator API


Bing Translator is the best freely available machine translation API that I know of. If you're interested in getting set up to use the API to make your own translation enabled apps, follow this guide.

Create an Azure marketplace account

1- Go to http://datamarket.azure.com/ and login using a Microsoft live account. If you don't have one, you'll need to create one beforehand. You can do that here.

2- Complete the registration:


3- Once logged in, head on over to the bing translator subscription page and select the free 2,000,000 character/month subscription package.




4- If you go back to your account page under "My data" you should see the following:




Create an Azure application

Once you have an Azure marketplace account with a subscription to Bing translator, the last step is to create an application. By doing this, you'll have a client key and secret you can use to call the API. 

1- Head on over to the application registration page. Once there, fill out all the necessary information. For Bing translator, you won't be using a redirect uri (even though it's mandatory). You can put any valid URI you want to complete the form. ** Before hitting submit, copy the Client secret and Client ID. We'll need these later.

2- If you go back to the application registration page, you should see your app show up:

Call the API

Using the client key and secret from above, you have all you need to call the API. If you're going to be implementing a client to call the API yourself, you'll need to first authenticate in order to get an access token, then use that in the header of your requests for authorization. 



I wrote some sample code(here) that retrieves the access token, before realizing there was a really great client for the translator written already called microsoft-translator-java-api. Here is some sample java code using this library which takes care of authentication and translation requests under the covers:


Here is a more involved project that involves Twitters streaming api and bring translator:

Here are some additional resources you might find useful:

Bonne Chance! Que la force soit avec vous.

Wednesday, February 6, 2013

Adding code snippets to your blog


When writing technical articles about programming, one needs to add code snippets for reference from time to time. The following post highlights two methods I find both simple and easy to use. 

Github Gist



Think of a Gist as a single source controlled file that can be edited, cloned and shared easily. This is one of my favorite options for sharing small code snippets. It has the added advantage of being version controlled as well.

Instructions
1- Head on over to gist.github.com
2- If you have a github account (which you should) sign in. Start by adding a file name for your gist; the syntax highlighting will automatically be detected by the extension:



3- Make sure to create the gist as "public". Once you do so, you'll see a "embed this gist" link ont he left hand side in the following format:
<script src="https://gist.github.com/<user_id>/<gist_id>.js"/>



4- Copy that and embed it in your blog. Here is an example with some ruby code:

SyntaxHighlighter



If you don't want to bother with an external service and you'd rather host your code as part of your site, there is another popular option called SyntaxHighlighter. It's essentially a set of javascripts you can add to your blog/website to easily display syntax-highlighted code in a variety of languages.

Instructions
(based on original instructions from Alex Shirmanov)
1- Go to your blog's html source and add the following in your <head> section. 
<!-- SyntaxHighlighter -->
<!-- 1- Core javascript and css -->
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCoreDefault.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js' type='text/javascript'/> 
  
<!-- 2- Brushes -->
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushScala.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>  
  
<!-- 3- Activate SyntaxHighlighter -->
<script language='javascript' type='text/javascript'> 
    SyntaxHighlighter.config.bloggerMode = true;
    SyntaxHighlighter.all();
</script>
Note #1: To edit the layout of a blogger blog, go to Template -> Edit Html
Note #2: If you have your own hosting capabilities, you might want to upload these files to your own server

2- Notice that in the brushes section (#2) we are loading various javascripts for different programming language syntaxes. For a full list of what's supported, see here. If you need support for a given language, just add an additional reference to shBrushXXX.js where XXX is the language you want support for.

3- We're now ready to add code snippets to your blog. You can do so easily by adding the following html (replacing brush_name with the language of your code snippet):
<pre class="brush: brush_name">
// Enter code here
</pre>

Here is an example with class="brush: scala":
// Hello world in scala
object HelloWorld {
    def main(args: Array[String]) {
      println("Hello, world!")
    }
  }

Happy blogging!

Tuesday, February 5, 2013

Multi-threading in JRuby

The traditional Ruby you might be used to running (along with other interpreted languages such as python) make use of a global interpreter lock which ensures that only one thread can interpret code at any given time. In Ruby 1.8, the process is only allocated one OS thread meaning any attempts at parallelism are basically useless. In Ruby 1.9 things were improved slightly; the GIL still only runs on one thread, but the process can take advantage of multiple threads. This means that anything I/O bound can run concurrently (database queries, network requests) but you won't be able to make effective use of the processing power of all of your cores.

JRuby on the other hand, is built upon the JVM. One can make use of "true" native threads which are able to run concurrently with you parent thread.

To visualize how this works check out this diagram:


Read more about the GIL here (from which I borrowed the above diagram).

Setup

Here are some quick instructions on how to setup JRuby on your machine so we can test out multi-threaded performance. The easiest way to install JRuby in my opinion is through Ruby Version Manager. RVM is essentially an easy way to manage multiple ruby versions (and their associated gems) on a single machine. For Linux/Unix/OSX instructions, see here. For windows go here.

1- Install JRuby:

>rvm install jruby

2- Check that it's installed

> rvm list gemsets









3- rvm use <name of the jruby gemset>
> rvm use jruby-1.7.1

4- Test it out in irb (ruby's interactive console). You should see "java" as the
RUBY_PLATFORM if things are installed properly:






Sample App

The following are two sample apps which showcase multi-threading in JRuby vs. Ruby 1.9 vs Ruby Enterprise Edition 1.8.7.

JRuby Example (also showcasing how easy it is to call java code from Ruby):

Standard Ruby example:
To run the experiment:
1- Run in Jruby:
> ruby multi_thread_jruby.rb

2- Run in Ruby 1.9.3
> rvm install ruby-1.9.3-p286
> rvm use ruby-1.9.3-p286
> ruby multi_thread_ruby.rb

3- Run in Ruby 1.8.7
> rvm install ree-1.8.7-2012.02
> rvm use ree-1.8.7-2012.02
> ruby multi_thread_ruby.rb

Average completion time summary:
JRuby(1.7.1):            199.3ms
Ruby(1.9.3p286):         610.0ms
Ruby(ree-1.8.7-2012.02): 748.6ms

As we can see, JRuby is close to 4 times faster than Ruby 1.8 on my 4 core MacBook Air. Ruby 1.9 performs slightly better by being able to take advantage of time during which the process is I/O blocked. Overall, JRuby is the clear choice for maximizing multi-threaded performance.

Additional Reading: 


More on calling Java from JRuby:
https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby

An interesting look at benchmarks of JRuby compared to other ruby versions. This shows that JRuby is not better in all scenarios: 
http://etehtsea.me/the-great-ruby-shootout

#SeekDiversePerspectives