Uncategorized

Django-admin and single-item tuples

Today I learned something about Python that I did not know before: single-item tuples must have a trailing comma.

I mentioned before that certain aspects of Django leave a bad taste in my mouth, and that I was looking to other solutions as I move my language of choice for web development from PHP to Python. However, in the end I really couldn’t ignore my own advice, and I’ve been getting to know Django a little better, mostly for all the batteries included (user authentication, for example). I kinda like it. The automatic admin generator, for instance, is really nifty; at work, it lets to get the non-programmers in the other room up and running before the project is really anything more than a handful of classes.

The admin site is dynamically generated from your models, and has a concept called fieldsets: logical groupings of fields in the admin form. From the docs, here’s an example:

[code language=”python”]
class FlatPageAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
‘fields’: (‘url’, ‘title’, ‘content’, ‘sites’)
}),
(‘Advanced options’, {
‘classes’: (‘collapse’,),
‘fields’: (‘enable_comments’, ‘registration_required’, ‘template_name’)
}),
)
[/code]

Basically, a tuple of tuples containing the fields and other options. Anyway, while I was creating my admin class, I kept getting a weird error:

Where the name of the field in question starts with the letter ‘x’. I was initially frustrated because the exception message isn’t very helpful, but it turns out, this isn’t so much an issue with Django as it is a gotcha in the way tuples work. My admin class contained a fieldset with only a single field, like so:

[code language=”python”]
fieldsets = (
(‘Advanced’, {
‘fields’: (‘xylophone’)
}),
)
[/code]

Okay, ‘xylophone’ is a little contrived, but my example error from earlier started with an ‘x’. Shoot me. Anyway, after RTFM, it makes perfect sense. Since my single-item tuple lacked a trailing semicolon, it wasn’t a tuple at all, but an expression. And the expression ('xylophone') returns what, class? The string xylophone. Very good! Then the interpreter was trying to iterate over a string, only there is no field that matches the first item returned: x. Hence, an error.

This is why I find actual fieldwork with a new language, framework or library to be the best way to learn it, over reading the documentation or sourcecode or anything else. Now that I’ve learned that little tidbit of information, it’s mine and I’m unlikely to make that mistake again (or if I do, I’ll recognize it instantly when I see it). No amount of documentation-reading could possibly make it stick in my head like that.

Uncategorized

Post Mortem: March Blogathon

So I mentioned before that I blogged every single day in March, and that I would be writing about it. I’ve either got to stop making promises on timelines or get better at estimating them, but here we are a few weeks after the fact… my post mortem for the March blogathon. A good post mortem has four things: a summary of activity, a review of what went right, a review of what went wrong/what could be done better next time, and a conclusion. So, without further ado: ### Summary Thirty-one days of blogging, about various programming/software development-related topics. At least, that was the intent. I spent about a week beforehand brainstorming topics to write about; most of them I hit on, a few I didn’t feel like I had time (more about that later). ### What went right I successfully posted every day, even if some of the posts were very short. I managed to put out what I feel is helpful information, mostly toward the beginning of the month. Also, putting thoughts in a public space turned out to be a _great_ sounding board. ### What went wrong Switching jobs in the middle of March didn’t really help with putting as much thought and effort into my posts as I would’ve liked: things got _really_ busy, and the amount of energy I was able to put into blogging definitely suffered. Which wouldn’t have been so bad, except that being on a 24-hour schedule doesn’t leave a lot of time to develop ideas or do research. Most nights, I found myself starting to write at 10pm, rushing to get something semi-coherent out before midnight, and going back after to edit and revise. There are a few topics that I wanted to hit (backbone.js, for one) that I didn’t get to, because I knew the subject would suffer in the amount of time I could give it. ### Conclusion All in all, it was a lot of fun, a little stressful, and worth it. I’d hoped that doing a blogathon would give me some exposure on the interwebs, and it seems to have done that… thanks to everyone who reads and/or comments regularly and all the insight that you provide, you people are great. I’m leaning toward a Monday/Friday update schedule from here on out; frequent enough to merit coming back on a regular basis, but with enough space in between to let me put some thought and effort into my writing (or at least, get _something_ out on the weeks where work is hell). The tentative plan at this point is to go back through and find posts that I thought had a good potential but could’ve been better, and give them more love… since I’ve been diving headfirst into both the Python and JavaScript worlds, I should have a lot to say on new subjects as well ([flask](http://flask.pocoo.org/) is great, doncha know).

Uncategorized

State of the union

After completing my March blogathon (I posted every day for a month… go me), I’m taking a break this week, and then I’ll be back with a Monday/Friday schedule.

I’m currently writing up a post-mortem on what I learned from the exercise, so you can expect to see that on Monday. And if you have any topics that you’d like to see after that, you’re always welcome to drop me a line at djstobbe@gmail.com.