is is not the same as equal in Python

December 1, 2006
8 comments Python

Don't do the silly misstake that I did today. I improved my code to better support unicode by replacing all plain strings with unicode strings. In there I had code that looked like this:


if type_ is 'textarea':
   do something

This was changed to:


if type_ is u'textarea':
   do something

And it no longer matched since type_ was a normal ascii string. The correct wat to do these things is like this:


if type_ == u'textarea':
    do something
elif type_ is None:
    do something else

Remember:


>>> "peter" is u"peter"
False
>>> "peter" == u"peter"
True
>>> None is None
True
>>> None == None
True

Solar power in the north African desert

November 29, 2006
0 comments Politics

"Scientists say the global energy crisis can be solved by using the desert sun"

I've been wondering for a long time why the open deserts in north Africa isn't used for solar energy. There's 3 times as much sun there as there is in Europe. Unlike wind power plants in Europe, there's plenty of space for solar energy mirrors on the vast stretches of the north African desert.

This Guardian article is really interesting for me. Here are some interesting facts:

"[the two German scientists] calculate that covering just 0.5% of the world's hot deserts with a technology called concentrated solar power (CSP) would provide the world's entire electricity needs"

"Together the region could provide all its electricity needs by 2050 with barely any fossil fuels and no nuclear power. This would allow a 70% reduction in carbon dioxide emissions from electricity production over the period."

The article also sheds some light on the comparison of fusion reactors compared to solar power.

"The outlook is not promising. More than 30 countries last week agreed to spend £7bn on an experimental fusion reactor in France which critics say will not produce any electricity for 50 years, if at all."

MUnderscorePatch - tired of typing manage_main?

November 29, 2006
0 comments Zope

I often use the Zope management interface but not to do much ZMI like stuff. Just simple things like creating new base objects, changing properties or testing a change to a template. For some reason I seem to lack the ability to spell the word manage_main. I can't remember how many times I've accidently typed manage-main , mange_main, manag_main, manage)_main etc.

There's got to be an end to that! Hence the MUnderscorePatch monkey patch. Now, all I have to do is to type in m_ at the end of the URL and it becomes the same as manage_main. Ie. I go to http://localhost:8080/ then I append m_ so that it becomes http://localhost:8080/m_ and it redirects automatically to http://localhost:8080/manage_main

Silly but actually quite useful for me. Want to download it too?

Entering multilanguage data in Zope

November 26, 2006
0 comments

Zope is really great with it's publisher transform things that automatically cast HTTP GET or HTTP POST variables as proper python variables on the fly. I needed a form that asks for a Title and Description in multiple languages. The result I wanted passed into the saving method was variables like this:


>>> titles
{'en': u'London', 'sv': u'Stockholm'}
>>> descriptions
{'en': u'Capital', 'sv': u'Huvudstad'}

How did I write that form? Easy, here's the code:


<b>Title</b><br />
Svenska: <input name="title.sv:record" /><br />
English: <input name="title.en:record" /><br />
<b>Description</b>
Svenska: <input name="description.sv:record" /><br />
English: <input name="description.en:record" /><br />

Truncated! Read the rest by clicking the link below.

Bloggers not responsible for comment?

November 23, 2006
0 comments Politics

As some of my friends on this website have might have noticed, I was threatened by Carphone Ware because of supposed defamatory comments made on this page

Here's an interesting article on BBC News about a case (in USA I should add) where a blogger could not be sued for the comments other people placed on her blog.

"Overturning a decision by the San Francisco appeal court, the court ruled that people claiming they were defamed online could now only seek damages from the original author of the comments - and not the website which re-posted it."

I need to think a bit more about it before I resurrect the currently removed comments from the page about Carphone Warehouse.

Food File Online

November 10, 2006
1 comment Misc. links

Food File Online What a good little website this is! It's a database with nutritional information about different kinds of food, like bran flakes, meatballs or hot dogs

The search isn't great but it could just be because it's a limited database they have to work with. One immediate piece of feedback; it'd be great if when you do a search that it shows the search form again if you want to change your search term when you're confronted by the results. Look for example at this search result

Crazy road in China

November 6, 2006
1 comment Misc. links

Crazy road in China Here are some amazing images of a less than safe road somewhere in China. I get an uncomfortable feeling in my stomage of just watching these but still deep inside I really want to go there and experience it myself. ...if my mom would let me, that is.

I know that they've got some similar roads in Spain but I can't remember where so I can't dig up any pictures.

Memory dump blog recovery

November 1, 2006
4 comments Linux

My Firefox froze in one of the tabs when in another tab I had a long Fry-IT intranet blog half finished. To avoid having to rewrite the whole text again Jan showed me how to dump the RAM memory onto disk which I could then look through with standard tools. For this to work you have plenty of diskspace since the dump file is about 1Gb big:


$ sudo su -
# df -h
# cat /proc/kcore > /usr/kcore.dump
# strings /usr/kcore.dump > /usr/kcore.strings
# ls -lh | grep kcore
-rw-r--r--   1 root    root    1016M 2006-10-30 10:18 kcore.dump
-rw-r--r--   1 root    root      74M 2006-10-30 10:19 kcore.strings
# less kcore.strings | grep 'Bla bla bla'

Was this the most boring blog item I've written in a long time? Maybe, but it's good to have it noted the next time Firefox crashes.

Oxfordtube's website sucks

October 30, 2006
2 comments

Today I travelled on the Oxfordtube which is a coach service from central London to Oxford. It takes about 100 minutes and the busses are quite comfortable. On the saturday night before this morning's ride I bought a ticket online to save myself some time I thought. Turns out that even that I didn't know or notice it, I had bought a ticket for the 10:01 service from Victoria so my ticket was invalid as I boarded the 08:05 service. Thus, I had to buy another ticket or wait on by the bus stop till 10.01!

The crazy thing is, that nowhere does it say "10:01" when you buy the ticket!! Absolutely nowhere!

Another annoying thing is that the ticket you get when you buy online is a weird email with the subject line "CARD Transaction". It tells you on the website to print this page out, but since I don't have a printer at home I had to write down the Merchants ID and the Transaction ID and show these long numbers to the bus driver, who I should add, was a very polite and nice man. This is what the CARD Transaction email looked like

Truncated! Read the rest by clicking the link below.

Sending HTML emails in Zope

October 26, 2006
3 comments Zope

Here are a few lessons learnt when sending HTML emails in Zope with unicode data. I'm still a beginner with this stuff and still probably have a lot to learn. What I've achived now works but might break with different encodings or on different systems so don't assume that these patterns will work in your setup.

To the send the email I'm using the default MailHost that comes with Zope 2.8.5. I call it's send() passing the subject line a second time because the subject line is already in the body string.

The most valuable piece of magic to learn and remember from this is that when you construct the multi-part message you have to attach the plain text before the html text. Thank you Lukasz Lakomy for that tip!

Truncated! Read the rest by clicking the link below.