sethmason.com The internet resource for all things Seth Mason.

jQuery and Friendfeed experimentation

So, I created a little page that’ll dump out the last 30 items from my Friendfeed feed.

jQuery and the Friendfeed API make it incredibly easy. It only took me about 15 minutes to get it working. The rest of the time was making it ugly with my crazy design skills.

If you want to see the page, it’s over on my vanity site if you are curious to check it out.


Google Code Search to the rescue

Mmkay, this tip is probably a gazillion years late but Google Code Search is a great resource for a budding developer. Heck, I’m a relatively seasoned developer and I use it.

It allows you to search public source code using a variety of methods. You can search for an exact string or a regex. You can search certain files, certain packages and certain languages.

For instance, today I was curious how to use the Perl API for writing a Pidgin plugin as their documentation is a tad sparce. A quick search looking for Purple::Find::buddy and I found a whole bunch of examples. You can also use it for silly things, like looking for quotes from "Hitchiker’s Guide To The Galaxy."

It’s official, Google now owns me. sigh


Machine specific startup files in BASH

Here’s a helfpul tip if you use a couple of different machines and need specific things set up on a specific machine.

Just add the following to your .bashrc

## Read Generic RC                                                                            
for rcfile in "$HOME/.shell/"*.rc;
do                        
    if [ -r "$rcfile" ]; 
done                        

Thus, common things are stored in your .bashrc (like aliases, functions, etc.) and things you want on a specific machine are in their own directory.

Then, just put whatever machine specific files you want/need in ~/.shell and name them with .rc. For instance, I have ~/.shell/smurf.rc that sets up some smurf information.

export FAVORITE_SMURF="Poppa Smurf"
export SMURF_LOVER=$HOME/bin/blue_love  

That way, if I ever need something that’s specific to a machine, I just drop it in my .shell directory and away we go.


Cost of try/catch in JavaScript

I know that in Java, using a try/catch is fairly expensive vs. a if check. Since JavaScript has the same syntax for the most part, I wrote up a simple benchmarking script to test it out. On my box, it outputs:

if avg: 0.029
try avg: 1.372

Note, that you’ll need the Firebug plugin for Firefox in order to run this.

var tryFunc = function () {
  try {
    document.getElementById("fake").innerHTML = "hi there";
  } catch (e) {
    // eat it!
  }
}

var ifFunc = function() {
  var el = document.getElementById("fake");
  if (el) {
    el.innerHTML = "hi there";
  }
};

function benchmark(name, func) {
  var repeats = 1000;
  var elapsed = 0;
  var startTime =0;
  var endTime = 0;
  for (var i=0; i< repeats; i++) {
    startTime = new Date().getTime();
    func.call();
    endTime = new Date().getTime();
    elapsed += (endTime - startTime);
  }
  console.log(name + " avg: " + (elapsed / repeats ) );
}

benchmark( "if", ifFunc );
benchmark( "try", tryFunc );

HOWTO: Invoke a shell script on a file on save with emacs

At my current job, we use a lot of Template Toolkit. Due to some design decisions (that I consider a tad strange), we have to run a shell script on the template files (e.g. files that end with “.tt”) after they are saved in order for them to be displayed on the dev site.

Since I started using emacs about two months ago, I’ve learned quite a bit. A new thing on the learning heap is the after-save-hook. Emacs to the rescue yet again.

Here’s a emacs lisp function I wrote to automate the execution of the script when a template file is saved:

(defun ssm-cheetah-after-save-hook ()
  "After saving a tt file, run the language_update file"
  (if buffer-file-name
      (progn
        (setq is-tt-file (numberp (string-match "\.tt$" buffer-file-name)))
        (if is-tt-file
            (progn
              (setq cmd (concat (getenv "B") "/bin/YOURSCRIPTHERE --template="))
              (shell-command (concat cmd buffer-file-name))
              (message "Updated template with %s" buffer-file-name))))))
(add-hook 'after-save-hook 'ssm-cheetah-after-save-hook)

What it does, is first defines a function that checks to see if we have a file name, (which should probably always be true since we are saving now that I look at it). If we do, check to see if the name ends with “.tt.” If it does, pass the name of the file to the shell script and output a message to the user saying the template was updated. Finally, the function is added to the after save hook.


Older