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!

Carbon XEmacs installed

March 14, 2006
0 comments macOS

Finally I have a sensible editor installed on my Intel iMac. It's called Carbon XEmacs (aka. just emacs :)

All thanks to Andrew Choi. He's prepared two patches that makes this possible. The version I got was XEmacs-21.5.23 plus two additional patches from Andrew for Intel support and proper "Quit Application" connection with the OS. Thankfully nothing was difficult because everything went smoothly without any error messages anywhere. I did have to do some reading, searching and downloading. The hardest task was to find Andrews Carbon XEmacs site to just get started. If you also have an (Intel) mac os x and want to install XEmacs too, here are some notes on what I did:

Truncated! Read the rest by clicking the link below.

Quick PostgreSQL optimization story

March 11, 2006
1 comment Work

There are several ways to do case insensitive string matching in SQL. Here are two ways that I've tried and analyzed on a table that doesn't have any indices.

Option 1:


(
 LOWER(u.first_name) = LOWER('Lazy') OR 
 LOWER(u.last_name) = LOWER('Lazy') OR
 LOWER(u.first_name || u.last_name) = LOWER('Lazy')
)

Option 2:


(
 u.first_name ILIKE 'Lazy' OR 
 u.last_name ILIKE 'Lazy' OR
 u.first_name || u.last_name ILIKE 'Lazy'
)

A potentially third option is to make sure that the parameters sent to the SQL code is cooked, in this case we make the parameter into lower case before sent to the SQL code

Option 1b:


(
 LOWER(u.first_name) = 'lazy' OR 
 LOWER(u.last_name) = 'lazy' OR
 LOWER(u.first_name || u.last_name) = 'lazy'
)

Which one do you think is fastest?

Truncated! Read the rest by clicking the link below.

Squeezebox + Pandora

March 8, 2006
3 comments Misc. links

Where I live we have a stereo in the living room with decent sound that I often use when cooking (assuming I'm home alone). Since I'm too modern to still use those blank things called CDs I have to rely on MP3 through my Treo 650 on which I've got an SD card filled with 1gb of MP3. This works but limits me. I love Pandora and have always wanted to be able to get Pandora into my livingroom stereo. It's not been possible until now. The solution is called Squeezebox and costs $300 (£170).

I've just finished this article on NYTimes.com and it seems to be the answer to all of my problems. It's a lot of money for something I only use a couple of minutes per day but I think I'm ready to spend it just because it can do Pandora as well as play the MP3 I have on my linux and my mac over the wireless home network. Now let's hope we'll see these on the UK market soon at decent prices. Wish me luck :)