Tao of the Machine

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

Dirty Python tricks

Or, abusing __getitem__ is fun.

Posted by Hans Nowak on 2004-01-30 20:16:18   {link} (see old comments)
Categories: Python

Python Apocrypha

Hmm, I didn't know this site yet. It says it's an online supplement to the Python 2.1 Bible. Even for those who don't have the Bible (easy to get from eMule Borders or Amazon), the code snippets may be useful.

Posted by Hans Nowak on 2004-01-28 22:05:28   {link} (see old comments)
Categories: Python

Abstract methods

Ivo Timmermans has a way to define abstract methods which is quite nice. For me, personally, a simpler implementation would suffice, though:

# first version

class AbstractMethodError(Exception): pass

class Foo:
    def bar(self):
        raise AbstractMethodError

class Bar(Foo):
    def bar(self):
        print 'yay!'

Instances of Foo have a bar method that cannot be called, instances of Bar (which derives from Foo) have a "regular" bar method.

If we don't know that a base class's method is abstract, and we want to call it just to be sure 1) , then this version may be better:

# second version

class Foo:
    def bar(self):
        if self.__class__ is Foo:
            raise AbstractMethodError

class Bar(Foo):
    def bar(self):
        Foo.bar(self)
        print 'wow!'

# testing it...
foo = Foo()
try:
    foo.bar()
except:
    import traceback; traceback.print_exc()

bar = Bar()
bar.bar()

A construct like this is easy to come up with if necessary. I don't recall ever having needed abstract methods, though. In my book, it's just another part of programming paranoia, together with data hiding and static typing. But who knows, maybe it'll come in handy someday.

(Nice site by the way... simple but elegant design, and a weblog talking about programming, go and calligraphy (among other things).)

1) A strange premise really, wanting to call a superclass's method without knowing what it does. But hey, it happens...

Posted by Hans "abstract met tact" Nowak on 2004-01-27 10:46:32   {link} (see old comments)
Categories: Python

Automatic Slashdot post generator


import random
import slashdot

def generate_post(topic):
    choices = [
        "I use Gentoo. How does this affect me?",
        "More info here: " + slashdot.insert_goatse_url(),
        "1. %s\n2. ???\n3. Profit!" % (topic,),
        "All your " + topic + " are belong to us",
        "First post!",
        "Mod parent down!",
        "I for one welcome our new " + topic + " overlords.",
        "RTFA!",
        "Site has been slashdotted already!",
        "Now imagine a Beowulf cluster of these...",
        "Anyone has pictures of Natalie Portman and %s?" % (topic,),
    ]
    return random.choice(choices)

slashdot.login(username, password)
thread = slashdot.get_newest_thread()
slashdot.post(thread, generate_post(thread.topic))
slashdot.logout()

Posted by Hans Nowak on 2004-01-26 18:51:06   {link} (see old comments)
Categories: (unclassified)

Lekker weertje

Not much of a winter here... 77°F today (25°C), when it's freezing in most of the US (and Europe -- hmm, snow in Greece and Turkey?).

Of course, that could change overnight. And probably will... for example, the forecast for Wednesday is 54° during the day and 24° at night. Brrr...

Posted by Hans "better get my eiderdown out again" Nowak on 2004-01-26 17:52:49   {link} (see old comments)
Categories: general

File retriever for PyPI?

This is just a thought, but now that the PyPI repository seems to have quite a few entries, is there also a way to easily retrieve packages?

It would make sense, IMHO; you can submit software through an online form or a script; the next step would be automatic retrieval. For example,

pypi-get.py wxpython

would use PyPI data to download the latest version of wxPython. Of course, refining should be possible;

pypi-get.py -v 2.4.2.4 wxpython

would download wxPython version 2.4.2.4, even if a newer version is available. Other switches I can think of:

pypi-get.py -f wxPythonWIN32-2.4.2.4u-Py23.exe
# if you know the filename

pypi-get.py --install wxpython
# try to unzip/install software after downloading

pypi-get.py --source wxpython
# download the source (default is binary)

pypi-get.py --platform=unix foobar
# download the Unix version of the 'foobar' package

etc.

An alternative approach could be, showing all the packages in a GUI, and let the user select them for downloading and installing. (Much like Fink, and the Cygwin installer, etc.)

Is this already possible? Would it be considered useful?

In theory, it would already be possible to write such a program, using the web interface. (Send a search string, scrape the resulting HTML form, download URL, find out download URL, download file.) That would be clumsy though; a dedicated interface would be better, e.g. a PyPI action that would return a record in XML format, or something like that.

Another caveat is that the "download URL" doesn't always point to a downloadable binary. In the case of wxPython 2.4.2.4, it points to the wxPython download page. Other packages have Sourceforge download URLs, which redirect you to a page as well.

But maybe this is already possible, and I have been blissfully unaware of it? (I haven't really been following the distutils and catalog SIGS.)

Posted by Hans Nowak on 2004-01-25 16:01:49   {link} (see old comments)
Categories: Python, ideas

XL

Here's another programming language to look out for: XL (part of the Mozart project). The compiler is not done yet, but people are working on it. Could be interesting.

Posted by Hans "Ada meets Python... and a whole bunch of other languages" Nowak on 2004-01-24 21:28:20   {link} (see old comments)
Categories: programming

Class methods

Today I used class methods for the first time in a "real" program. Before that, the need for them simply didn't arise, and even now I wonder if I really need them.

Firedrop has an "abstract" (more or less) Publisher class. Content type classes like Weblog and ArticleCollection derive from Publisher. I thought it might be a good idea to add methods that generate certain files for the given content type. (For example, Weblog.gen_build_ini() would generate a template build.ini with attributes appropriate for a weblog.)

This could not be a regular method, since that would require an instance, which cannot be created yet at this point (because that requires the very files that we haven't generated yet). So, I opted for classmethods. Fair enough, they work, but I wonder if I really need them. Since they don't use the class itself, functions would have worked just as well. The only difference is that class methods reside in the class namespace (of course), while functions sit in the module namespace.

So I'm wondering, what good are class methods? Is there really a need for them? I can see why they're useful in languages like Java, where just about everything has to be inside a class (even main, which is kind of ridiculous, IMHO). But in Python, are they really necessary? What can they do that functions cannot?

Update. After reading the first three comments: I understand that class methods have an implicit reference to the class they're referring to, and I also understand how and when they are used. My point is, I'm still wondering what they can do what a function can't. I mean, the class method MyClass.foo() could easily be coded as a function foo(MyClass) (with the exact same code, it seems). Why were they introduced in the first place? OK, a method is tied to the class, while an equivalent function would just "float around". Still, that seems kind of like a meager benefit.

(Apparently this has been discussed before, almost 3 (!) years ago. For example, MAL expressed scepsis as well.)

On a side note, I can also see why people want a different syntax. The current syntax

class Foo:
    def bar(cls):
        do_something()
    bar = classmethod(bar)

is awkward. It's not clear how to do this better, though. There have been several proposals and ideas, none of which seems to stand out as *the* solution. def classmethod bar(cls)? def bar(cls) as classmethod? def bar(cls) [staticmethod]? def Foo.bar(cls)? None of them seems particularly attractive.

Posted by Hans "enlightening comments welcome" Nowak on 2004-01-24 13:16:38   {link} (see old comments)
Categories: Python

--
Generated by Firedrop2.