# 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!

 # Floating Point Division 231871064940156750 / 5 # => 4.637421298803135e+16 # Turn the result into a integer int(4.637421298803135e+16) # => 46374212988031352 # Floor Division 231871064940156750 // 5 # => 46374212988031350

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

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)

Once connected, check that the USB is being recognized:

• In the Hardware Section, you should see something like this

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:

 screen /dev/cu.usbserial 115200

view raw
rpusb.sh
hosted with ❤ by GitHub

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:

*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.

 # This is what your probably have "linter-rubocop": command: "/Users/sungkim/.rvm/gems/ruby-2.2.3/bin/rubocop" # This is what you should have # Notice that the bin was changed to wrapper "linter-rubocop": command: "/Users/sungkim/.rvm/gems/ruby-2.2.3/wrappers/rubocop"

view raw
rubocoprvm.cson
hosted with ❤ by GitHub

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

# Terminal Hax: Showing Hidden .FILES in Mac

By default, .FILE_OR_FOLDER_NAME files or folders are hidden. To see them in Finder, open up Terminal and enter the following.

 # The first line will get all hidden folders and Files to be shown # The second line will close and reopen Finder windows so the changes will # take effect. defaults write com.apple.finder AppleShowAllFiles YES killall Finder # To hide the files/folders once more defaults write com.apple.finder AppleShowAllFiles NO killall Finder

view raw
hidden_files.sh
hosted with ❤ by GitHub

# 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

 # Type this into your terminal. You will notice that we are actually using Ruby to install Homebrew. ruby -e

view raw
homebrew.sh
hosted with ❤ by GitHub

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

 # Into your temrminal type which ruby # This will give you the path to where ruby is installed if installed ruby -v # This will give you the version of ruby you currently have installed # You can check just about any other program in the same fashion which git git -v

view raw
check_ruby_git.sh
hosted with ❤ by GitHub

Step 6: Install Sublime Text
Pretty straight forward. Sublime is a text editor. This where we will edit code.

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
If

AFTER

# 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

 //////////////////// //JavaScript Logic// //////////////////// \$('.project_btn').each(function(index) { \$(this).on('click', function(){ \$(this).addClass('selected') \$('.project_btn').not(this).removeClass('selected') var tab = (\$('.project.info').eq(index)) var otherTabs = (\$('.project.info').not(tab)) console.log(index) if (otherTabs.hasClass('active')){ otherTabs.removeClass('active'); otherTabs.addClass('vis_hidden'); setTimeout(function(){ otherTabs.addClass('hidden'); },20) } if (tab.hasClass('hidden')){ tab.removeClass('hidden'); setTimeout(function(){ tab.removeClass('vis_hidden') tab.addClass('active') }, 50) } //Closes active tab else{ // tab.addClass('vis_hidden'); // tab.removeClass('active'); // tab.one('transitionend',function(event){ // tab.addClass('hidden'); // }); } }); });

view raw
displayNone_Hack.js
hosted with ❤ by GitHub

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

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.

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.

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)

Step 4
Type `"E501"` so the JSON files looks like this:

Step 5
Save the JSON file and you should see the error disappear!

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:

See all the errors disappear! Beautiful.

# 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.
`pip install "ipython[notebook]"`
` "/usr/local/bin/python", "-u", "\${packages}/SublimeREPL/config/Python/ipy_repl.py" `