Monday, February 13, 2012

My secret weapon... Python

I came across this blog post the other day "Why Python is important for you" . It felt like he read my mind. I was going to have a similar post explaining why I believe everyone should learn at least basic Python coding. But since he wrote it up so well I let you just read it "why" and instead I tell you my love affair with Python and why I it is my secret weapon.

I use Python for my work every day. I write all the backend software for clients iOS apps using the Django framework. Also use Python extensively to talk to 3rd party APIs and collect/crunch data. Automate a lot of things that the software needs to do without user interaction. It is ridicules how simple to update a bunch of file, replace code in them. And python has a Giant standard library you can use and it pretty mush covers everything, or as they say Batteries Included.:) But if you still not convinced look at all the places python used, also MIT switched to Python from Scheme.

My love for Python started back in 2003 although I started to use it in 2002 to parse log files. Switching from Perl to Python was an awesome experience. We got our first digital camera and taking a lots of picture. But when downloaded to the computer we had a couple hundred images with IMG_00433 etc.. My wife organized them into folders using the thumbnail view but it was still tedious. So to make my life easier by making hers easier:) I opened up python prompt and started to hack out a quick renaming script. She loved it. now it took no time at all to rename a bunch of images and she could even provide the prefix "birthday_". Then she was mad since some of the images needed rotating, no problem there is a battery for that, lets get PIL add an other function, an other command line question if she wants auto rotate. Done. It was so painless and fast to implement that for awhile I was certain that everyone wrote a script like that just like everyone wrote at one point "Hello World".
Well it wasn't the case and there was a lot of happy people around me using this simple tool.
That's when Python became my secret weapon, and it is ever since. 
Well, not so secret anymore...

If you haven't touched python yet go and open up your terminal if you are on a Mac or Linux and type in at the console$ python and start playing. On windows just download the installer and you are a few clicks away.
Python uses white-space indentation instead of curly braces which makes the code beautiful and reading it a pleasure.

Friday, February 10, 2012

My new texteditor Sublime2, Essential packages and Pro tips

This week I read Quick Review of Mac OS X Text Editors for Lua...
In there I saw Sublime2 and since I have never heard of it I went to check it out. After reading the feature list and looking at some screenshots I just knew I have to give it a test drive. After download and install I opened up one of my django models.py file. Immediately recognized the syntax and presented me with a very nice syntax highlighting. To my biggest surprise even auto complete worked. After reading up some of the docs and a couple of blog posts I learned that I can do pretty much anything without my hand ever have to leave the keyboard. I am a longtime vim user (although vim was not my choice of texteditor in day to day programming), and it felt good being able to open files, jump to any function/method and even install new packages without using the mouse. At this point I closed my eclipse and I knew it was not a temporary move. I had tried many texteditors/ides over the years but I always ended up going back to eclipse. Eclipse is a very convenient ide with all the language support I needed, but it is also a resource hug and many times it overkill what I am using it for. Sublime2 is a great compromise with it's functionality and simplicity.

Essential packages
Below packages in my opinion needs to be installed after a fresh new sublime install.
  • Sublime Package Control
  • Soda Theme
  • Bracket Highlighter
  • Prefixr Plugin
  • Djanerio
  • Sublime Linter
  • Sublime CodeIntel
I started with the Sublime Package Control package because once that's installed you can use it to install the rest.:)
  1. Open up Sublime and press Ctrl+` that will open up the console
  2. Paste this into the console:
    import urllib2,os; 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())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'
  3. Press enter. It will install the Package Control for Sublime.
  4. Now restart Sublime and you are ready
Now lets install those packages. All you need to do is Press Cmd+Shift+P. This will open the Command Window, type package to sort to the Package commands. Select Install Package and it will bring up the package search window, there just start typing the name of the package and press enter to install. That's all, it just does not get any easier than that.

Update:
To enable the Soda Theme open preferences and user settings. Add this into it

"soda_classic_tabs": true

Pro tips
Here are some of the commands I learned and learned to love spending less than a day with Sublime2. I will keep this list update as I learn more of them.

  • Cmd+Shift+P => Brings up the Command window, you can access pretty much everything here.
  • Cmd+P => Open anything window. My FAVORITE feature, you can just start typing and it will display all the files that insudes the typed in text, then press enter and open it. Seriously this saves a ton of time when you know what file you want to open and it is burried many level deep in directories.
  • Cmd+R => Find any function/method/class window. This is an other huge time saver. Once you typed in text it will display all possible choices, Use the arrow key to scroll through it and it also moves the cursor immediately at the location. Got to love it!
  • Type lorem then press Tab => Inserts a full paragraph of lorem ispum text. I am pretty sure everyone uses it to fill up empty space to represent content.
As you can see I jumped into this love affair head first. So far very impressed with Sublime2 and at this ppint I see no reason why I would need to go back to eclipse, unless I have to write some code for Android which I am not planning on but who knows...
I am going to write up an other post on basic configuration and how to create Projects with Sublime2 and switch between them using your keyboard only.

Monday, February 6, 2012

Syntax highlighter for my blog posts

Adding a syntax highlighter to a blog is so much simpler than use to be. Since I am planning on posting a lot of code on this blog I wanted to make sure I have this in place as soon as possible. Fortunately this is as simple as adding css and js to the Blogger template and wrap the code around with <pre class="prettyprint"> tag. After testing a few of them I ended up using Google Prettify. I like how simple it is to integrate it and it auto picks up most of the languages, so I don't have to reference it. Yes I know I am lazy, but lazy programmer is good.:)
So here is the first test:
$(document).ready(function() {
   var syntaxHighlighting = 'Super Simple';
});

And a second one:
class Test(object):
    def __init__(self):
        self.test = True

And a third one:
- (void)sayIt
{
    NSString *str1 = @"Syntax highlight";
    NSString *str2 = @"Super Simple";
    NSLog(@"%@ %@", str1, str2);
}

I chose the desert style even though my IDEs have light background, but it helps to make it standout in a post.