Tao of the Machine

Programming, Python, my projects, card games, books, music, Zoids, bettas, manga, cool stuff, and whatever comes to mind.


Contracts in Python. The latest in a series of articles that I don't find very good.

Let's dissect this one, for example. Page 1: "In a weakly typed system like Python, variables don't have types." Eh? Since when is Python weakly typed? Now this issue has come up before in c.l.py, and some people like to say "weak typing" when others mean "dynamic typing", and "strong typing" for "static typing". No biggie, but sentences like this are just plain wrong, and Guido doesn't do anything to correct him. IMHO, the translation should be:

original text translation
"In a weakly typed system like Python..." "In a dynamically typed system like Python..."
"...variables don't have types." "...you don't have to declare variables."

Page 3: "Doesn't the lack of type declarations on method parameters make it hard to change code, especially in public libraries? People can define a class and overload * to mean anything. They can then pass in an instance of their class to times, and use the times method in a way the method's designer never imagined, because * means something completely different."

Yes, that's the whole point. Who says you can use code only for things that its designer imagined? Signature-based polymorphism makes it *easier* to change code. A lot easier than in Java or C++ (or Pascal, for that matter) where all the declarations are in the way. You can build up a working system in no time (prototyping, something Python was originally intended for), but also change it quickly relatively painlessly.

(I am not just guessing this. Ever try refactoring a Python program, versus a Delphi program? :-)

(And of course, I can overload a method in Java, C++, etc, to mean something different and unexpected. At worst, that is just an example of bad design, and not a flaw in the language.)

Overall, the interviewer doesn't seem to know a lot about Python or dynamic languages in general. But maybe users of statically typed languages are the target audience for these articles, dunno. Still, spending a day in comp.lang.python (not python-dev! ;-) would probably teach you more about Python than this series.

Posted by Hans Nowak on 2003-02-06 17:37:56   {link}
Categories: Python

Try to explain this to an American...

CBR maakt rijbewijs duizend euro duurder [Dutch]. Basically the article says that getting a driver's license in the Netherlands will cost €1000 more than it does now. In the near future the total cost will be around €3000. Even if this raise is just a rumor, the current cost is around €2000.

At the moment the euro (€) has roughly the same value as the US dollar. Hmm, I wonder what Americans would say if they had to pay $2000 or $3000 to get their license... not to mention, take weeks or months (or years, in my case :-) of drivers lessons, followed by a very strict and difficult exam, where only around 35% of the candidates actually pass. Once you have your prized license, you get rewarded by the driving experience in the Netherlands: lots and lots of traffic signs (and you are expected to see them all at once), lots of narrow streets, roundabouts, speed bumps and 30 km/h zones, and interesting rules, like, bikes coming from the right have the right of way. I am not making this up.

Also, most cars are stick-shift, making it all the more fun. And don't expect kids on bikes and mopeds (or anywhere else, for that matter) to know the traffic rules. They do whatever they want, cross red lights, etc. And if you accidentally hit them with your car, it's your fault, no matter what they did.

But if you think that is ridiculous, you don't know the price of gasoline in the Netherlands yet... ^_^

Posted by Hans Nowak on 2003-02-05 13:38:58   {link}
Categories: general, Nederland

Tuesday roundup

More struggling with web application platforms. Fortunately the people in c.l.py are quite helpful. I got WebWare 0.7 working now, with some hackery, on Windows 2000 with CGI/IIS. Quixote looks interesting too, more elegant and less esoteric than WebWare, at least at first glance (which is usually wrong :-). Then there's CherryPy...

On a totally unrelated note: everybody knows about Fredrik's decision by now. He makes some valid points. I mean, it's easy for me as somebody who doesn't develop Python to stand by the side and criticize, but what I saw recently was... worrisome. As /F says, no talk about the next Python 2.3 release; instead, abhorrent syntax changes are discussed with zeal. Especially this "thunk" (what the hell is that anyway?) stuff:

foo = bar:
    x = 1
What does this code do? I don't have a clue, and in spite of reading a bunch of posts about "extended function syntax" and "thunks", I haven't been able to figure it out yet. Very intuitive it is not. Let's hope we don't see this stuff anytime soon, unless the developers have a really good reason to put this in. >_<

Posted by Hans Nowak on 2003-02-04 21:02:50   {link}
Categories: Python

A quirk in Kaa's embedded Python

Kaa currently has a quirk, in that it wants, and assumes, the first line to have an indentation of 0. You can write this:
    print 'whee'
but you cannot write this:
    print 'foo'
    print 'bar'
Why does the first example work and the second doesn't? Because the first line is assumed to have indentation 0. Kaa sees the second example as:
print 'foo'
    print 'bar'
In other words, inconsistent indentation. There are no problems if you just use an indentation of 0 to begin with, like
print 'foo'
print 'bar'
This needs to be fixed before 0.9.

Posted by Hans Nowak on 2003-02-03 09:48:52   {link}
Categories: Python, Kaa

Spijs op je lijf

For work, I'm currently looking at something like ASP for Python. Python Server Pages, so to speak. One good candidate seems to be Spyce.

Including Kaa, I've seen three programs that use embedded code in HTML (the other two are Vellum and Spyce), and they all use a different syntax. Kaa uses <% %> and <# #>. Vellum uses <% %> as well, but allows the "=" syntax (IIRC) and weaving HTML and Python, using the word "end" as a dedent replacement. Spyce uses [[ ]] and allows "=" and weaving as well, and uses { and }. To illustrate, here's an example from the Spyce site:

  Hello [[print 'world!',]]
  [[ for i in range(10): { ]]
  [[ } ]]
  Color chart:<br>
  <table border=0 cellpadding=0 cellspacing=0>
  [[ cols = ['22', '66', 'AA', 'EE' ]
     lencols3 = 5*len(cols)
     for r in cols: { ]]
       [[ for g in cols: { ]]
         [[ for b in cols: { ]]
           <td width=5 height=[[=lencols3]] 
         [[ } ]]
       [[ } ]]
  [[ } ]]
Notice constructions like
[[ for i in range(10): { ]]
[[ } ]]
which are not allowed in Kaa. In Kaa you would write:
for i in range(10):
    print i,
which is more Pythonic, IMHO... no artificial extensions to the language.

For fun and profit, here's how you would write the example above in Kaa:

print "Hello world!"
for i in range(10):
    print i,
print "<br>"
print "Color chart:<br>"
print "<table border=0 cellpadding=0 cellspacing=0>"
cols = ['22', '66', 'AA', 'EE']
lencols3 = 5 * len(cols)
for r in cols:
    for g in cols:
        for b in cols:
            print "<td width=5 height=", lencols3,
            print "bgcolor=" + "#"+r+g+b, "></td>"
    print "</tr>"
print "</table>"
and here's the result:

Hello world! 0 1 2 3 4 5 6 7 8 9
Color chart:

(Note: Of course, I'm only comparing the ways of embedding Python here, not the capabilities of both systems... Spyce is a "Python Server Pages" system, Kaa is a client-based weblogging tool, two entirely different beasts.)

Posted by Hans Nowak on 2003-02-03 09:27:32   {link}
Categories: Python, Kaa

Try this one for size

Not much to write today, so I'll just share some code. I did some refactoring of the Firedrop GUI, and this useful little class came out. It's not spectacular, but it does the job.
class SizerPanel(wxPanel):

    def __init__(self, parent, direction, *args, **kwargs):
        self.id = wxNewId()
        assert direction in (wxHORIZONTAL, wxVERTICAL)
        wxPanel.__init__(self, parent, self.id, *args, **kwargs)
        self.sizer = wxBoxSizer(direction)

    def finalize(self):
    def Add(self, *args, **kwargs):
        """ Shortcut to replace sizerpanel.sizer.Add(). """
        self.sizer.Add(*args, **kwargs)
I found myself using this layout style too much: create a panel, create a BoxSizer, create components, add components to the box, tell box and panel about each other, and finalize with some calls to Fit() and SetAutoLayout(). With SizerPanel, you can do this shorter and cleaner:
p = SizerPanel(parent, wxVERTICAL) # or wxHORIZONTAL
That's all.

(Note: Yes, in spite of all my Swing ramblings, I decided to code the first version of the Firedrop GUI in wxPython. If it doesn't work well on the Mac, I can always code another front-end in Swing. But that seems a lot of work, mostly in finding out how all the components work, and I wanted to get going.)

Posted by Hans Nowak on 2003-02-02 11:15:52   {link}
Categories: Firedrop, Python

Generated by Firedrop2.