Blog

During the course of the day I encounter lots of challenges, some of which take minutes to solve and others that take much longer. My goal for this section is to keep track of these challenges and their solutions. I will turn the longer ones into articles, while the shorter ones will stay as blog entries. You can expect topics to range from very specific programming challenges to broader topics like life.

Below is a list of the recent blog entries. You can also browse the blog by using the tags on the right side, or if you know what you are looking for then you can use the search box at the top right.

Thank you, come again

OS X

About two months ago a friend insisted that I should seriously think about switching to a Mac. OS X is especially of interest to me because I have been into Linux for a few years now, mostly using it as a learning platform, and, given the Unix core, OS X becomes a very nice alternative. There are several things that have kept me from moving to Linux, including (please read the complete post before adding comments) the hit-or-miss upgrades, missing or little support of some hardware, not so great support for Windows based applications (like I said, read the whole post first :)), and the fact that my main jobs have revolved around Windows development. Mac, on the other hand, overcomes many of these issues (VMWare Fusion looks awesome).

With my recent involvement in web development, starting out with Ruby on Rails and then settling on django, I have realized that I now have many more choices of operating systems than I did before. This is especially true since my editor of choice is vim, which is supported on most of the platforms, and I generally do most of my development on a server running ArchLinux. So, here are my thoughts after two months of honest exploration, even going to the extreme of asking a friend running Hackintosh to lend me his computer (in exchange for the tablet :)).

Read full post...

Getting around boolean Sphinx bug when using Postgresql

When using Sphinx with Postgresql you have to be a little careful. Other than the search binary requiring the MySQL libraries even when you are not using MySQL, I just encountered another issue with Postgresql where it doesn’t look at postgresql boolean attributes correctly; in this case sphinx thinks of all values as true. To get around this, until the fix is released, you can CAST the boolean field as integer. So, in your sql_query you would do something like the following.

bc[sql].
sql_query = SELECT id, title, DATE_PART(‘epoch’, created_on) AS created_on, CAST FROM mytable

The above SQL also demonstrates the DATE_PART function. The sphinx documentation shows UNIX_TIMESTAMP, which is a MySQL only function, as the function to use when indexing sql_attr_timestamp.

Firefox 3 coming this Tuesday, June 17th!

It is official, Firefox 3 will be released on June 17th! You can always download the release candidate if you don’t want to wait till the official release. Oh, and remember to pledge to download on June 17th and help set a world record. “Did you pledge?” you ask? Obviously!

Added a new article

Last night I added an article about fixing the “Access: Local only” issue in Windows Vista. Interestingly, this is another “feature” of Windows Vista that is causing trouble. Check it out in the articles section. Below are a few URLs that also talk about this problem.

http://blogs.technet.com/teamdhcp/archive/2006/11/08/use-of-broadcast-b-flag-in-dhcp.aspx
http://www.askdavetaylor.com/dhcp_unicast_broadcast_flag.html
http://thedaneshproject.com/posts/vista-not-working-with-dhcp/

Quickly running doctests in ipython

Python has a nice module called doctest that lets you embed test cases with the function documentation. This is a really nice time saver because these test cases also serve as examples, which I would have to write anyways. I was working on a function where I decided to write a few doctests, but I did not like waiting for django’s test runner to create the database and then run the tests, which wasn’t necessary for these specific tests. So, I thought why not manually run the doctests in a django shell?

ipython has support for outputting in doctest mode (%doctest_mode magic function), but it doesn’t come with any magic functions that will let you quickly run doctests. So, I ended up writing the following magic function that will let you accomplish this.

To use this, here is what you do.

  1. Edit your ~/.ipython/ipy_user_conf.py file…
    1. Copy and paste the dodoctest function at the bottom (before the main() call).
    2. Copy and paste the ip.expose_magic call all the way at the end of the file (i.e. after the main() call).
  2. Run django shell (or ipython directly): django-admin.py shell
  3. Import your models: import myproject.myapp.models
  4. Call the %dodoctest function with your models as a parameter:
    %dodoctest myproject.myapp.models

The magic function.

1
2
3
4
5
6
7
8
9
def dodoctest(self, arg):
    """Initializes the test runner and runs the doc tests for the specified object."""
    ip = self.api
    ip.ex("""import doctest, unittest;
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(%s))
runner = unittest.TextTestRunner()
runner.run(suite)
""" % arg)

The call to register the above magic function.

1
ip.expose_magic('dodoctest', dodoctest)

Editing HL7 Messages in VIM

Example of the status line, notice the "OBX-3" at the bottom.

vim is my editor of choice, but, unfortunately, currently there isn’t anything special that vim does for HL7 messages. The one thing that I miss the most is not knowing which segment/field the cursor is currently in. Knowing that vim is very customizable, I set out to see if I could add this feature myself. What I ended up was two functions with about 35 lines of code.

The first one SetHL7StatusLine changes the vim statusline to call the second function, which looks at the current line and figures out the HL7 segment name and field number, and then outputs that to the statusline. Below are the instructions if you would like to add this functionality.

  1. Copy the code below and paste it in your vimrc file (:e $MYVIMRC)
  2. Make sure that status line is visible (:set statusline=2)
  3. Once you have the message in vim, call the function to set the appropriate variables (:call SetHL7StatusLine())
  4. Enjoy! 🙂

I realize that this is a lot of work, but right now there isn’t enough functionality to turn this into a vim plugin.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function! SetHL7StatusLine()
  set statusline=%<%f\ (HL7:\ %{HL7SegmentInfo()})\ %h%m%r%=\ %-14.(%l,%c%V%)\ %P
endfunction
" This function adds the segment name and field number to the status line.
function! HL7SegmentInfo()
  let line=getline(".")
  let pieces=split(line,"|")
  let cursorAt = col(".")
  if len(pieces) == 0
    return "No Segment"
  elseif (len(pieces) == 1) || (cursorAt <= strlen(pieces[0]))
    if strlen(pieces[0]) > 3
      return "Invalid segment?"
    else
      return pieces[0]
    endif
  endif
  let seg=pieces[0] . "-"
  let position=strlen(pieces[0])
  let segNum=0
  for index in range(1, len(pieces) - 1)
    let segNum += 1
    let position += 1
    let piece = pieces[index]
    if cursorAt <= (position + strlen(piece))
      return seg . segNum
    endif
    let position += strlen(piece)
  endfor
  " If the last piece was the separator (|) then VIM doesn't treat that
  " as a separate piece so we have to account for this special case.
  if strpart(line, strlen(line)-1, 1) == "|"
    let segNum += 1
  endif
  return seg . segNum
endfunction

screen is awesome!

Screenshot showing two windows sharing the same screen session

Linux/Unix has so many cool utilities for the command line and very often I find new things about the tools that I am already using. I have recently upgraded to dual screens at home (19” and 22” combo) and needed a command line window to be open on each screen. I have been using screen to have multiple sessions and keep my taskbar uncluttered1, but the multiple windows put an interesting spin on my screen usage. I wanted to be able to switch to any session in either window without having to detach and then reattach in a different window. So, I started reading the screen manual and noticed the -x command line argument. Guess what it does? Attach to a session which is already attached elsewhere (multi-display mode). Exactly what I was looking for!

By the way if you are looking a hosting service, make sure to checkout WebFaction. They have excellent packages and support, and have screen installed on their servers.

1Additionally, with screen you don’t have to open multiple SSH session if you are connected remotely.

Mozilla Firefox 3 RC1

Firefox 3 with several tabs and numerous extensions

I have been using Firefox 3 since Beta 4 and I love it so far. The better memory management made it very much worth the few crashes that happened due to the Beta status. If you have been thinking of upgrading to Firefox 3 then now is probably a good time because Mozilla released the Release Candidate 1 over the weekend (Saturday, May 17). Obviously I upgraded right away, and so far have found it to be very stable.

Most of the extensions aren’t officially supported, but I have found that many work without any changes. You can easily get around the version check by setting extensions.checkCompatibility to false.

Pahonix!

Since I am in Phoenix right now, my brother sent me a link to the following FedEx Commercial that has a reference to Phoenix (or shall I say “Pahonix!”) =D

You can also find it at: http://www.youtube.com/watch?v=72cn6Yyw4Ko

Have you lost email on gmail?

2012-12-10, fa: If you experience any lost email, first make sure to go through this troubleshooting guide

Recently I have been thinking about consolidating all my email accounts into a single one because I would like to be able to check all my emails from any computer, and check it quickly. The obvious choice was gmail, but there was an interesting incident that happened about two weeks ago which has made me a little skeptical about gmail as a possible choice. I was sharing a desktop session with a friend who heavily uses the whole gmail suite (calendar, gmail, etc.) and interestingly majority of his email suddenly disappeared! One second the email was there, and the next second it was gone. He looked around in the trash, the spam folder, and every other corner where it could have gone, but it was nowhere to be found. This was all happening in front of me… We searched on google groups for gmail and found numerous threads about lost email.

Thinking about it, I am wondering how many folks had this issue, but decided not to post on google groups (time is important). Have you had this issue? I am aware of a similar incident that was brought up about a year ago, and I am wondering if this is another wave, and in the end, whether gmail is reliable. Are you aware of any better alternatives? (Ideally free)

As with anything important, I am also researching my options of backing up my gmail account.