Posts Tagged ‘programming’

hMollom

Friday, February 26th, 2010

You may or may not be aware of the excellent anti-spam service provided by Mollom, founded by two friends and colleagues, Dries Buytaert and Benjamin Schrauwen. Mollom provides an API to program against, allowing support libraries to be written in a variety of languages and for numerous platforms. Top-notch examples are the Drupal plugin (obviously :-) and the WordPress plugin. Next to that there are a number of libraries for Java, PHP5, Ruby, Python, etc. Sadly, a Haskell library was lacking.

Given that I enjoy programming in Haskell and need to sharpen my Haskell-fu, I set out to write a library for interfacing with the Mollom API. The library is far from finished, but it is functional at this point, so if you have Mollom keys, you can call the services and fight the spam war on your Haskell driven website.

I maintain two repositories where you can get the library:

At this point the library has been given version 0.1. So it is still lacking quite a lot of features. Stuff that is on the immediate TODO list:

  • Add fault handling
  • Add server list refreshing
  • Encapsulate the state in a monad

Any feedback is appreciated.

Flemish programming contest

Thursday, January 28th, 2010

On March 17th, 2010, Ghent University organises the second Flemish Programming Contest, in collaboration with the universities of Leuven, Hasselt, Brussels, and Antwerp.

For three different categories of participating teams (of at most three participants), we offer five problems that need to be solved in three hours. The goal is to have at least one fairly easy and one quite hard problem for each category, but as we all know, a problem is only hard when you do not see the solution. So YMMV. Still, we aim to have each team solve at least one problem. For this, teams can use any one of twelve programming languages: C, C++, C#, Haskell, Java, Pascal, PHP, Prolog, Python, Ruby, Scheme, and VB.NET.

Programming should be fun, but to add an extra incentive, we have up to €2500 in prizes. So, without further ado, head over to the contest website and register.

Python XML-RPC needs a facelift.

Friday, June 15th, 2007

I have been experimenting with the Python XML-RPC implementation for a while now, and yesterday, I came across what is most accurately described as a bug. Let’s consider a nice figure to illustrate how the XML-RPC implementation handles things in the Python 2.5 release.

Python xmlrpc madness

So, basically the XML-RPC ServerProxy files a request with the Transport class to deliver the XML goodies to the remote server. However, in the current implementation, Transport uses httplib.HTTP. This is a wrapper class that uses HTTPConnection for most things, but not for receiving responses from the server. And that is exactly where the problem lies. The HTTP.getreply function fetches the HTTP status, reason and headers. But the XML-RPC Transport class does not check the headers for any indication of a content length. When they get the response, things really turn haywire. No matter what, they ask a socket (or a file imposing as a socket) to read 1024 bytes. The socket library tries to comply, but obviously when either the content is shorter, or the connection is closed after the content has been read, an error is raised.

So what are the options to correct this behaviour. I think one can do two things. First of all, fix the Transport function that asks the socket for data to use an extra argument indicating the expected payload size. Obviously, once the headers are received they should be chacked for the presence of a Content-Length field and the requested size should correspond to the value in this length field. I’ve implemented that and it works.

However, I think a second option is perhaps better. Why remain with the HTTP class when a nice and shiny HTTPConnection class is available that does all we need and more? Let’s move the XML-RPC HTTP connection object to that class, and voila! Fixed.

In unified diff format, it boils down to this:

--- /sw/lib/python2.5/xmlrpclib.py      2006-11-29 02:46:38.000000000 +0100
+++ xmlrpclib.py        2007-06-15 15:59:02.000000000 +0200
@@ -1182,23 +1182,13 @@
         self.send_user_agent(h)
         self.send_content(h, request_body)

-        errcode, errmsg, headers = h.getreply()
+        response = h.getresponse()
+
+        if response.status != 200:
+          raise ProtocolError(host + handler, response.status, response.reason, response.msg.headers)

-        if errcode != 200:
-            raise ProtocolError(
-                host + handler,
-                errcode, errmsg,
-                headers
-                )
-
-        self.verbose = verbose
-
-        try:
-            sock = h._conn.sock
-        except AttributeError:
-            sock = None
-
-        return self._parse_response(h.getfile(), sock)
+        payload = response.read()
+        return payload

     ##
     # Create parser.
@@ -1250,7 +1240,7 @@
         # create a HTTP connection object from a host descriptor
         import httplib
         host, extra_headers, x509 = self.get_host_info(host)
-        return httplib.HTTP(host)
+        return httplib.HTTPConnection(host)

On the joys of C

Friday, December 1st, 2006

Half a year ago, I posted about the lack of programming language knowledge the CS students have, and which they require for courses besides the actual programming courses. Yesterday I was assisting at a practicum on operating systems. The assignment was quite easy: add some code to get a working linux module that allows mounting and reading a tarball. To my horror, the following snippets were jotted down in the students favourite editor (pico). FYI, bd is a char*, and points to the header of the tarball. Find the errors. Pick your favourite one.

  String name;

  char *tmp;
  strncpy(tmp, bd[124], 12);

  bd.substring(124,136);

  printk("size = " + this_tar->fileinfo[next_inode].size)

And, oh joy! Who needs compiler warnings or errors anyway? Crap spouted to the screen can be safely ignored until we get a kernel oops, or better, a automatic reboot.

We really, really need to teach these kids something besides Java. There is supposedly a C course compiled by students that they very urgently need to read!

WordPress comments functional again

Friday, September 8th, 2006

I fixed the bug that disallowed people to post comments. It turned out that two things were causing this. First of all, the machine purportedly running the web server for our department seemingly forwards request to another machine, causing the WordPress stored root to differ from the actual root. In some way, this ensures that the $_POST array is quite empty. Second, in the php file processing the comments, somebody seems to have thought that the $_POST entries were available as globals. Which should never be the case.

So feel free to fill my hard drive space up with your witty, hilarious, or outright hostile comments.

Student Haskell Code

Thursday, June 8th, 2006

Some students refuse to grok even the basics. It saddens me that some do not even try to load their program into a Haskell environment, such as Hugs or GHCi. I’ll not say who wrote the following code, but he/she got very few points. None, in fact. Can you distinguish all the languages thrown together?


78 minimal2 c endconf tempconfs bestc {
79 while ((length tempconfs) != 0)
80 do
81 -- Eindconfiguraties hebben geen verder onderzoek nodig
82 if isFinal (head tempconfs)
83 endconfs = endconfs ++ (head tempconfs)
84 tempconfs = tail tempconfs
85 -- Als een tussenconfiguratie al een hogere kost heeft dan de
86 -- goedkoopste eindconfig, moet er niks voortgerekend worden
87 else if betterCut bestc (head tempconfs)
88 tempconfs = tail tempconfs
89 -- ... en anders berekenen we de volgende configuraties
90 else
91 tempconfs = (tail tempconfs) ++ followers (head tempconfs)
92
93 -- Beste configuratie uitfilteren
94 foreach conf in tempconfs:
95 if (betterCut conf bestc)
96 then bestc = conf
97 return bestc
98 }

Of course, most code submitted was quite good.

Programming Languages or Lack Thereof

Thursday, March 9th, 2006

The first programming languages I was taught were (in order): (Turbo) Pascal, C and C++. During my final year at uni I grew an interest in languages no professor ever spoke of: Perl, Python, Awk, Bash (as far as shell stuff can be called a language). I took a course on functional and logical programming – for which I happen to be one of the teaching assistants at the moment – where I met Haskell and Prolog. While the latter has never managed to grab my interest, the former I really took a liking to. The functional programming course got me looking at Clean, Ocaml, SML, … I must admit I never really got used to the ML’s, but that’s largely because I do not digg the syntax very much.

Somehow it seems a bit sad that there are no real introductions or even hack sessions for the alternative (in the mainstream opinion) languages such as Python, Perl or even Ocaml or Clean. Even PHP is skipped, AFAIK. There are some introductions given by students to students, but it would be better if there was at least some structured course that would introduce at least a few of these languages, say Python, PHP and Haskell (the functional programming course is dying with the new Bachelor/Master system).

I think that most programming languages can contribute something essential to the programmers toolbox. (Even Visual Basic. Like how not to do it.) However, the last years the focus of the Computer Science courses has been more and more committed to Java. Java for starters, Java for Datastructures and Algorithms, Java for this, Java for that. I am the first to admit that Java has its uses, and that when used in the right environment it’s even quite cool, but why drop everything else in favour of Java? Because the industry demands it? Should the university not deliver computer scientists with a broad spectrum of academic qualities and with a broad range of programming languages they at least know the basics of? Of course they should be proficient in one or two languages, but they should at least have some knowledge of others. At least they should know what language is best suited for a particular task. Java is not a text-processing language, whereas Perl is. Right now we seem to deliver Java bees. Who can explain to these people what a pointer is? When the next mainstream, overhyped language rolls along, will we teach only that language?

Maybe we should teach people some brainfuck, whitespace and friends.