Tags

Nav_top_border

Blogs I Read

Nav_bottom_border

Search

My blog feed

Welcome to Farhan’s personal blog

This will probably be the most active part of my site. During the course of the day I encounter lots of challenges, few that take minutes to solve and others that take 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 in reverse chronological order. You can also browse the blog by using the calendar archive on the left side, or if you know what you are looking for then you can use the search box towards the lower left part.

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.

1
sql_query = SELECT id, title, DATE_PART('epoch', created_on) AS created_on, CAST(is_deleted as integer) 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.

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!

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/

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…
    • Copy and paste the dodoctest function at the bottom (before the main() call).
    • 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)
Example of the status line, notice the

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
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.

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

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.

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

Older