Python Hax: True Division vs Floor Division

I have been recently doing an algorithms course via Python 3 and I was doing what I thought was a simple division.

I was trying to do 231871064940156750 / 5. When I did it in Python I would get 
46374212988031352. But it would then fail tests telling me the answer should be 46374212988031350.

At first, I thought maybe it was a bit error and that the number wasn’t being stored correctly. But after reading the docs for int in Python 3, there doesn’t seem to be any limit to int vs long like there was in Python 2.

I then thought maybe converting the result from scientific notation to int was causing a rounding error somewhere along the line.

It turns out that I can fix this problem by dividing with the // operator rather than the / operator. Take a look at the code below!

In Python 3, all division returns as a float, not an integer (unlike Python 2, Ruby 2 or JavaScript).

Pi Hax: Connecting A Raspberry Pi Via Console USB MAC

learn_raspberry_pi_console_cable

In this hax, I will explain how to connect a Raspberry Pi to a computer using a USB cable you can purchase here or here. ($8 – $12)
* I am currently using the one from ADAFruit

You need to connect the 4 cables so that:

  • The Red Cable to Pin 2 (5v)
  • The Black Cable to Pin 6 (Ground)
  • The White Cable to Pin 8 (TXD)
  • The Green Cable to Pin 10 (RXD)

raspberry_pi_circuit_note_fig2a.jpg

Once connected, check that the USB is being recognized:

  • Go to the top left Apple > About This Mac > System Report…
  • In the Hardware Section, you should see something like this

Screen Shot 2015-12-25 at 12.18.22 PM

The vendor for my USB cable is Prolific Tech. It appears that it might not be recognized by Mac OS Yosemite without a driver. If you are having trouble connecting to the Raspberry Pi, try downloading the driver here.

Once its connected, open up terminal and try the following:

It might tell you that is was unable to find the device. Disconnect the pi, reconnect, and then try the screen command again.

Once connected it, you should get something like this:

Screen Shot 2015-12-25 at 12.54.44 PM

The default username and password for the raspberry pi is:

raspberrypi login: pi
password: raspberry

*Note the password will not show when you type it in. Just press enter after you have typed out raspberry.

Thats it! You should be successfully connected to the CLI of the Pi 😀

Atom Hax: Installing Rubocop For Atom

I have recently been trying out Atom text editor and noticed that I couldn’t get Rubocop to work. I kept getting:

Error: env: ruby_executable_hooks: No such file or directory

After some quick searching, the following solution came up on SO. Basically you need to go back to your ~/.atom/config.cson and make the following changes.

 

Fixed everything for me and now it works like a charm!

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.

BEFORE
Screen Shot 2015-06-24 at 2.43.59 PMIf

AFTER
Screen Shot 2015-06-24 at 2.43.38 PM

 

CSS Hax: Doing transitions with Display: None;

I want to preface this post and say I have’t quite figured out the “perfect” solution to this yet but I thought I would share what I have learned so far from my experimentation.

Initially, I thought the problem would be a “quick fix” (but then I am always reminded there isn’t such a thing”). I have tabs in on my personal site that show difference information based on what tab is clicked. I wanted the tabs to transition in rather than just display by adding an active class through css. So naturally, I decided to use CSS transitions via jQuery .addClass. Quick and easy right?

Nope. 😦

It turns out you can’t put a transition on display. So after some stack overflow searching, people recommended that I play around with the opacity and height. First you reduce the opacity to 0 and then you change the height to 0. This does work but then you have a different problem. Even with height 0, you get a tiny bit of white space. This isn’t too much of a problem with one tab and most people would never notice. However when you have more, the white space get progressively larger. However, display none would solve this problem yet you can’t do a transition on it.

My current solution?

Info Tab
.info {
clear: both;
height: auto;
padding: 0 8px;
overflow: hidden;
display: block;
border: 1px solid #EEEEEE;
-webkit-border-radius: 0px 7px 7px 7px;
-moz-border-radius: 0px 7px 7px 7px;
-khtml-border-radius: 0px 7px 7px 7px;
border-radius: 0px 7px 7px 7px;
-webkit-box-shadow: 0px 2px 2px #DDDDDD;
-moz-box-shadow: 0px 2px 2px #DDDDDD;
box-shadow: 0px 2px 2px #DDDDDD;
background: #FFF;
height: auto;
padding: 8px;
}

Display None Class
.hidden{
display: none
}

Opacity 0 Class
.vis_hidden {
opacity: 0;
-webkit-transition: opacity, 0.01s linear;
-moz-transition: opacity, 0.01s linear;
-o-transition: opacity, 0.01s linear;
-ms-transition: opacity, 0.01s linear;
transition: opacity, 0.01s linear;
}

Opacity 1 Class
.active{
opacity: 1;
-webkit-transition: opacity, 0.7s linear .3s;
-moz-transition: opacity, 0.7s linear .3s;
-o-transition: opacity, 0.7s linear .3s;
-ms-transition: opacity, 0.7s linear .3s;
transition: opacity, 0.7s linear .3s;
}

JavaScript Logic

This essentially added delays on Display:None for the divs I want to hide so that it is able to transition first. The only problem I am having now is trying to keep the page from “jumping” for a split second as the other elements on the page fill the space the div once had only to then move back down once the new div is added.

I am thinking that if I can keep a div there to keep the space occupied or add a container div I can solve the problem. Has anyone else encountered this problem? How did you approach it?

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

Bonus!
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/ipy_repl.py"
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!