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.







Git : Quick Reference

Get Started

To get started with git read the first two chapter in git documentation. I think after the first two chapters it would become useless to  keep reading, instead just keep learning as you go. Use git help <command> to learn more about a command or just google. If you still want to keep reading look at the reference URLs at the end.

To create a local git repository read git-basics. You can use any remote computer as a server very easily with git. Just create a git repository and then clone it from another computer using
git clone <username>@<IP>:<path/to/.git>
Git will use ssh to login and copy the files so no extra setup is needed. The instructions at  git-basics show how to create a git repository without a name, but if you want to give a name to you git repository i.e. instead of just a .git folder you want myrepo.git you can do :
mkdir myrepo.git 
cd myrepo.git 
git init --bare
Add remote repositories with
git remote add [shortname] [url]

Configurations

  • color your diffs
git config --global color.ui auto
  • more configuration options git-scm

Working with git

Working with Remotes

Use the following command to create and track remote branches (ref)
git checkout --track -b <local branch> <remote>/<tracked branch>
OR
git checkout -t <remote>/<tracked branch>
The second command keeps the same name as the remote tracked branch. To avoid the default merging with a remote branch with git pull  use git fetch 
git fetch <remote>OR
git fetch --all
to fetch from all remotes. You can then checkout a branch and fast forward using
git merge --ff-only <remote>/branch

Reverting to old commits

Look at the reset command.
git checkout <branch>~n checkout the commit n commits before the latest commit
git checkout <commit> <file1> <file2>...  checkout files form commit
Note: <commit> can be a branch name or the checksum of any commit.

Looking at the commit logs


Look at diff and status.
git diff <branch old/commit checksum>:[<file>] <branch new/commit checksum>:[<file>]
to see the difference in the files. To see commit log with only the names of the changed files do
git log --name-only
Useful : git log -p -n shows the changes introduced in the last n commits. Use gitk --all to see everything in a gui.


Branching

Show all branches
git branch -a
Create branch on the current state
git branch <name>

Git Stash

Use git stash to quickly save your current state without messing up your commit history
git stash save "<message>"
You can also do multiple layers of stashes. So list all the stashes you have done using

git stash list
stash@{0}: On shoulda: Updating instructions
stash@{1}: On master: started merge but need to fix #104 first
stash@{2}: On feature1: Adding some stuff

Pop the stash from your commit and get back to the state before "git stash save".
git stash pop/apply
A note with this command, it deletes that stash for good, while "git stash apply" does not. You can manually delete stashes with:
git stash drop <id>

and delete stashes with
git stash drop <id>
or delete all of the stored stashes with:
git stash clear
URLs :
a. http://gitready.com/beginner/2009/03/13/smartly-save-stashes.html
b. http://gitready.com/beginner/2009/01/10/stashing-your-changes.html

Renaming branches

Refer to here

Now to rename the master branch you have first change the default head to something else on the remote. You cannot do this from the client. First ssh onto your server and do 

git symbolic-ref HEAD refs/heads/new_master

Now, you can do as you want.

Frequently Used Commands 

  • git add    add files to git tracking
  • git commit   commit the files to the local git
  • git push    update the remote with the current branch
  • git pull   fetch and merge current branch from remote origin
  • git fetch    fetches named heads or tags from one or more other repositories, along with the objects necessary to complete them.
    • git fetch --all   fetch from all remotes
  • git merge  incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch.
    • use "git merge --ff-only <remote>/branch" to fetch and merge from remote 
  • git branch <name>  creates a branch at the current checkout
  • git log  show commit logs for the current branch
  • git checkout <file OR branch OR branch:file> Checkout files and branches

Reference URLs

http://git-scm.com/doc
http://gitready.com