Saturday, August 23, 2014

Macbook, Sublime Text and IPython : the best combo for a coding project

I recently switched to a mac so I may be a a bit over-zealous, but my macbook is the first computer that I like to work on. It doesn't get stuck, has extensive command line features, is highly customizable, and has a great retina display.

In the past whenever I got a new computer the enthusiasm lasted not more than a week. It may be that I have discovered the right set of tools to work with, this time, but I never really had so much ease in coding in a language other than MATLAB and it's not that I haven't done projects in other programming languages. The combination of IPython's great debugging features, OS X's superb command line iterm and Sublime Text's easy text manipulation and great plugin's has made my life easier by a great deal.

So I'll try to make a quick reference for my future self who has a tendency to forget things quite a lot.

OS X tools 

Iterm :

  • You can divide the terminal into mutiple windows like terminator. Use Cmd+d and Cmd+Shift+d to divide the current tab.
  • You can re-assign keys to move quickly on a line  : link
I have re-assigned the left option key to Esc from Preferences > Profiles > Keys > Left Option Key Acts as. Now, in addition to the usual bash line navigation Ctrl+a, Ctrl+e, etc. , I can move one word left and right using Option+f and Option+b respectively. I can also delete word by word forward and backwards using Option+d and option+delete without trying to stretching my hand across the keyboard.

Caffeinate:
With windows, I was extremely irritated when the following happened. You are just about to head out but you suddenly remember you haven't checked your email. You open up your laptop to quickly find out your venue (because you are too lazy to remember everything), so that you can catch the bus arriving in two minutes outside your apartment. But Windows decides that there's a dangerous virus that you cannot be not-protected from and you have to wait for an hour for the software updates to finish before you can check your email.

Thankfully this doesn't happen with my macbook, because in addition to not being extremely irritating, it's battery lasts long enough  (atleast for now) for you to check your email and not have to look for a power socket, which is partly because OS X has a very aggressive power save mode. Which is fine until you are want to connect your laptop to your TV and watch a TV show without having to get up and move the mouse every few minutes.

caffeinate <command> is helpful in this scenario.

iBooks and the default OS X dictionary:
I like to have a dictionary around whenever I'm reading anything, which is too much effort for when you are reading a soft copy.


Menu Search:
Looking through the online documentation of a software to find out exactly where the option you want to set resides, is too much trouble especially considering the fact that it's easier for you to go through Petabytes of data on another computer (google's server farms) than to look at a mere 100s of bytes you have on your computer. That is why the help menu search keyboard shortcut Cmd+Shift+/ is one of my frequently used key strokes.



Sublime Text

There's nothing to document since the text editor's documentation is easy to find and it's easy to configure. Here are the plugins that I have installed in my Sublime Text 3, in decreasing frquency of usage (in addition to the in-built command palette features)-

  • Origami
  • Bracket Highlighter
  • SublimeCodeIntel
  • Text Pastry
  • WhoCalled
  • Advanced New File


IPython debugging 

pdb is a great debugging tool, but one flaw of debugging with these tools is that to set a breakpoint to get to the one case that isn't working you have to go step through the hunderds of cases that are  and it get too tedious. And that's why I preferred to use print statements to debug.

Now, enter the python modules logging. So here's what every piece of code I write now looks like -

try:
function_to_debug()
except Exception:
logging.exception("here")
_breakpoint()  # 0fa1ab74
The  logging module let's me know where exactly the exception was raised and the breakpoint let's me examine the variables values at that point. So I write my code without too much worrying about the extreme cases, and handle them as and when  they arrive.

People have also extended the logging module to print colored output. I'm too lazy to google for that now, but that would be a killer tool for any debugging task.

There's also the %pdb IPython magic feature which is essentially the same thing, except for the logging.exception("here") line above.

Note to self : keep adding rambling and features to this.







No comments:

Post a Comment