programming: October 2004 archives


Football for the Lose

[ rakaur on Sat Oct 30 at 09:26 PM // category: eawr, programming, school, technology ]

So, we lost 20-28 today in the first regional game. It sucked. Pana was much more fun. I wouldn’t have regretted staying home.

In other news, I still find myself unable to find even basic documentation on Ruby that isn’t outdated. I really want to get all the information I can on this language (even though I believe Python to be the “right choice,” which I’ll go into). I need to get my hands on the second edition of Programming Ruby: The Pragmatic Programmer’s Guide but it seems as if no one is.. ahem.. distributing it. I don’t have the $30-45 to shell out for it.

I still believe Python to be more elegant. Maybe it’s just me, but:

[x for x in li if os.path.isdir(x)]

just looks more natural than:

li.collect do |x|
    x if File::directory? x
end

And even so, with the first you’d wind up with everything that’s a directory. With the second you’d wind up with everything that’s a directory, and everything that’s not a directory replaced with nil. I see no way to prune this, either.

Update: You can apparently prune it with this:

li.delete_if do |x|
    x.nil?
end

But that’s still an extra step. A filter iterator would be more apt than collect/map in this case.

Update: Okay so I was tired when I posted that. Obviously you could just do:

li.delete_if do |x|
    !File::directory? x
end

My bad.

Anyway, back to having nothing to do.

Someone buy me this.

-- rakaur // 2004.10.30 @ 09:26 PM

Web Design With a Side of Potatoes

[ rakaur on Sat Oct 09 at 01:33 PM // category: life, programming, technology, web ]

It’s that time again!

I need a new layout for my sites (namely this one). I realize I haven’t had this one up very long; but, I’ve been using this layout for quite some time. So, if any of you layout-desiging fools out there would care to take a crack at it, I need:

  1. Light on dark (ie, white text on some dark-but-not-black color);
  2. A nice font that doesn’t look crappy like this one (eg, verdana);
  3. A simple “liquid” layout (ie, horizontally centered, sidebar, content);
  4. A main menu, and submenus for sections that need them.

I’ve been messing with it, but you know, I suck ass at layouts.

That reminds me, I need to figure out: Python or PHP. It seems mod_python is broken on the latest Apache, so I’m not sure. PHP is geared towards Web stuff, but you know, I hate PHP. Python is just so much cleaner and organized; and, for the simple stuff I do it’d work just fine (in fact, I already wrote a Python version of my blog). The only bad part about using Python is that there’s no (decent) Markdown implementation for it. There’s a few floating around on the Web, but they all do something improperly and they’re so hacked up in a Perlesque manner that I can’t figure out what to do with them. Porting that will suck.

Anyway.

We went up to Bunker Hill last night to shoot the potato gun. Holy shit, it’s loud. I mean like, it rivals something that uses gun powder as opposed to air and starting fluid. It doesn’t shoot quite as far as I thought it would, but it’s still pretty neat (and fun to shoot when it actually works).

I might have to get me one of those.

-- rakaur // 2004.10.09 @ 01:33 PM

The Python Rant

[ rakaur on Mon Oct 04 at 07:45 PM // category: programming, technology ]

I’ve posted a rant on things I don’t like about Python.

I’ll post updates here.

On a side note, I got moved right the hell next to the girl I want in one of my classes. Perhaps I’ll accomplish som… no, no I won’t.

-- rakaur // 2004.10.04 @ 07:45 PM

Regular My Ass

[ rakaur on Fri Oct 01 at 11:20 PM // category: programming, technology ]

Here’s the RE my friend and I wound up with:

pattern = r"""
           ^                  # beginning of string
           (?:                # non-capturing group
               \:             # if we have a ':' then we have an origin
               ([\w\W]+)      # get the origin without the ':'
               \s             # space after the origin
           )?                 # close non-capturing group
           (\w+)              # must have a command
           \s                 # and a space after it
           (?:                # non-capturing group
               ([\w\W]+)      # a target for the command (can contain '#' etc)
               \s             # and a space after it
           )?                 # close non-capturing group
           (?:                # non-capturing group
               \:             # if we have a ':' then we have freeform text
               (.*)           # get the rest as one string without the ':'
           )?                 # close non-capturing group
           $                  # end of string
           """

It allows me to remove the code that deals with removing the ‘:’, which is another speed up because string manipulation on any level in Python is mind-blowingly expensive.

-- rakaur // 2004.10.01 @ 11:20 PM

Regular Python IRC

[ rakaur on Fri Oct 01 at 08:12 PM // category: programming, technology ]

Definitely owning in IRPG:

  1. rakaur, the level 52 destroyer of worlds. Next level in 7 days, 15:00:56.
  2. sycobuny, the level 51 CockGrabber. Next level in 12 days, 23:52:30.
  3. rintaun, the level 50 ultimate lego warrior. Next level in 0 days, 01:03:37.
  4. madragoran, the level 50 gaidin. Next level in 10 days, 01:27:34.

In other news, I wrote a twelve-line IRC protocol parser in Python earlier. Actually, I wrote several. First I wrote one a la C, via string manipulation and concatenation, etc. Then I timed it parsing two million lines of IRC protocol (not realistic: it was the same two lines a million times). Then, I wrote one using a regular expressions. I immediately thought the RE one would be slower. However, after I wrote and looked at it I realized it was less than half the code to implement. When I timed it, it was only three seconds faster. So, I moved the re.compile() call to a global so it’s only compiled once per program run, and that shaved off about 30% of the time. Then, I fixed the argument vector counting and that shaved off about 10% of the time. All in all, the RE one was 60% faster than the string manipulation one (maybe because strings are immutable in Python and thus the interpreter has to build a new string for every operation).

I can tell you’re all quivering in anticipation, so here’s the code. I’ll paste the RE first (as of now; when my friend gets home I’ll ask him if there’s anything to optimize it as he knows far more RE than I do):

import re

# A regular expression to match and disect IRC protocol messages.
# This is actually 60.501% faster than not using RE.
pattern = r"""
           ^             # beginning of string
           (:\w+)?       # if we have an origin it starts with a ':'
           \s?           # space between words
           (\w+)         # there has to be a command
           \s            # space between words
           (\W?\w+)?     # if the command has a target it might be a #channel
           \s?           # space between words
           (:.*)         # anything after a ':' is one string
           $             # end of string
           """

pattern = re.compile(pattern, re.VERBOSE)

And here’s the parser:

def parse(self):            
    """Parse IRC protocol and call methods based on the results."""

    global pattern

    # Go through every line in the recvq.
    for line in self.recvq:
        parv = []
        parc = 0

        # Split this crap up with the help of RE.
        origin, command, target, message = pattern.match(line).groups()

        # Chop off the leading ':' on `origin` and `message`.
        if origin:
            origin = origin[1:]

        message = message[1:]

        # Make an IRC parameter argument vector.
        if target:
            parv.append(target)
            parc += 1

        parv.append(message)
        parc += 1

… and that’s about all I have for you today.

-- rakaur // 2004.10.01 @ 08:12 PM

« programming: September 2004 | Main Index | Archives | programming: November 2004 »