Date formatting in Python or in PostgreSQL (part II)

April 19, 2006
0 comments Python

This is an update on Date formatting in python or in PostgreSQL where the test wasn't done very well. The solution using Python for the formatting created a new DateTime object each time for each formatting because the time_stamp extracted from the database was a string. That would be beneficial to the Python formatting alternative but that's not the whole point. I suspect that the way I did the experiment last time (code is lost by the way) was wrong and didn't focus on the correct benchmark.

In this, my second attempt, I've done a more correct test and tried it on 500 selects. 500 formatted in SQL and 500 formatted in Python. The results are even more outstanding for PostgreSQL than last time.

Here are the results:


times1 (python formatted)
0.113439083099
times2 (sql formatted)
0.00697612762451

That means that doing the date formatting in SQL is 16 times faster!!

Bare in mind that this is optimization and you always have to be careful when doing optimization. For example, the SQL database shouldn't get involved in the presentation and if you need to use a different locale you might to change your application in two places which is risky.

Careful when dealing with options in IE

April 14, 2006
2 comments Web development

If you have a drop down that looks like this:


<select name="name"
 onchange="alert(this.options[this.selectedIndex].value)">
  <option>Bill</option>
  <option>Gates</option>
</select>

you'll get a different alert message in Firefox and IE6. Try it here

Firefox will understand that if you don't have a value attribute on a option tag, it takes the content of the option tag as the value. IE doesn't. On the above code it returns blank (aka. empty string) in the alert.

Truncated! Read the rest by clicking the link below.

Case insensitive list remove call (part II)

April 11, 2006
1 comment Python

Yesterday I blogged about a simple algorithm for removing a string from a list of string case insensitively. I was happy to see that several people joined in with suggestions just a few hours after I published it. I have now thought about it a bit more and to honour those who commented I've done a little benchmark to find out which one is the best.

What both my own solution and some peoples suggestions forgot was to raise a ValueError if it fails just like this would: list("abc").remove("d")

So, I've tidied up the suggestions and where need be I've added the exception throw. This is not the first time list comprehension in python impresses me. The winner in terms of performance is Andrews list comprehension suggestion. Here are the timeing results:


f1 0.704859256744
f2 1.5358710289
f3 1.37636256218
f4 0.468783378601
f5 0.475452899933
f6 0.666154623032

Truncated! Read the rest by clicking the link below.

Case insensitive list remove call

April 10, 2006
13 comments Python

Often when working with lists of strings in python you might want to deal with the strings in a case insensitive manner. Today I had to fix an issue where I couldn't use somelist.remove(somestring) because the somestring variable might be in there but of a different (case)spelling.

Here was the original code:: def ss(s): return s.lower().strip() if ss(name) in names: foo(name + " was already in 'names'") names.remove(name)

The problem there is that you get an ValueError if the name variable is "peter" and the names variable is ["Peter"]. Here is my solution. Let me know what you think:


def ss(s):
   return s.lower().strip()

def ss_remove(list_, element):
   correct_element = None
   element = ss(element)
   for item in list_:
       if ss(item) == element:
           list_.remove(item)
           break

L = list('ABC')
L.remove('B')
#L.remove('c') # will fail
ss_remove(L, 'c') # will work
print L # prints ['A']

Might there be a better way?

UPDATE Check out Case insensitive list remove call (part II)

Shark kayak

April 5, 2006
19 comments Misc. links

Shark kayak Did you see the spectacular photos from 2003 where a surfer is accompanied by a big dolphin looking like a shark? Well, it just looks like a shark but apparently is a dolphin (you can tell by the top fin). Well, here's a photo of a guy in a kayak being followed by a great white shark.

Accordning to Snopes.com the photo is real and fortunately it's a safe setting. The guy in the kayak is a great white shark researcher. Even if it is safe, they didn't know that until after the experiment :)

Merrill Lynch's f**ked up website

March 28, 2006
3 comments Web development

Don't ask me why but I today clicked a Google ad about IT jobs at Merrill Lynch. Not that I'm interested but I guess I was just curious to find out what this super-rich company has to offer in terms of IT technology. I guess what they need is a team of web developers. Here's why...

First of all, look at this screenshot. Apparently my browser is too crap for their website, but I know they're wrong. Firefox 1.5 is one of the fastest growing and most advanced browsers available. According to Google Analytics, about 30% of visitors of peterbe.com use Firefox. Granted that that site is geek oriented but more than 90% of my visitors are new ones who drop in through random Google searches. Out of curiousity I started the Windows machine we have at work to try the site there and when I reached the site I was horrified to know that there it does work in Firefox. Are Merrill Lynch Linux-haters? If so, let's continue complaining.

Truncated! Read the rest by clicking the link below.

Teach me about OCR

March 25, 2006
0 comments Linux

I might soon need a good OCR program to read scanned in pages but these pages aren't perfectly scanned pages from a novel. The kind of pages I'm scanning are stuff like printed out invoices and other stuff like that with tables, headers, logos, footers, etc.

The only program I've looked ocrad and I've had pretty decent results with it. I did scan an invoice and thanks to a quick python script I was able to find out the correct rotation with a 57% confidence (the second best was 37%). That's a start. ocrad seems very flexible and quite active judging from the mailing list

I guess I need to do more research into tuning ocrad with the right charsets, image formats and some of the immediate options of ocrad before I give up. When I scanned my invoice, the words it found did look like words but not much qualitative could be used out of. The company that sent the invoice was for example not anywhere in the recognized words :(

What do people use out there? I bet Amazon didn't just use ocrad when they did their Search Inside the Book

To br / or not to br/

March 23, 2006
1 comment Web development

Back in the very beginning of this centuary I remember that there was a web browser called "netscape". Apprently it had a bug in it that you had to write all linebreaks like <br /> and not <br/> if you decide to use XML compliant HTML.

Ever since then I've almost always respected that rule putting in that extra space between the r and the /. Unless anybody can think of a reason not to, I'm going to ignore and forget about netscape and the mandatory extra whitespace. Bye bye netscape+XML+Peter!

Annoying Safari just ate my blog

March 20, 2006
6 comments macOS

I rarely use Safari when I'm on my mac but the reason is just because I'm not that bothered. At work I'm 100% Firefox and when I do any web development I use Firefox because it's extensions and bits and pieces makes it superior bar none for that purpose. Tonight I was using Safari actually to blog about Richard Feynman. I had written quite a long text and since I have several tabs open that I wanted to link to I went to one of the other tabs then back to the blog-adding-window. But, because I stumbled a bit with my mouse I accidently pressed the close icon. Result: I lost all of my text I had written :(

It's stupid because it's so easy to trip over the close icon. I prefer the way Firefox does it where they have one close icon for all (or actually the current highlighted tab). Another disadvantage of having the close icon for each tab is that it takes up valuable space for writing the page title out with more letters. Example screenshot

What a silly rant this has been. I'm sorry for wasting your time. I just thought I'd let you know that Safari wasted a lot of mine.

Kung fu in East London with Shkar

March 16, 2006
11 comments Kung Fu

Shkar mugshot after the kung fu class in stratford (taken with my Treo) If you live in the Stratford or east London area you should do what I did last night. I went to Shkar Sharif's new kung fu club in Stratford. It's a quick walk from the Stratford station and getting to Stratford is quite easy for me thanks to the Waterloo & City line + the Central line. Doesn't take long.

Shkar and I have been training for many years now together under Dave at the City & Islington club; Shkar a bit longer. Last year, Shkar took his instructor grading and has now started this club on his own (with some help from Dave). He's been running this club now for only a couple of weeks but is (t)here to stay. I genuinely recommend anybody in the area to come along and train there. At the moment he's only teaching kung fu (Fujian White Crane) at that venue but will soon have t'ai chi classes too.

If you want more details about the classes go to his club page See you in class!