Speno's Pythonic Avocado 2004/5

2004-05-28

I'm in the Mac and the Mac is in me

My office is filled with Unix geeks, young and old, and the majority of us are using MacOS X now. Most switched from other Unix platforms like Solaris, Tru64 UNIX, or even Linux. Lately, when new folks join our group, they inevitably choose to get a shiny new Macintosh on their desk. I'm very pleased as I've been a big promoter of the Mac since 1986 or so, doubly so since Apple released MacOS X 10.1. Let us not talk about 10.0...

We have an internal mailing list for MacOS X users in the office which we use to share tips and advice with each other. This helps the switchers come up to speed quickly. Just this week I added three new members to the list as MacOS X use is now spreading to other groups! I won't be satisfied until we eliminate Windows from the office. Then we'll take on the 30,000 other people on campus! Smiley

I've mentioned that I do all my Python development on MacOS X, but what I haven't mentioned is how successful that's been. Using VirtualPC, I'm able to build distributions of my Python applications for Unix, MacOS X, and Windows all on my Macintosh, aka the Hellmouth. I'm chuffed about that. However, as readers of the MacPython list know, there are some problems with Python on the Mac under 10.3 that are still being worked out. I'll have to exhume a 10.2 machine on which to build the next release of our big application to guarantee that it will run on all the Macs we support. Oops.

Take care.

posted at 21:42:24    #    comment []    trackback []
 
2004-05-24

The flowers are still standing! (abusing iterators)

A few months ago a friend and I were discussing a bug in dnspython when I first learned about the trouble of trying to delete items from a dictionary while iterating over that dictionary. If you try to do this, you'll get a RuntimeError exception explaining that 'dictionary changed size during iteration'. Try it yourself:

d = { 1: 'one', 2: 'two', 3: 'trois'} for key in d: del d[key]

I bring this up now because a similar issue came up recently and bit me. I was trying to enumerate a list and delete some of its elements. Python lets you do this without raising an exception, but it's probably not going to work the way you think it does.

>>> l = range(3) # take a little list >>> e = enumerate(l) # enumerate it >>> e.next() # ok (0, 0) >>> del l[0] # get rid of this element >>> e.next() # oops, we skipped one... (1, 2) >>> e.next() # The flowers are still standing! >>> e.next()Traceback (most recent call last): File "", line 1, in ? StopIteration

The list shrinks in place when you delete an element, but the index part of your iterator moves ahead and causes you to skip an element. In this case, we skip the element with the value of '1' because it resides at l[0] after the deletion.

So, if you wanted to do this properly, use range, start at the end of your list, and count backwards...

Take care.

This post references topics: python
posted at 23:03:28    #    comment []    trackback []
May 2004
MoTuWeThFrSaSu
      1 2
3 4 5 6 7 8 9
10111213141516
17181920212223
24252627282930
31      
Apr
2004
 Jun
2004

One python programmer's search for understanding and avocados. This isn't personal, only pythonic.

XML-Image Letterimage

© 2004-2005, John P. Speno