Category Archives: Nerd Up

Only the nerdiest, geekiest shit goes here. Don’t judge me, monkey.

Auto-indent + Paste = Murder

I use Vim pretty much exclusively as my IDE for all programming tasks be it Python, HTML, JavaScript… whatever. One of the Nifty Features™ of vim is auto-indentation which saves a lot of extra tab keystrokes in the longrun. The side effect of this Nifty Feature™ is that when you go to paste code from another source into a file you’re editing, it goes berserk and indents everything you just pasted. If the code you’re pasting is already indented, you end up with nested double indentation, and with languages like Python where whitespace is significant this can be a huge pain in the ass.

So I finally had enough and decided to share this solution along. This is using Vim’s pastetoggle, which allows you to map a keystroke to toggle auto-indentation when you want to paste stuff. Put this into your .vimrc:

" map <F3> to toggle paste to prevent auto-indent mangling when pasting
noremap <F3> :set invpaste paste?<CR>
set pastetoggle=<F3>
set showmode

To use it, you hit F3 to toggle paste mode, paste what you need to paste, then hit F3 again to turn it back off. And done!

Logical Meltdown

I have been totally unable to accomplish any coding this week.  I’ve been rewriting this application at work and while I’ve made some decent progress, I haven’t gotten a damn thing done this week and it’s frustrating as hell!

Of course now that it’s Friday and I’m feeling the urge to start writing more code having to do with dongs.  What the fuck is wrong with me?  Don’t answer that.

I figured that by explaining some of what I’m struggling with, some magic will happen.  I need to blog more and work on my “explaining complex shit in a meaningful way” skills, so if this in any way interests you please read on.


The application I am rewriting is a MySQL database-driven app that has two interfaces:

  1. A command-line interface (CLI) utility written in Perl for direct manipulation (addition/removal of records) and querying (searching) of the database. Only engineers have access to this tool.
  2. A web application written in PHP for searching the database for non-engineers.  Mostly support and operations staff use this. This also provides a hacked up “API” that returns a list of comma-separated values (CSV) of matching records.
It’s something I wrote years ago using PHP & MySQL that is way more complex than I remember it and double-dog dare you more complex than it needed to be. It’s strange going back in time with my code, especially PHP (shame), and realizing how competent I was even then, but how unnecessarily convoluted I made some things.

There are a shit-ton of logical pitfalls in that code that have really thrown me off.  Being that we have a Perl CLI tool and a PHP web app, the much of the code is duplicated for each language. The tool was originally written in Perl with the web app piece tacked on as an afterthought.  At the time I was really into PHP’s native support for MySQL but was pretty much a novice so ended up making some downright lazy and bad choices.

With PHP and it’s bloated cousin Perl, there are so many ways to do the same thing that there really isn’t one right way to do things.  Typically that means there are that many more wrong ways to do them.  For example, horrible crap like this where we have PHP making a system() call to execute a Perl script from within index.php:

    <tr valign="top">
          system('perl column="' . $POST['column'] . '" value="' . $POST['value'] . '" netmask="' . $_POST['method'] . '"');
Couple that with my prior lack of regimented programming style because I originally learned to code from the back of a Cracker Jack box (aka the internet), and what I am working with is pretty much a nightmare.

“Why is that?”, you ask?  Well let me tell you!

First things first, the primary users of the app (aka the stakeholders) have been requesting some new features for a long time and with everything else going on it hasn’t really been a high priority for me.  Especially because it was written in Perl, which I have grown to hate passionately since I started using Python.

Next, the systems from which the CLI tool runs are being replaced, which means the app has to be moved to the new systems. We run a Red Hat offshoot distribution of Linux, so we use RPMs to manage what gets installed on machines.  What this means is that any Perl module dependencies need to be installed via RPM. With RPM and Perl there are always dependencies of your dependencies, and you can bet that some of those won’t be available in the repository. This opens a massive can of worms of having to download the module source and convert it to RPM.  For those of you who are familiar with Perl’s CPAN and RPM, you’re probably thinking “Just use cpan2prm!”. Right, if only it were that simple…

You see, the new hosts are running a 64-bit kernel, which means that the subset of available packages is even smaller because packages have to be specifically built (or rebuilt) for 64-bit installation. Even if it’s the same files inside of the package, it still has to be re-packaged for 64-bit.

So, I was left with the choice of either uprooting the tool from its current location, wrangling package dependencies, rebuilding packages, modifying the Perl code to shoehorn it into the new systems all the while overlooking the implementation of the requested features; or rewriting it using Python & Django.

Naturally, I chose to rewrite it because the thought of having to trudge through antiquated Perl hackery gave me diarrhea and I really don’t want to ever touch Perl or PHP again if I can help it. By rewriting the app in Python, I can implement the new features and bring the code into the present at the same time without having to go through bouts contemplating suicide. Annnddd… That leads us back to the nightmare that is Perl and PHP.

As I mentioned above, with Perl & PHP there are so many ways to do things that most of them are convoluted, confusing, hard-to-read, and just plain fucking wrong. With Python–and Django for that matter–there is usually only one right way to do things, and that way is pretty much always elegant.  Plus, one of the most beautiful things about Django is that it wholly and completely abstracts the database interaction from the source code.  That means not having to write complex SQL statements manually, and the less SQL I have to write, the better.

In meeting with the stakeholders on rewriting the application, we decided that all of the energy should be invested in replacing the web app with a real API with a RESTful interface. If you don’t know the basic premise of REST (which is a methodology, not a protocol), it’s that URLs should be easy-to-read, self-describing and beautiful. Beautiful URLs? WTF? How can a URL be beautiful?  I’m glad you asked!

The old PHP version of the API is very heavy on the query string, requiring GET variables to be passed in the URL itself which is not only cumbersome, but can get confusing and hard to read very quickly:

Compare that to a RESTful version of the same:

This is where Django excels because it encourages pretty URLs by making it easier to use pretty URLs than not to.

So, cutting this post off at the knees, I’ve been pounding on this for the past few weeks, learning a lot about how NOT to do things from my old code and how TO do things using Django as my guide.  I made a lot of progress, got the API working, and a basic search interface that returns stuff all using the same code.

But this week, I’ve been stuck.  I haven’t made any progress, have been getting really confused, and just generally bashing my skull into the keyboard.  I think I’m just fried from trudging through all of Perl’s curly braces, semicolons, ampersands, dollar signs… OMG THE SYNTAX!  KILL ME NOW!!

Besides, it’s Fried-day anyways, so I hope that just by spelling this all out here that I’ll be able to dig back into the code after the weekend with a renewed perspective.

p.s. Fuck Perl.

p.p.s. Fuck PHP too, but wrap that rascal. She gets around!

Using Caps Lock on Your iPhone

I’ve had an iPhone for over a year and still hadn’t learned how to use the mysterious caps lock feature.  I enabled it in the settings, but had no idea how the hell to make use of it!  Of course it turns out to be just as intuitive as everything else on the iPhone. I just figured this out today so I thought I would share it.

Step 1:  Enable Caps Lock

Navigate to Settings > General > Keyboard and switch “Enable Caps Lock” to “ON”.

Enable Caps Lock

Enable Caps Lock

Step 2: Use Caps Lock

To use caps lock, all you have to do is double tap the shift key on the lower left of your keyboard.The key will turn blue indicating that you have successfully engaged CAPS LOCK.



To turn it off, just tap the shift key once more.

Your life has now been enriched, and I can move on with mine.

San Diego Comic Con 2009: Brain Dump

So I just spent the last four days of my life at the 2009 San Diego Comic Con. It was the third year in a row I have been to SDCC and the first year that I went for more than just one day.  It was both too much and not enough at the same time.

I found myself wanting to live more of the professional experience of talking to artists, getting books signed, hunting down exclusive items and generally just living of all the action that makes it so awesome.  I met some interesting people, and was surprised that many of the most popular comics out there just don’t interest me.  I guess I have always kind of been like that.  I’ve never really been IN to comics in the way that most comics fans are, or at least how I perceive them to be.

I feel like most of the plots and stories I are cliche and most of the art is just too absurd or derivative.  It’s really hard to explain without sounding like I’m a pretentious dickwad.  Fact is I’m not a fanboy, I’m just a fan.  I only buy into comics when I can get in on the ground floor and feel like I’m truly experiencing a story from the beginning.  For the most part that leaves me out of pretty much every mainstream franchise and I’m totally okay with that.

So rolling with that, I’m going to list my favorite things from SDCC 2009, only the first of which is actually a comic:

  1. Kick-Ass: I recently discovered Kick-Ass in a local comic store.  As I mentioned above, I only like getting into stories from the beginning. Issue #6 was the first one I bought, which has a little girl covered in blood holding two swords.  I was intrigued.  I bought issues 1-4, and 6.  They were out of #5. I went to SDCC with the mission of finding #5.  Turns out, they are making a Kick-Ass movie and held a panel for it at SDCC!  I missed the panel, but did make it home with #5.  Mission accomplished.
  2. Avatar: James Cameron’s first film in 15 years?  I’m there.  I didn’t see the panel nor the exclusive trailer, but I heard lots and lots of chatter all weekend about the concept of the avatars.  There was also an Avatar booth with a massive 20-foot mech suit (wicked!) from the film and prototype toys in a diorama case.  I hate it when you can’t play with the toys!
  3. District-9: I attended the panel for this on Friday. Peter Jackson himself led the panel and spoke candidly on the birth of the project. It was fascinating to learn that as soon as the plug was pulled on the Halo movie, they immediately focused all of that creative energy into their own project which became District-9.  Considering it was made on such a low budget ($30M USD), you would never know having seen the 7-minute teaser we got to see.  I popped at least 17 boners, while watching it.  And we got to see it twice.  So that’s what… 34 boners minimum?
  4. Iron Man 2: Aww yeah,  Scarlett Johansson as Black Widow?  17 more boners.  Not seen:  Gwyneth.  Who cares?
  5. Chessex Booth: Dice!  Role-playing supplies!  I was at this booth for like half an hour drooling over the absurdly vast selection of dice.  I just love dice!! Of course I bought some.  I could feel my girlfriend judging me all the way from LA, but it wasn’t enough to stop me.
  6. Half off all trades! Got some great last-minute deals on some books I’ve been wanting for a long time.  Like Wanted for $10!
  7. Half-ass costumes. Low point equates to high point.  I laughed a lot at the expense of others.  A LOT.  Come on people.  If you’re going to dress as G.I. Joe, go the distance.  Slapping on some camo pants and a toy gun is not half the battle.
This was the biggest SDCC to date.  Try to imagine 125,000 geeks, freaks, nerds, dweebs, artists, impersonators, celebrities, executives, and a handful of ingrates descending on San Diego like the plague.  Make no mistake: it was C R O W D E D.  You couldn’t swing a dead cat without hitting a fat nerd dressed as a Klingon.

Ok, I’m exaggerating… a little.  I didn’t see a single Klingon this year!  The theme was definitely Steampunk, which I found odd because there is nothing coming out that directly inspires such a theme.  I guess that’s just the in thing right now amongst the nerdy elite.  It’s like Back to the Future 3 meets Final Fantasy, if you can picture that.  Or how about people in trenchcoats with goggles, rapiers, army boots, and… laser guns!  pew pew pew

Being that it was so crowded there was a lot of pondering about how “it can’t go on like this”.  The average wait was like two hours for the big panels and no less than an hour for everything else.  There was a rumor floating around that this could be the last year of SDCC in San Diego as we know it.  The rumor was that it could be moving to Los Angeles or (God forbid) Las Vegas if the San Diego Convention Center and possibly even San Diego at large can’t meet the growing demands of the con.  Something about the contract being up for negotiations, blah blah blah, etc etc.

All I gotta say is it just wouldn’t be the same SDCC without the SD in it. I wouldn’t mind it being a little closer to home in LA, but downtown LA is fucked enough as it is without 125,000+ nerds descending upon the place for five days in the dead of summer.  Yeesh.  I hope the folks in San Diego don’t let the convention walk.  That would be bad stuff.

I had a great time and was sad to leave on Sunday.  Despite how crowded it was I want to go for the entire time next year, instead of only Friday – Sunday.  That is, if it’s still there… dunt dunt dunnnn