Tao of the Machine

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

Anti-pitfalls?

My Python pitfalls article seems to be a popular read. It's written from a somewhat negative viewpoint: things that can go wrong. (Of course, that's useful; you're never going to do these things wrong again. Right? ;-) To counter-balance this, I think there should an article with a more positive viewpoint as well. So I was wondering, if I could collect 10 "anti-pitfalls"; parts of the Python language that are especially clever, elegant and intuitive.

By "clever", I don't mean "a clever hack", or "clever code", which is not always a Good Thing; rather, I mean clever design that is present in the core Python language. The ability to loop over just about everything, in a consistent and intuitive way, may be an example of clever design. Try doing that in Pascal, C or Java.

Let me know if you have any suggestions for anti-pitfalls. Simplicity counts, I am not looking for obscure corners of the language.

Posted by Hans Nowak on 2003-08-15 10:49:21   {link}
Categories: Python

.hack//infection

Quite an interesting game. It's yet another RPG, but with an innovative twist: it pretends its an online multiplayer game. Pretends, because the PS2 isn't really going online (although it's apparently capable of it), yet it plays like a MMORPG.

How does that work? First of all, you have a "desktop". Here you can read mail, work with "data" (load/save games), read news, and start the pseudo-online RPG called "The World". There are a lot of NPCs in this game, and you can talk to them or trade with them like in a real MMORPG (or so I assume -- I've never really played one :-). To make things more realistic, whenever you log on you see (mostly) new players. Some of them can become your friends, and you get their "member code" to join a party with them, and start a quest.

.hack does a good job of faking the online aspect. When you leave The World, you might have new mail from people you met; or maybe somebody posted to the bulletin board, answering your questions, or providing a new quest.

Still, it's an RPG, so you do the usual... create parties, fight monsters, pick up objects, explore dungeons, go on quests, etc. The game uses an unusual world creation system... there are "keywords", that can be combined to form a key to enter a world. Gluing random keywords together works, but can be a dangerous business, since the choice of words affects the terrain, monsters, and difficulty of the world being created.

I will only be able to play this game until Saturday (then it goes back to Video City), so I won't be able to finish it or even see most of it. That will prevent me from getting bored with it, and my daughter from humiliating me by beating the game in a couple of days. emoticon:wink2

The offcial site is here. By the way, there's also a manga of .hack. Not sure how close it sticks to the game's plot.

Posted by Hans Nowak on 2003-08-14 22:37:25   {link}
Categories: games

10 Python pitfalls

is the name of an article I just wrote. It's late, so it's quite possible that it contains typos, blatant errors, oversights, and the like. If you have a suggestion for
corrections/additions, drop me a line, or leave a comment.

Posted by Hans Nowak on 2003-08-13 02:39:12   {link}
Categories: Python

The elastiC language

elastiC. Quite an interesting language; Pythonic in many ways. At first glance, it seems to combine some good stuff from Python, Perl, Smalltalk, Lisp and C. Yet (again at first glance) it doesn't seem as kludgy as Ruby. Then again, what do I know... :-)

Can it compete with Python? Not yet, IMHO. What's missing, for example, is an extensive standard library. I also don't see an interactive interpreter (great for experimenting). And, it seems to be biased towards Unix. That's not surprising, since it's probably the platform it's being developed on. However, for me to seriously consider it, I'd need a Windows version that is up-to-date and full-featured.

But, let's see what I can do with it on Cygwin...

Update: So far, all attempts to compile elastiC under Cygwin and Mac OS X failed. emoticon:nosmile Let me know if you have more success at this than I did.

Posted by Hans Nowak on 2003-08-12 13:57:38   {link}
Categories: programming

Freebie days

De Telegraaf: Nederlanders hebben weinig vakantiedagen. (This roughly translates as: Dutch get few days off.)

How many days off do they get, then? On average, 28 days a year. Hoh hoh... here in the US, a well-paying job gives you 10 days a year or so... and you usually don't get them immediately, but only after you've worked there for a year (or two). (I'm basing this on the experience of people I know and job offers on Monsterboard and the like.) When you do menial work, you often don't get any days off at all. (Yah, there are official holidays like Christmas, Easter, 4th of July, and some typical American ones like Veteran's day, President's day, and Hitler Columbus day. My experience is that these days are mostly annoyances: post offices and some stores are closed, but *you* don't get the day off. ;-)

Free days are much better in the Netherlands, and the same goes for benefits... you automatically get insurance, build up pension, and can get (some) compensation in case you get sick or lose your job. In the US, you only get this with certain jobs.

On the other hand, taxes are *much* lower here. How would the average American feel about giving away 38% of their wages? Or maybe 50% or more if you make a lot?

Posted by Hans "'t is niet goed of 't deugt niet" Nowak on 2003-08-12 00:15:35   {link}
Categories: general

Another silly Python/Windows hack

I don't know about you, but I find Windows terrible for serious file management. There's no decent command shell, and if you use something like 4DOS or 4NT it's often a pain to deal with ultra-long filenames and names containing spaces. On the other hand, moving files around in explorer only gets you so far. What I miss, for example, is an easy way to move a file to its parent directory. In a shell, that's easy enough:

move filename ..

But in explorer, there's no such beast. You can move a file to a folder, but the reverse operation takes a few steps: select, cut, move to parent folder, paste. Too many steps for a lazy hacker. ;-)

So today, we're going to do two things: write a Python script that moves a file to its parent directory, and associate this script with "any file" in Windows.

Writing the Python code is easy. This version uses the Windows API to move the file:

# moveup.py
# Moves a file to the parent directory.

import os
import sys
import win32api

path, file = os.path.split(sys.argv[1])
parent_dir, dir = os.path.split(path)

if parent_dir != path:
    dest = os.path.join(parent_dir, file)
    win32api.MoveFile(sys.argv[1], dest)

(I haven't tested this code extensively. Let me know if you encounter problems with it.)

Now, associating this script with just one file type in Windows isn't very useful. So we're going to use it with any kind of file. This requires a registry hack. (The usual disclaimer applies... do this at your own risk, I'm not liable for any damage to your computer, etc. If you're not comfortable with editing registry keys, maybe it's better not to proceed.)

Start regedit. Go to HKEY_CLASSES_ROOT. The key we need is *, probably the very first in a long list. If a subdirectory called Shell already exists, enter it, otherwise create it. (Right-click on the *, choose New -> Key, type "Shell".)

In Shell, create a new key called "Move up" (or whatever you want to call it). Double-click on the value called "Default", then enter "Move up". Create another key here (a sub-key of "Move up"), called "Command". Double-click the "Default" value and enter something like

c:\python22\python.exe c:\scripts\moveup.py "%1"

That's it. If all went well, all files should now have an option "Move up" in their context menu. Right-click any file to find out.

Once you get this to work, the upload script becomes a lot more useful too (obviously because it lets you upload any file, not just a specific kind of file).

Note: This construct only works for files, not folders. To make that work, you probably need to associate the script with the "Folders" key, but I haven't tested this.)

References/further reading:

  1. Associating applications with any file or folder
  2. win32api.MoveFile

Posted by Hans Nowak on 2003-08-10 22:27:36   {link}
Categories: Python

--
Generated by Firedrop2.