DOIT is now on twitter!

Posted by knorby on December 1, 2008 under Python, internet | Be the First to Comment

I was in need of a break from work, so I finally got around to implementing DOIT of the Day on twitter! For those not familiar with DOIT jokes, they are a set of jokes from USENET, or even before that. A while ago, I started maintaining a fortune database, which I wrote about here. I haven’t gotten around to packaging it up for various Linux distros yet, but I will eventually. So, you can now get your daily fill of DOIT jokes from twitter. Non-twitters can DOIT with RSS.

I wrote a couple of python script/libraries to auto-follow followers (CPAN was having some problems, so I didn’t use a perl script someone else had already written) and a fortune->twitter script. I will clean those up at some point soon and put them up somewhere.

IQs and the Internet

Posted by knorby on November 26, 2008 under Python, google, internet | Be the First to Comment

After reading the comments on a story on reddit on IQs, I became curious about how IQs are reported on the internet. A few people were saying that when they see someone mention their IQ on the internet, it is usually above 130. The explanations given were along the lines of people lying, biased online tests, and segmentation in where people browse. I was curious what sort of frequencies the different IQs are mentioned, so I wrote up a little python to get the google search results for IQs 50-199 (I would have included lower values after seeing the result, but I choose to go the scraping route rather than gdata, which ends up getting you blocked by google, something I didn’t know). I ran the number with the word “iq”; I think there may be better queries, but simple seemed good enough. Here are the results, plotted with matplotlib:

I found these kind of surprising. Most of the result counts were around 6 million, but there were a few sharp drops. I was especially surprised by 100 and 130, since, if memory serves, 100 is the 50th-percentile for IQs and 130 is the 99th; I would expect a greater count on these two, since more sites would include those numbers while explaining the scale; instead, there are large drops. Weird. I don’t think there is any connection between these results and anything proposed on reddit either.

Fun with One-liners

Posted by knorby on October 23, 2008 under Python, coding, javascript | Be the First to Comment

I have always enjoyed putting as much work as possible into a line, especially in higher level languages. In the crazy javascript (speaking of which, MochiKit 1.4 was finally release!) system I wrote (still need to put the final touches on that….), I was thrilled when I was able to combine all of the parts of my system into one one line. Since I mainly program in python, list comprehensions and generator expressions make it pretty easy to use one liners a lot, and at least with generators, it often means that is efficent too. Basically, what I am trying to say is that I love the one liner. Last night, while I was bored while doing my discrete homework, I came up with a memoized factorial function one liner. The standard, niave version is straightforward:

fac = lambda n: int(n==0) or n*fac(n-1)

Since I was bored, I wanted to see if I could memoize that expression, and still keep it in one line. Here is what I came up with:

fac_dict, fac = {}, lambda x: ((x in fac_dict or fac_dict.update({x:(lambda n: int(n==0) or fac(n-1)*n)(x)})) and False) or fac_dict[x]

which can be simplified slightly to:

fac_dict, fac = {0:1}, lambda x: ((x in fac_dict or fac_dict.update({x:(lambda n: fac(n-1)*n)(x)})) and False) or fac_dict[x]

There are still issues with recursion depth for large values, but a helper function could probably solve that.

I needed to write something for my blog, as I have gotten out of the habit, and this thing seemed as good as anything.

Eulogy for Two Fried G5s

Posted by knorby on May 6, 2008 under Apple, Python, humor | Be the First to Comment

I wrote this “eulogy” for two computers that apparently got fried (as in electrical surge or something) recently in the Maclab. I intended it for fellow tutors, but I was fairly fond of it. It would probably help to know that these computers were named “python” and “ada.”

We are gathered here today to mourn the deaths of Python and Ada. They lived good, long lives as G5s. Tragically, Brian discovered their charred remains yesterday, which was confirmed today.

Ada always dreamed of being a missile guidance system, but as a G5, it was never able to fulfill its dream. It forgot its dream, and instead spent its life running word, with the occasional bit of matlab and powerpoint here and there. As it felt its final death blow surge, it visualized tracking a laser point until meeting a glorious, explosive end, it quietly whispered “I’m going home!”

Python suffered a far more tragic death. Realizing it was at its final moments, it began to question the meaning of it all:

>>> raw_input(“So this is it? Was it good? Why do I have to die? Where
I am going?”)
So this is it? Was it good? Why do I have to die? Where I am
going?Traceback (most recent call last):
File ““, line 1, in ?
EOFError

Unfortunately, its questions were left unanswered:

>>> raise UnboundLocalError, “Oh Noes!!!!”
Traceback (most recent call last):
File ““, line 1, in ?
UnboundLocalError: Oh Noes!!!!
>>> raise SystemExit
$

Unfortunately, no one was there to catch its exception.

Playing With Ctypes and Ghostscript

Posted by knorby on March 25, 2008 under C, Python, coding, postscript | Read the First Comment

I have been playing around with the ctypes library in python recently. It is a foreign function interface for dynamic linked libraries(DLL)/shared libraries. It works back to python 2.3, and it was added into the standard library in python 2.5. Anyway, it provides a much nicer way to access C libraries then using one of the various methods to create a C extension, though it is a bit slower. As I learned at pycon, one of the benefits is that PyPy (one of the craziest things I have ever seen) can use ctypes. I was thinking of staying for the PyPy sprint at pycon, but I think they were going to work on a pure python implementation of the library, which was far too intimidating to me. I will post about pycon at some point soon.
I guess I find ctypes so special just because it is the first thing in recent history with python where I was really amazed. I first saw it with µTidylib, which is a python wrapper around the HTML tidy library. What got me was just playing with it on the interpreter:

>>> from ctypes import *
>>> libc = CDLL("libc.so.6")
>>> libc.printf("Hello, World!\n")
Hello, World!
14
>>>

Obviously, it is a lot more powerful than this example illustrates, but you get the idea. I have been fixing up some of the filters in the print system at the Maclab over spring break, so I have been working with ghostscript a bit. There is an API for ghostscript, so I have started writing a wrapper library for it in my free time to practice using ctypes, and, well, because one doesn’t exist. I am trying to think of a name that somehow combines ghosts, snakes, and desktop publishing….

Creating Interfaces in Python

Posted by knorby on February 9, 2008 under Python, coding, design | Be the First to Comment

While working on our project for software construction today, my partner and I started to work out an implementation of interfaces in Python. Fortunately, we didn’t end up needing to go to this extreme (it’s a weekend assignment). Interfaces have been proposed for python before, but the changes were never made. Anyway, we worked out a basic strategy to implement interfaces in python. The most common approach for this sort of thing is to just make a base class with methods that raise the NotImplementedError if they should be overridden. For the most basic implementation of an interface, this approach works, but what if we want to put contracts or a docstring test on this method? There is essentially no way to go about such an implementation with standard methods in python. Instead of using the traditional syntax to express inheritance, some function would be needed to implement interfaces. Really, most of we want to get from the interface could be considered “shell” around another function. We just want to put in the guts part of the method, and if the guts are not put in, python should raise an error. The easiest way to implement something like that would be to just maintain two methods. So, for every method foobar(self, arg1, arg2, ...), there would also be a function __foobar(self, arg1, arg2, ....). Perhaps a different convention, such as __Interface_foobar(self, arg1, arg2, ...), would be appropriate as to not interfere with one’s ability to assign __foobar to something else, but these are points are trivial. Ideally, I will come up with some sort of nice interface metaclass or base class and a set of function decorators that would take care of much of the work involved, but written out if full, it would look like

class Foobar(Interface):
    #===============================================================================
    def __init__(self):
        raise NotImplementedError, "__init__ must be implemented by a subclass"
    #===============================================================================
    def foobar(self, aArg):
        """
        This is a function is the shell of an interface method
        """
        if not isinstance(aArg, BarType):
            raise TypeError, "aArg must be a BarType"
        return self.__Interface_foobar(aArg)
    #===============================================================================
    def spam(self):
        """
        Just some regular method.
        """
        return 3

Then after defining some class that implements this interface Foobar, do something like this


Foobar.implementedBy(eggs)

I will probably have to make some changes once I start banging away at python. I know python well, but I don’t know every bit of trivia.

One of the consequences of this setup is that everything is done at runtime, and some external testing procedure would be needed to verify that everything follows the interface. On the plus side, these would be something unique from interfaces as well; these thing really could be used for a whole bunch of things beyond the scope of normal interface. Also, everything needed to implement this sort of thing could be done without any changes to the language itself or use of modules outside of the python standard library. So, I will work on this thing sometime when I have time, which is never…

My Current Projects

Posted by knorby on January 4, 2008 under Python, coding, javascript, personal | Read the First Comment

This post is really for myself. I started a new project for myself last night, just another simple utility, mostly for my own use, that shouldn’t take me anytime at all. I say that before I start most things, but I think it will probably be true in this case. Anyways, I have a pretty large pile of projects that I have to work on or complete, and I wanted to make a list so that I might be able to judge which ones to work on and to just have a list.

(Mostly) Completed/There is Always More to Do

  • pyXSD – pyXSD was my first big project. It was what I worked on at ORNL as a student intern over the summer of 2006, between high school and college. It was also my first program in Python, my favorite language. It is an XML Schema Validator, which works in some funky ways. It fills a small void in th python+xml world, but it still has a few bugs to work out, and there are a few features I would like to add. It has stayed at 0.1 for some time now, and I don’t know when I will get back into it. I have been kind of hoping for a few people to submit some changes, but only one person has (and that was barely anything). I really just can’t get excited about this project anymore. I don’t use XML Schema (RelaxNG all the way!); I really don’t have a reason to do much more with it. I am guessing it will stay at 0.1 for a while….
  • ACLUofC Website – I mostly included this one, because I didn’t want this section to be one item long. There is really very little to this site. It has a simple design that I am mostly happy with, considering that I did it fairly fast. It needs content still, but I don’t really worry about that one too much.

Almost There, but Still Needs Some Work

  • xmlViewer – My project at ORNL over the summer of 2007. Basically, it is an xml-based expert system to create an editor for a specific xml format. The system is currently written in javascript. It is a really nice system, and there is a lot in it that I am proud of. It just has a few bugs at the moment. I have been planning to write an xhtml editor with it as well, as I figure it could really do some interesting things. It also needs a better name.
  • lambdaCarte – I was working on a project for a class, and I needed a nice command-line menu system in which I could associate actions, which usually are small. I did some quick searching, but I didn’t find any python packages that did just what I needed. I wrote it mostly in a night, and it seems to work surprisingly well. It needs a lot of cleanup, but it works. It’s really simple, but such things just make life easier.
  • DOIT Fortune Library - I am not going to try and justify this thing’s existence here, mostly because I have none. It is not as much a project as it is a mission; I feel it is my duty to make available in any form I can conjure (that doesn’t take that much time and requires no money) to spread a list of bad ambiguous sex jokes from USENET I found when playing on the twenex.org computer. There are few things to work out on what I have so far, but I have been planning to make a DOIT of the day rss feed, and I recently started to look into making a DOIT facebook app. I must DOIT to the world.

Partially Complete

  • xmlTools – I wanted to work with xml from the command line or in scripts, and there isn’t any easy to use toolset that I am aware. It is written in python and it uses the lxml library. A bunch of them work, but it needs further tests, and there are a few functions I want to add.
  • pySchedule – A python scheduling library. I haven’t touched this thing in a while.
  • packagemapWriter – A simple utility to create packagemaps for google code search. It is really a simple utility; I just wanted to something to pass the time over this last break, and I wanted it for some of my projects.
  • deviceXmlFileSync – I have a bunch of gadgets with file storage, specifically mp3 players. I often will get a new album and need to load it on to several different devices, but I don’t remember which ones I have put it on. This python utility helps to queue up transfers. I haven’t worked on it in a while.
  • fireScraper – The project resulting from the method I outlined to screen scrape using Firefox from the command-line. Still has a lot of work to be done, but I have already done much of the needed work.
  • Political Networks – A research job I have.
  • rstDocs – I write a lot of technical documents in reStructuredText (rst). Basically, I wanted something like google docs to write rst on and render it (except just for me and maybe some friends). I started this thing a couple nights ago. It was partly an excuse for me to play around with php.
  • New Print System Interface – I work in the Mac and Linux Lab at the UofC, where we have a print monitor to prevent users from printing things they didn’t write and to enforce a quota. As staff, we have to be able to make exceptions and correct errors. We also have to maintain kiosks so that users can select which jobs they want to print. I started to rewrite it to use ajax techniques as opposed to a simple php script with a page refresh every few seconds. I also wanted to add a few possible actions that maclab tutors and users could use on documents.

Barely Scratched

  • xmlDebt – I didn’t want to use any of the account tracking software I found, and it seemed like something simple enough to code, so I started on one, but I guess my love for accounting is not that great….
  • rsoFundTracker – Simple system designed for RSOs (clubs at UofC). Financially, most have a lot in common, as they all have to fight..err…..deal with ORSCA.
  • cssExtended – Basically, I think css sucks. It would if it was written using JSON instead, and it would be nice if the selectors included XPath and regular expressions. I started outlining this format, and a javascript engine to implement it. I was also thinking of writing a python script to implement it in  the xhtml server-side or to files after any changes are made. I was also thinking of calling it jss.

So now I have a list to remind me of all the things I started so I don’t start more when I want to code. For the ACM, Cord and I have been working on PhoenixForge, a gforge server for UofC students wishing to work on projects on campus. Hopefully, it will cause me to be more productive.

Some Local Python Humor

Posted by knorby on December 2, 2007 under Chicago, Python, coding, design, humor | Be the First to Comment

I follow the local Python user group’s mailing list (see chipy.org). Though UChicago has prevented me from going to all but one meeting, I can say that it is a fun group. Anyways, someone asked a question that spurred a few hilarious responses. The two best are this one bit of coding absurdity and this one just hilarious response.