CodeHax: Code to Deploy – Part one. Installing Homebrew, Ruby, Git & Sublime

I just recently taught an intro to Ruby on Rails class and the hardest part was not explaining data types or object oriented programming but how to install everything you need to begin with. If you are using a Mac, the good and bad news is it already has Ruby installed.

While you can play around with Ruby out of the box, if you want to do something more serious with it such as Rails, you will need to install the latest version & tell your computer to use that path rather than the one already on your system.

But before we even get there, using a package manager to download, maintain and easily install/uninstall other programs is also super helpful. For this, we are going to use Homebrew.

Step 1: Find the Terminal
First, you need to find your terminal (also known as a command prompt or command Line interface CLI).

Step 2: Install Homebrew

You will probably get a prompt asking you if you will install xCode. Its a large file and there are ways to not install it but Homebrew depends on xCode so just install it.

Step 3: Install Ruby

Step 4: Install Git

Step 5: Checking Git and Ruby installations

Step 6: Install Sublime Text
Pretty straight forward. Sublime is a text editor. This where we will edit code.
go to here and download it.

Part 2 will cover scaffolding a basic CRUD function application in Rails as well as Postgresql.

Sublime Hax: Handlebars Syntax Highlighting

I have been recently working in Ember.js a javascript frontend framework (like Angular or Backbone). Its uses a semantic template language for the HTML.

If you are writing your code in Sublime Text like I am, you will quickly find out that it doesn’t recognize Handlebars and so displays its a plain text. (Yuk >,< )

Luckily, you can install this handy dandy package found here

If you don’t have package manager for Sublime, read this post and it will get you up to speed.

Screen Shot 2015-06-24 at 2.43.59 PMIf

Screen Shot 2015-06-24 at 2.43.38 PM


Sublime Hax: Turning off the Error Checking for Anaconda in SublimeREPL

Screen Shot 2015-04-18 at 12.00.10 PM

So I been using the Anaconda Package in Sublime via Sublime REPL. If you are trying to do this too, I have a few posts on it that you should refer to.

Now if you are already doing this, you might see something like this when you load a file from someone else.Screen Shot 2015-04-18 at 12.03.03 PM

If you these error alerts drive you crazy and you want to turn some of them off, this is the post for you. At first, I use to go back and fix these errors but most of them deal with spacing around certain things like comments, equal signs, ( ) etc.

While it might be good practice to code in accordance to these conventions, going back to fix some one else’s code or even when you are writing some yourself, these errors can be extremely distracting. If you hover over the line, at the bottom of the Sublime window, it will tell you what the error is.

For example, the first error in this picture is “[W] PEP 8 (E501): line too long (89 > 79 characters), Line 2, Column 78” So what if you didn’t care that the line is “too long”. Lets change it so it stops showing us this error.

Step 1
Go to Sublime Text > Preferences > Package Settings > Anaconda  and Open: Setting – Defaults and Setting -User.
Screen Shot 2015-04-18 at 12.13.44 PM

Step 2
Copy all of settings JSON in the Defaults and paste it into the User Settings Window.

Step 3
Find the “pep8_ignore”: in the JSON (Mine was around line: 338)

Screen Shot 2015-04-18 at 12.16.42 PM

Step 4
Type "E501" so the JSON files looks like this:
Screen Shot 2015-04-18 at 1.30.10 PM

Step 5
Save the JSON file and you should see the error disappear!
Screen Shot 2015-04-18 at 1.38.16 PM

If you want to remove multiple error checking codes, connect new error codes with a , and make sure you put them in a ” ” like this: Screen Shot 2015-04-18 at 12.00.10 PM

See all the errors disappear! Beautiful.
Screen Shot 2015-04-18 at 1.41.59 PM

Sublime Hax: Setting System iPython to SublimeREPL

If you haven’t setup Sublime REPL or hacked it to use your system version of Python, please read my blog post on it.

I been fiddling with the Python console in Sublime for some time but everyone in the data science community uses iPython as it has some additional features and syntax highlighting (not too important since Sublime already does it for me)

Anywho its really easy to set iPython on Sublime REPL

1. Install iPython

I didn’t have a system version of iPython so I need to download it.
In your terminal type
pip install "ipython[notebook]"

This will install iPython and all of its dependencies.

2. Open up the SublimeREPL settings JSON
Screen Shot 2015-03-25 at 11.09.04 AM

Screen Shot 2015-03-25 at 11.13.38 AM

Screen Shot 2015-04-10 at 2.45.14 PM

Line 70 you want to type within the brackets [ ]
"/usr/local/bin/python", "-u", "${packages}/SublimeREPL/config/Python/"
This is assuming that your Python is installed at that location.

If you don’t know for sure, type into the terminal which python and it will tell you.

Go DatSci go!

Sublime Hax: PANDAS and NumPy in Sublime REPL

Sublime with Python REPL

Note: if you don’t have SublimeREPL installed in Sublime, refer to this post.

Problem: Your favorite Python Modules like PANDAS is not working 
If you try to import Pandas in the SublimeREPL, you are going to get an error saying it couldn’t find it, even if you have it install on your system. What the heck is going on!? Well, the SublimeREPL is not using your system version of Python and thus is unable to access the modules you have installed on it.

Solution: Change the SublimeREPL setting to use your System version of Python
Ok, first go to Sublime Text > Preferences > Browse Packages…
Screen Shot 2015-03-25 at 11.09.04 AM

Navigate to Packages > SublimeREPL > config > Python > Main.sublime-menu like so:
Screen Shot 2015-03-25 at 11.13.38 AM

You should see a file like this: Note Line 22 is not going to be commented out for you, that is of my own doing.
Screen Shot 2015-03-25 at 11.16.11 AM

Now you have the choice of either deleting or commenting out line 22. My line 23 is what you are going to want to add to this file. The path (“/Library/Frameworks/Python.framework/Versions/3.4/bin/python3”) is where my python3 is located on my system.

If you are using Python 2 and don’t know where it is on your machine, you can type which python into your command line (Terminal) and it should give you the path. Mine is at /usr/local/bin/python.
Don’t forget to add a comma at the end of your additional line!

Now, if you don’t have PANDAS installed, you need to do one of two things:
If you have Python 3.4 (Released in March), simply type into

pip3 install pandas

If you have a older version of Python:
You will need pip if you don’t have it. Type this into your Terminal
Then you can install PANDAS with this in

pip install pandas

And you are done!

Sublime Hax: What if you had a REPL in Sublime?

Sublime with Python REPL

I have grown so accustom to using Sublime that I when I started using Python (specifically the Anaconda package) for Spyder I was a little heart broken.

While Spyder is pretty awesome and relatively easy to use, I missed all the little stuff Sublime does for you. Also, I just really wanted to work out of one text editor. In this blog post, I will walk you though how to install SublimeREPL.

We are going to install SublimeREPL with package control, if you don’t have Sublime and/or Package Control, follow the instructions of this post first.

Install SublimeREPL
With Package Control, it is really easy to install new plugins! In Sublime hit Command + Shift + P and it will open the Command Palette. Alternatively, you can go to Tools > Command Palette. It will open up a interface towards the top of your editor like this:

Screen Shot 2015-03-25 at 10.50.07 AM

In that box, you want to find Package Control: Install Package:

Screen Shot 2015-03-25 at 10.52.25 AM

After you select it, you should get a box like this:

Screen Shot 2015-03-25 at 10.55.07 AM

In this box, type SublimeREPL and select it.

Opening your First Sublime REPL

I like to have two “screens” to see my code on one side and my REPL on the other. To do this, go to View > Layout Column 2

Screen Shot 2015-03-25 at 10.59.33 AM

Then go to Tools > SublimeREPL and choose the REPL for the language you want.
And voila! You have a REPL in Sublime!

Sublime Hax: How to Install Plugins for Sublime Text

Getting Started

First things first, if you don’t have sublime text editor, download Sublime 2 or Sublime 3Note: Sublime 3 is currently in beta. Next, we need to install Package Control for Sublime. This will allow you install plugins for Sublime (such as SublimeREPL). In your Sublime Text Editor, hit either Control + ` or go to View > Show Console and you will the “command line” interface for Sublime.

Screen Shot 2015-03-25 at 10.42.08 AM

In that interface, Copy & Paste the following code:

Sublime 2

import urllib2,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler()) ); by = urllib2.urlopen( '' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); open( os.path.join( ipp, pf), 'wb' ).write(by) if dh == h else None; print('Error validating download (got %s instead of %s), please try manual install' % (dh, h) if dh != h else 'Please restart Sublime Text to finish installation')

Sublime 3

import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( '' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

After you have installed Package Control, you need to restart Sublime.

And you are done! Easy right?