Tao of the MachineProgramming, Python, my projects, card games, books, music, Zoids, bettas, manga, cool stuff, and whatever comes to mind. |
Front page Last 10 archives 2003-11-01 2003-11-08 2003-11-15 2003-11-22 2003-11-29 2003-12-06 2003-12-13 2004-01-03 2004-01-10 2004-01-17 Category archives bettas books CCGs Charm esoterica Firedrop games general ideas internet Kaa linkstuffs Lisp manga music Mygale Nederland nostalgia programming Python SGI switch Wax Zoids :: Search All posts ...by category ...by date ![]() :: About me :: Oasis Digital :: Resume :: GeoURL :: RSS :: Atom ![]() ![]() |
Dirty Python tricksOr, abusing __getitem__ is fun. Python ApocryphaHmm, 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 Abstract methodsIvo 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).)
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() Lekker weertjeNot 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.) XLHere'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 methodsToday 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, 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 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 (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.
Posted by Hans
"enlightening comments welcome"
Nowak on 2004-01-24 13:16:38
{link}
(see old comments)
--
Generated by Firedrop2.
Categories: Python |