Mocking a Python standard library

March 14, 2008
2 comments Zope

Here's one of many things I've learnt today at PyCon. Inspired by code that Grig Gheorghiu showed in his slides on automated testing, you can monkey patch a standard library that your application is using in your unit tests to, in my case, mock a remote service without having to run a server. I've done lots of monkey-patching in Zope but then I've only been monkey patching individual methods or attributes of imported classes. This is very similar to that. Here's what my application does:


from poplib import POP3
class MyZopeApp(...):
   def check4mail(self, hostname, port, user, pwd):
       connection = POP3(hostname, port=port)
       ...download emails and process them...

Adjacent to this I have a unit/integration test that looks like this:


class TestCase(ZopeTestCase):
   def test_check4mail(self):
       # monkey patch!
       # note that this imports a module, not a class
       from Products.IssueTrackerProduct import IssueTracker 
       FakePOP3.files = ('test1.email',)
       IssueTracker.POP3 = FakePOP3

       # now check what happens when check4mail() is run
       result = self.folder.tracker.check4mail()
       assert ...

Truncated! Read the rest by clicking the link below.

See you at PyCon 2008

March 11, 2008
0 comments Python

I'm going to Chicago on Wednesday for the PyCon 2008 conference. I'm going to stay at the Crowne Plaza (or whatever it was called) like many of the other people at the conference.

This is what I look like:

See you at PyCon 2008

If you see this mug, go up to it and say Hi. It speaks British, Swedish and some American and loves food, beer and tea which might be helpful to know if you would feel like to talk more to it. Its interests for this conference are: Grok, Zope, Django, Plone, buildout, automated testing, agile development and Javascript. Its main claim-to-fame is an Open Source bug/issue tracker program called IssueTrackerProduct which it is more than delighted to talk about.

I've never been to Chicago before and I'm really excited about Tuesday night as I've bought tickets to a Chicago Bulls NBA game (basketball). All other nights I'm hoping to socialise, get drunk, get full and get down and dirty nerdy all week. See you there!

File check before delete

March 7, 2008
1 comment Linux

Because I always forget, here's how to check if a file exists before attempting to delete it in bash:


[ -f foobar.html ] && rm foobar.html

If you don't do it this way and the file doesn't exist you get this:


rm: cannot remove `foobar.html': No such file or directory

"Confessions of a College Callgirl" is the new "My Secret Life As A Prostitute"

March 2, 2008
2 comments Misc. links

Back in 2004 I used to sometimes read a blog called My Secret Life As A Prostitute which was both sexy, sobering and kind of educational. That blog was also a callgirl who wrote about her work-experiences, sex life in general and problems she had with her boyfriend. Sadly that blog disappeared suddenly. Perhaps the girl who wrote it got "discovered". By that I might mean that a colleague of hers at some investment bank found out what she was spending so much time on in her lunch breaks. Jokes aside, it's hard to tell if this is for real or just made up based on someone's fantasy of being an escort girl. Sometimes the blog gets very technical to the point that it's hard to imagine her not being a real call girl.

As I'm skimming through this new blog I find that it's so similar to the one I was skimming back in 2004. She's very blunt and has the same sense of humor and just like the 2004 blog every now and then, as she describes her encounters/jobs she also describes how she herself gets off on it.

Another similarity is that reading this gives the same feeling of shame of reading it. I'm obviously not so ashamed that I can't confess that I'm reading up it does feel a bit intimate and "dirty" at the same time. It's like reading an Bret Easton Ellis book which is sold in respectable normal bookshops but whose content is sometimes worse than porn.

And for people who've seen Flight of the Concordes, check out this post

hostip.info - Look up the location from an IP

March 1, 2008
1 comment Linux

I've never really needed it but I've been looking for a tool that is super easy to use that quickly looks up the location of an IP address. The super easy tool is called hostip.info and this is what happens when you do a lookup on www.peterbe.com's IP at http://api.hostip.info/get_html.php?ip=80.68.212.7 :


Country: UNITED KINGDOM (UK)
City: (Unknown city)

Cool. I'll remember that till next I really need this. I discovered hostip.info by reading this cool blog by Corey Goldberg

CommandLineApp by Doug Hellmann

February 22, 2008
0 comments Python

I just read the feature article "Command line programs are classes, too!" by Doug Hellmann in the January 2008 issue of Python Magazine about his program CommandLineApp and I've tried it out on one of my old Python programs where I do the opt parsing manually with getopt. The results are beautiful and quick. It's sprinkled with Doug specific magic but I quickly got over that when I saw out easy it was to work with. There are still a few questions of things I didn't manage to work out but that will unfortunately have to wait.

If anything, the worst thing about this library is that it's not part of the standard library so either you have to tell people to sudo easy_install CommandLineApp in the instructions or include it yourself in your packages if you prefer to ship things with a kitchen sink included.

If you want to check it out in action, either subscribe to the magazine (and support the effort) or just download csvcat

If Americans knew - An interesting insight into the Israeli Palestine conflict

February 12, 2008
4 comments Politics

If Americans knew - An interesting insight into the Israeli Palestine conflict I'm not a political editor and I can't confirm the validity of these statistics but if they're at least close to true it's a hell of a scary picture of the injustice in the Israeli/Palestine conflict.

Israel receives $6.8 million per day in aid. I doubt that all of that money goes towards building better schools for Israeli families.

Truncated! Read the rest by clicking the link below.

Chinese New Year and the Persecution of Falun Gong in China

February 9, 2008
2 comments Kung Fu

Chinese New Year and the Persecution of Falun Gong in China Today I'm celebrating the Chinese New Year with a big dinner in Chinatown in central London. I love the Chinese people, their food, their culture and am perpetually interested in learning more about them and their ways.

But we mustn't forget that this otherwise great country also has a very very dark side which we must never accept or forget: The Persecution of Falun Gong.

Didn't the nazi persecution of jews teach us anything? Are you aware that Falun Gong practitioners are imprisoned, bullied and tortured? Do you know that this oppression is fully sponsored by the government and not rogue individual groups?

I was made aware of this website (warning: graphical content) and this video just a few days ago. I'm fully aware that China is a big country where many "bad" things can happen due to the sheer numerical chances with such large numbers and such many different internal cultures but that's never an excuse for this barbaric behavior.

Think about it. Remember it.

logrotating all my Zope event logs

February 6, 2008
0 comments Zope, Linux

I've installed a lot of Zope instances on my laptop since version 2.7.3 and out of curiosity and desperate need for more hard drive space I thought I'd log rotate them all with the standard Linux logrotate program.

Before doing the log rotate, the total size of all my event.log files came to about 290Mb! After running logrotate (twice of course to go from event.log.1 to event.log.2.gz) the total size become 20Mb. Not a huge significance in the world of gigabyte hard drives but at least something.

Ocado gets customer service right

February 2, 2008
8 comments Misc. links

I don't shop from Ocado anymore but maybe I should start doing that again. The reason I only tried it once was that I wasn't able to find as many products on their website as I needed plus if you're not a big family it often doesn't make sense to buy too much fresh food in one go.

They're cleverly keeping me in check by sending sporadic emails very rarely about changes to their features and other bits and pieces. I'd call it "spam I don't mind" and I don't get annoyed when I get them because they're so rare. And here's one that arrived the other day which I actually appreciate a lot. Basically, they've gone out of their way to improve their image on the environment to send me another email. It's well phrased, humane, well layed-out and informative.

Ocado gets customer service right

Keep up the good work guys!