D520 – Week Five

Chapter 5 of IronPython in Action deals with XML, although it starts out covering some of the more advanced things you can do with functions.  I considered skipping this chapter (the function material is perhaps a bit advanced, and covering XML isn’t a necessity), but decided that it was worth learning about XML in .NET (since it’s so common) and that it would make using the MultiDoc example tricky (since the file format is XML) and I really wanted to use MultiDoc.  I gave the students notes [PDF], again covering the textbook material that we could look at, the tools (unchanged), key points, and a link to the MultiDoc example code.  We had a new lab exercise [PDF] this week (implementing Conway’s Game of Life), as well as two new recommended reading articles: a Lukas Mathis post about preferences and another Spolsky post, this one about “architecture astronauts”.

Students again read the recommended reading (again, before class!).  There was some debate about the Mathis article about preferences.  The points I hoped they took away were the difference between configuration and preferences and that preferences don’t suit ‘casual’ users.  I think some students were unconvinced, but I hope that I managed to get across the point that they are (by definition, as students in this course) more than casual users, and so not the sort of user that preferences don’t suit.  (However, even though I’m far from a casual user, I too generally dislike preferences – one reason I use a lot of Apple products).

I got the feeling that the material on functions was a little advanced, as I suspected.  This was, however, the ideal place for me to cover lambdas, as I proposed doing last week, so I added that in.  I talked about functions defined in other functions, passing functions as arguments, anonymous functions (lambdas), and decorators.  These are important things to learn if you’re going to be a Python developer (or a developer in other languages that let you treat functions in this way), but perhaps belong in a more advanced class.  Next year I might skip over this (i.e. assign it as recommended reading), and just rewrite any of the textbook examples to use regular functions.  On the other hand, it’s great exam question material!

Last week, I stepped through the MultiDoc code, typing it in as I went.  I felt there wasn’t sufficient time for that this week, so instead opened up the source that Michael provides and talked through it.  I don’t feel this was as successful as the previous week, so I’ll go back to typing as much as I can.  I didn’t have time to comment the code as I went, but did so after class (for the version of MultiDoc as it stands at the end of the chapter).  The commented version is available under the same license as the original.  I completely skipped going over xmldocumentreader.py since it’s fairly generic and well covered in the textbook, and concentrated on documentreader.py and documentwriter.py instead.

Implementing Conway’s Game of Life wasn’t an exercise that was in the course in previous years (although it would be essentially the same task to do it in Visual Basic), although I have used it some time ago in other courses (and I think I had to do it myself back in my undergraduate years).  It seemed like an exercise that would concentrate on Python programming in general, and just happen to use some .NET classes.  Two of the classes that they would need (Timer and PictureBox) I expect they’ll want to use in their projects later on.

I particularly like to cover Timer (whether it’s Visual Basic or IronPython) because the students at this point are still working in a procedural fashion, rather than an event-driven one.  That means that their natural instinct when needing to do something like update the grid every X seconds is to have a loop with a time.sleep() call in it.  They then end up with an unresponsive GUI, because they are blocking the main thread.  A timer isn’t the only way to deal with this, but it’s a good default choice, and covering it explicitly saves me explaining it to many students individually when they come across it (e.g. in project work).

However, most of the class found that they didn’t know where to start (and were either so stuck that they really had no idea, or just didn’t feel like taking a crack on this particular day).  So I ended up working through the exercise with them (rather like I ended up doing the Quiz the week after that exercise).  I don’t mind doing this occasionally, but the students really need to start on their own eventually.  I could perhaps provide a detailed design next year, that clearly showed what functions you would use and how they would be tied together.

I wrote three versions of Life in the end (all extremely similar).  I wrote my first one simply to check how difficult it would be for the students, intending it to be an example answer.  The second was a small modification of the first, which added a bit more colour to make the display more interesting.  The third was the one I wrote during class – it’s considerably simpler (i.e. not as nice code) than the other version.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: