Tuesday, March 20, 2012

Gister - gists at your fingertips

So long time no post... been busy with a lot of real life stuff.

Introducing: Gister

Luckily I got some time to play again on Sunday night - and the result is a script called gister

gister can save, cat arbitrary files from gists, or execute them if they have #! at the top.

Why? I hear you asking silently to yourself. Well, the inspiration behind this was as follows: I was busy writing a script to execute a command for each commit in the history of a git repository. This resulted in the following script/gist: git-foreach.sh

Simple enough. This gist clones the repository, goes through EVERY commit in the repo (for all branches), and then executes a script that was passed in as an argument for each of the commits. So this resulted in yet another script, stored as a gist on github.

Cool, that's handy. Now combining the two scripts we get a nice newline seperated list of the number of files for each commit. Now let's say we want to plot this. Lets use gnuplot. But everytime I use gnuplot, I need to go re-study the syntax of the plot files for gnuplot. And everytime I do that, I end up with basically the same script that I've written 70 times in the past. So I decided, screw it. I need a better plan.

So that's where gister comes in. The idea behind this is to store all these handy scripts that you write, as gists on your github account, and then run them from the internet, right in your shell. So no need to download anything, or to put something in your PATH everytime you want to run a small script. You just install gister once, and you can cat/run/store any public gist on github. This also works for OTHER peole's gists as well, of course. So we can all sharea nd publish our neat little scripts online for people to use.

So go to and install gister. Download the single .sh file, give it +x permissions and (optionally) put it on your path, and start playing with it. I suggest creating a $HOME/bin directory, placing gister there, and then putting it on your path in your .bashrc . This will probably be where gister installs binaries one day should it get that functionality.

Here are some things you can do with it:

Get a response from the magic 8ball:

Get a gnuplot of the number of lines over time in any git repository (you need to chdir to the root of the git repo):

Play a simple maze game in your terminal:

It also supports passing arguments to the online gist, but appending a "-" followed by the arb arguments:

So there you have it. Play with a bit. It would be awesome if you guys could find any other interesting things you can run with gister on github. Remember the limitations though. The Gist MUST have a filename, and multi-file gists will not work at the moment. Of course, if you want to help out, here is what's needed in order of importance:

  • 'Anonymous' gists - ie. gists with no filename, just an id.
  • Multi-file gists

Those are all immediate featues, adding value which will be valuable later regardless. Now, what about the future? I'm thinking along this following lines:

  • Ability to install the gists locally, in $HOME/.gister_bin on something. Which can then be on your path.
  • Ability to track versions, sort of like a package manager for shell scripts. So gister -u runs through all installed gists and updates them
  • Ability to track not only shell scripts, but arbitrary documents. This needs to work both ways. I want to use gists to store notes, for example. So I would have a gist for all my notes, and install/track it with gister. Whenever I update a file, I use gister to upload all the changes I made.

So play with it a bit, let me know what you think. This was only tested on zsh. Bash should work. Other shells may not work.

Thursday, March 15, 2012

Knockout.js

I am no HTML, CSS or JavaScript expert, but I have seen some nice JavaScript libraries the past few weeks. Nico introduced us to node.js and I am sure everybody is familiar with jquery. Kaveen also mentioned HighCharts. Yesterday Marlo introduced me to Knockout.js which helps to "simplify dynamic JavaScript UIs by applying the Model-View-ViewModel pattern". I am not going to try and show you some code examples, because I think the Knockout.js developers have done a very good job themselves. Just head on over to http://learn.knockoutjs.com and play around with their "interactive coding exercises". Let us know what you think about the Model-View-ViewModel pattern..?

Friday, March 2, 2012

Find the bug - part two.

Nico, a wonderful idea! Here's my contribution:

The most complicated thing in the world to understand is concurrency. Not even Einstein's General Theory of Relativity comes close. Here is an interesting gem that I got from a newsletter quite some time ago. I found the puzzle incredibly interesting and it convinced me to NEVER EVER EVER EVER:
  1. Assume I understand concurrency.
  2. Use the old core Threading stuff rather than the new modern concurrency frameworks (i.e. Threading pools, etc.).
  3. Assume I understand concurrency.
  4. Skip testing threaded applications - because I'm clever, it makes sense... and besides I understand concurrency.
  5. Assume I understand concurrency.
Oh, I almost forgot, the most important rule is to never assume I understand concurrency (not even at a job interview at google)!

Look at the following Rorotika-inspired class and tell me what happens. Keep careful track of all the delays.

OSGi and your strong opinions

Most of you should know by know that I have started a new job yesterday. So far the Development Manager has pointed me towards the following educational slide shows of IBM:

OSGi Feature Overview:
http://publib.boulder.ibm.com/infocenter/ieduasst/v1r1m0/index.jsp?topic=/com.ibm.iea.wasfposgi/wasfposgi/1.0.0.0/Overview/wasosgijpafep_OSGi_overview/player.html

Modular and dynamic OSGi Applications Part 1: Motivations and specifications:
http://publib.boulder.ibm.com/infocenter/ieduasst/v1r1m0/index.jsp?topic=/com.ibm.iea.was_v8/was/8.0/ProgramingModel/WASV8_OSGi_part1/player.html

Modular and dynamic OSGi Applications Part 2: OSGi applications support in WebSphere Application Server:
http://publib.boulder.ibm.com/infocenter/ieduasst/v1r1m0/index.jsp?topic=/com.ibm.iea.was_v8/was/8.0/ProgramingModel/WASV8_OSGi_part2/player.html

OSGi is the "Open Services Gateway initiative framework" which is a "module system and service platform for the Java programming language that implements a complete and dynamic component model". Thank you Wikipedia.

I am still working through these. So far I like the fact that OSGi is a way of enforcing modular architecture and deployment as well as the fact that it is a solution to the multiple versions of the same class on the CLASSPATH problem. What are your strong opinions on OSGi and/or alternatives?

Meta-post

I enjoy looking at the statistics of our blog. Particularly those of the "traffic sources" and the "audience". These respectively show which sites refer to our blog and where people are located that visit our blog. We suddenly had a spike in the number of visitors from Russia. Unfortunately we cannot see which post(s) caused the traffic from these sources. Here are some screen shots for the statistics of the past week:

Audience

Traffic Sources

Thursday, March 1, 2012

Can you find the bug? Part One

With this year being a leap year and all, I've been looking at a couple of famous leap year (and general date) blunders tonight. It's quite embarassing how often we as programmers get date arithmetic wrong. I've read about a couple tonight, not the least being a bug in old apache commons-net which made files created on 29 Feb dissapear from FTP file listings!
Anyway, if I'm honest, I'm sure there is code out there that I've written that could also potentially have leap year problems. You need to be wary when working with dates. For example, doing something like:
    Date lastYear = new Date(today.getYear() - 1, today.getMonth(), today.getDay())
for working out a date a year back could be potentially problematic. So we should just be always wary of date arithmetic when writing such code.
In any case, this post has got another point: I want to start a sort of "game" or "tradition" on the blog if you will. I want to call it: "Can you find the bug?". The idea being, that whenever we come across a piece of code with a subtle, or huge, or blaringly obvious bug in our daily lives, we share it, and "challenge" the others to see if they can find it. Not with the idea of mocking the code or the author, but with the point of learning. And also testing our bug-catching eyes. Because there's no better way to learn than to analyse code that is actually running in the wild. Kind of like our own DailyWTF - but a bit more focused, and perhaps relevant.
Now we shouldn't be shy, it could be code we wrote ourselves (I know I've had a couple epic ones!).
Now today, I've got a doozy for you guys. I'm not going to give you the full story just yet... but let's just say this code comes from a VERY prominent IT company, and this was code that actually shipped in a product that this company delivered and was used by millions of people all over the world. This piece of code also made the product in question completely and utterly useless on a certain day. I want you guys to find out what the bug, and that day is. Bonus points if you can tell me the company/product in question. This bug is not limited to a certain, specific, once off day - it will happen on any day that fits a certain pattern.
The original code was C, and for the purposes of this excercise I took the relevant part and transcribed it into python. Here it is:

I've verified that the general case of the bug exists by running it myself. You can run the script with "python ". The point of the code was apparently to find out the year,month,day that a given value representing "days since 1980" falls in. I've just transcribed the relevant parts. Hope I didn't screw it up!
So, see if you can spot the bug. Additionally, what would you do to fix it?
Hope you guys enjoy! And if this works out to be a fun exercise we'll do it again!

The Good Saltmine Slave

An article about characteristics of good employees was circulated where I work and I couldn't help myself from reading it with the context of [database] saltmine slavery in mind. Here is a quick interpretation:

1. You ignore job descriptions - Willing to go into the deepest shafts to get the salt even when assigned a nice surface patch in the shade

2. You're eccentric - You rile up your co-slaves with your humour and positive working attitude even at the risk of angering the slave drivers - happy slaves == more salt and happy masters

3. But you know when to dial it back - when the slave driver cracks his whip, you work shut your piehole and work as if your life depends on it

4. You praise openly - You pat your slave friends on the back if they've gathered acceptable amounts of salt and tell them that the masters appreciate their effort

5. You complain in private - If some slave pisses you off, go beat him up somewhere deep in the mine - you don't want to start a riot and stop the others from gathering the precious minerals

6. Speak up when others are intimidated - When you haven't had food for 3 days(not less), speak up - if you die of hunger your bosses will get less salt and have to get new slaves

7. Like proving others wrong - that nice pocket of salt in the dangerous underwater cavern CAN be mined.  You do not just want the master satisfied - you want him to smile.  Possibly, other motivations can also play a role, for example stupidity.

8. Always fiddling - You always try to find ways to gather more salt, unselfishly ignoring the fact that you'll spend the rest of your (short) life in the hole you're digging to get it

Of course the article was not referring to slaves hundreds of years ago, but I had fun thinking this up.  Feel free to suggest improvements to my interpretations!

One last thing: Any parallels you might see to your own life or to people you know's lives is unintended.

Disqus is pissing me off

Bwhahahaha

For some reason, my personal account seems to be linked to the blog's disqus account. I just had a "whale" of a time (see what I did there) to try to sort out this confusion - and I've come to the conclusion that it's gonna be impossible.

I think what happened was that I was logged into the blog disqus admin interface when I logged into my google account, and now it seems my google account is forever linked to whaleventures disqus account.

Also, a comment from Dawid was logged as spam: http://whaleventures.blogspot.com/2012/02/managing-callback-spaghetti-in-nodejs.html#comment-452433000

I cannot see any reason why in the world that comment should be marked as spam.

So the point is, if any of you moderate using the whaleventures account, be sure to use an incognito window or even another browser or something. And, disqus just lost some major points in my eyes. Also, the fact that Wiehann had to find a work-around for notifications didn't make it start off on a lot of points either. Lets give it another week or so, but perhaps its prudent to start thinking of ways to migrate to another commenting system?