My first user group presentation

It’s pretty hard to believe that the entire summer has gone by, and I’ll be coming home in less than two weeks. But before that, I did something brave! On Monday, I went way outside my comfort zone and gave a talk at EdmontonPy, the local Python user’s group. The talk was about a specific metaprogramming feature in Python called decorators. If you know what that means, or think you’d like to know, you can check out the project page on GitHub, or jump straight into the GitHub repo for extra goodies.

Anyway, I’m not going to reiterate the content of the talk here. The text is online (I even edited a lot of things after the presentation), as is a rough version of my script, and a recording is on YouTube. That basically covers anything anyone could want. I just thought I’d write up my thoughts about the experience.

Inspiration

Towards the end of June, I started wondering whether I should invest more in my programming skills. I was already learning a lot at work, by necessity, but I thought I could be doing more. I searched for “Ottawa Python”, and found Ottawa Python Author’s Group. Obviously, I then checked to see if there was anything for Edmonton - and there was! So I tentatively decided to attend the July meeting, and managed to follow through on it, too (though I came close to turning around a couple times on the way there).

I enjoyed Daniel Nephin’s talk on itertools, and Aden Grue’s talk about Unicode brought up the one or two Unicode pitfalls I was lucky enough not to trip on already. I chatted for a bit with one of the primary organizers, Brian Forst, who also works at the University of Alberta. While I hadn’t asked my supervisors for permission yet, I’d also gotten particularly excited about the idea of going to PyCon Canada. I asked around, and was directed to (I think - correct me if I’m wrong!) Diana Clarke who has been involved in organizing the most recent PyCons. We chatted for a bit about the conference, and she more or less suggested that I might want to wait until next year and apply for funding of some kind. Didn’t matter in the end, though, because the timing was bad (my supervisors would be getting back from month long trips at the same time).

Anyway! So that was all well and good. As I was walking back to the subway station, obviously inspired, I started reading an ebook I’d bought called Guide to: Learning Python Decorators by Matt Harrison. I got the ebook on Amazon fairly cheaply, but I won’t link it ‘cause he deserves a larger cut of the sales. It’s concise, the writing is clear, and organized beautifully.

It’s laid out in such a way that concepts many people struggle with learning - functions as first class objects, closures - seem to just follow naturally from the content of the previous sections. Even though the majority of the book isn’t literally about decorators, the earlier sections make them much easier to understand. Plus, it turns out that closures are pretty simple and they’re actually really useful.

In other words, there may never be a better explanation of decorators than Matt’s book.

Despite the high quality of the book, upon getting home and finishing the book (it’s no longer than it needs to be), I decided I could probably give a talk on the subject. I must have known at the time that I couldn’t possibly present the topic as well as the book had, but I felt like stretching my boundaries, so I sent off an e-mail volunteering to give a talk in August.

I promptly got an e-mail back saying they already had two talks scheduled, but they’d keep me in mind. They had announced the talks for August, right in front of me, and apparently I forgot five minutes later. Oops. But then a few weeks ago I got an e-mail saying the original presenter was too busy to prepare a talk, and they were looking for volunteers. Woo!

Anyway, I’ll post shortly about how it actually went, and then after that will be a bunch of technical notes that will, some day, show up as search results for the people as frustrated as I was when I prepared my presentation.

When I have to install a bunch of Python libraries

wheningit:

hedgehog

Alright, I know this is uncharacteristic for my tumblr these days, but this is pretty much how I feel about dependencies on Windows. All the time. I waste entire life (or at least, entire summer life) messing with dependency bullcrap. All I wanted to do was use C++ to put a picture on the screen, and it became a several hour time suck trying to get CMake to work to install a graphics library (and all the dependencies of its dependencies…). So then I moved to Python and PsychoPy, but the only reasonable way to install PsychoPy on Windows is to download their standalone package that includes Python. So now I have three versions of Python installed (2.7.3, 3.2, and PsychoPy’s 2.6.6) and I have to mess with my PATH to get everything in the right order.

The alternative option was to add their dependencies to my 2.7 installation, but that would be an endeavour worthy of the above .gif times three. Trying to handle the fact that PsychoPy has a dozen (literally, count them) dependencies, the fact that there are three Python package managers (pip, easy_install, setuptools) and none of them seem to work 100% of the time…

It sucks and I much prefer writing code. Even if the code uses the subprocess module and calling kill() doesn’t actually work on Windows so I have to borrow a function that makes calls to the Win32 API… That is better than managing dependencies outside Unix. Because it means the software for running my experiment is almost ready!

for-else construct in Python4

A couple of weeks ago, I agreed to facilitate a workshop on Python for non-programmers in the cognitive science department at Carleton. It’s been alright so far for the first two sessions - about seven-ish people attending, but with wildly varying skill levels. Specifically, one guy is experienced with C/C++ and several others know almost nothing at all about programming. It’s been hard to engage everyone at once. There’s been a lot of “we can talk about this after” and “this is interesting but probably not important to most of you”… especially from me because I get excited and hope they will still understand.

Anyway! This evening I was trying to find good code examples to show in an online Python interpreter that doubles as a visual debugger. Looking through the available examples, under the advanced Python features section, I saw an example called “for-else” (the title of this post links directly to it). Wait… what is that?

As it turns out, this exists in Python. I found a blog post on the topic that shows a useful application of the technique. This can be applied to a for loop or a while loop, and the code in the “else” clause only runs if the loop exits normally. In other words, it runs if you don’t break out of the loop. In a way, it’s kind of like the “else” is attached to all the break statements inside your loop - namely, if none of the break statements are reached, run the else clause. I have a really hard time matching up the idea of this meaning with an interpretation of “else” - to me, this seems more like a “finally” clause that is surpassed by break. finally is used for exception handling in Java, but by definition you can’t pass over it - code in a “finally” clause is run no matter what. Well, I think. I’m sure there are loopholes I don’t remember, probably involving destructors somehow because they’re a source of much evil.

I feel like it’s a solution in search of a problem, which is probably why this isn’t a very well known feature. Off the top of my head, the only thing I use break for is when I’m looking for a single thing in a collection and want to do something with it afterwards - and not do anything with the rest of the collection. continue is a different story - I use continue a lot more often, because looking at an item of a collection and doing nothing with it is a lot more common than finding one item and discarding the rest of the collection.

But hey, there you go, some unique semantics (I think?) in a programming language. If it was really such a great idea, it probably wouldn’t be so rare.