Entries in category “Python”

36 entries in this category. See also: Feed icon feed of entries in this category, list of all entry categories.

How Python does Unicode

As we all (hopefully) know by now, Python 3 made a significant change to how strings work in the language. I’m on the record as being strongly in favor of this change, and I’ve written at length about why I think it was the right thing to do. But for those who’ve been living under a rock the past ten years or so, here’s a brief summary, because it’s relevant to what I want to go into today:

In Python 2, two types could be used to represent strings. One of ...

Entry published September 5, 2017. Read full entry.

Let’s talk about testing Django apps

For quite a while now, I’ve maintained several open-source Django applications. And with Django 1.11 on the horizon, it’s time for me to go through and make any changes and clean out their issue queues while I’m at it, so I can roll new officially-compatible releases. When I’m doing this, I always like to try to evaluate the current state of the art and see if I can switch over to better ways of doing things, since Django gets a new features with every release and people in the ...

Entry published April 3, 2017. Read full entry.

How to break Python

Don’t worry, this isn’t another piece about Python 3. I’m fully in favor of Python 3, and on record as to why. And if you’re still not convinced, I suggest this thoroughly comprehensive article on the topic, which goes over not just the bits people get angry about but also the frankly massive amount of cool stuff that only works in Python 3, and that you’re missing out on if you still only use Python 2.

No, this is about how you as a developer can break Python, and break it ...

Entry published November 28, 2016. Read full entry.

On Python 3, again

A while back, Brett Cannon went into some detail on why Python 3 happened (that is, why it was backwards-incompatible with Python 2 and why it was backwards-incompatible in the particular ways it was). Python 3 has been pretty controversial, with people I respect chiming in on both sides of the good idea/bad idea debate. And the transition (which Brett has also taken a recent look at) has indeed been slow, but that at least was fully expected — at the time, I remember announcements coming from the Python ...

Entry published June 10, 2016. Read full entry.

Things and stuff

I’ve been awfully busy lately, but also thinking about a lot of stuff. Since microblogging is no longer really a thing I have access to (Pownce, I miss you), that means doing periodic brain dumps, yay!

Conferences and costs

Over the past few months there’s been a pretty significant conversation starting about tech conferences and “paying” speakers (where “paying” typically means at least providing a free ticket to anyone giving a talk). Which is a topic full of complicated thoughts for me.

On the one hand, the obvious argument: there’s significant overlap ...

Entry published May 5, 2016. Read full entry.

A real Python “wat”

A few weeks ago I went through and explained the various items in a list of “Python wats” — behaviors of Python which seemingly made no sense. Calling them “wats” is a bit of a stretch in most cases, though, because most of them were simply consequences of fairly reasonable design decisions in how Python or its standard libraries work, but presented in ways which obscured what was actually going on.

Lest I be accused of defending Python too much there, I’d like to point out an absolutely genuine “wat” moment ...

Entry published November 15, 2015. Read full entry.

Wat’s up, doc?

In much the same vein as Gary Bernhardt’s wonderful talk about JavaScript, there’s a collection of Python “wat” moments which goes around every so often. There’s also an associated quiz linked from that page (which I won’t spoil; you can read through it yourself and then check your answers). Every language has some unintuitive — or at least seemingly-unintuitive — bits, and Python is no exception. But if you’re working in Python, understanding why (or, perhaps more appropriately, wy) these snippets of code behave the way they do is interesting ...

Entry published October 13, 2015. Read full entry.

django-registration updates

Since announcing its resurrection a couple weeks ago, I’ve been working on django-registration pretty much continuously. There are over 30 commits since that last post, and I think it’s finally getting close to release quality; what’s left at this point is, I think, mostly stylistic cleanups and editing and pushing some more documentation.

There are a couple big highlights from the past few weeks’ work, and I’d like to take a moment to go over those in a shameless attempt to build up hype and excitement for the forthcoming release.

No more ...

Entry published September 6, 2015. Read full entry.

django-registration

I mentioned a couple weeks ago that lately I’ve been in the process of providing updated releases for all the various open-source projects I maintain, and specifically mentioned a desire to resurrect django-registration which, once upon a time, was the most popular thing I’d ever written. Over the past week I’ve been focusing more heavily on that, and now it’s time to start talking about a release.

Ancient history

I’ve always felt pretty strongly that Django’s killer feature is its concept of an application; the ability to wrap up a chunk of ...

Entry published August 13, 2015. Read full entry.

A couple quick tips

As noted the other day, I’ve spent the last little while working to freshen up various bits of open-source code I maintain, in order to make sure all of them have at least one recent release. Along the way I’ve picked up a few little tips and tricks; some of them may be old hat to you if you’ve been lucky enough to be working with fairly modern Django and Python versions for a while, but I think they’re still worth sharing. Today I’d like to dive into two in ...

Entry published July 22, 2015. Read full entry.

Personafied

Reminder for people who try and think it’s a bug: Persona, on this site, is for me to be able to log in and post entries. As such, you will not be able to log in to this site, since you don’t have an account and can’t create one. This isn’t a bug, it’s intended functionality — site owners can control whether accounts can be created, and by whom.

So, last week I mentioned in passing that my next project for this site would be implementing Persona for authentication. Since I ...

Entry published September 5, 2013. Read full entry.

Catching up

For a while now I’ve been pretty embarrassed by this site. Not by the visual design, or the functionality (though some bits have been lost along the way, for reasons that will become obvious in a moment), but by the fact that it was old. As in, over five years old. It was running on a bunch of ancient code that I’d written a long, long time ago, on an ancient Subversion checkout of Django — for the historians in the audience, it was pre-queryset-refactor — and was, frankly, not ...

Entry published August 26, 2013. Read full entry.

django-FUNserver

Step 1: pip install django-funserver

Step 2: Add “funserver” to your INSTALLED_APPS.

Step 3: Run manage.py funserver.

For best results, don’t do this on a real server.

Code is here, for those interested.

Entry published November 1, 2010. Read full entry.

Let’s talk about WSGI

Recently Armin Ronacher (whose blog you should be reading if you do anything at all involving Python and the web) has published a couple of good articles poking at the current state of WSGI, the standard interface for Python web applications. Some of his comments dovetail nicely into concerns I’ve been trying to put into words for a while now, so I’m glad he’s posting on the subject and providing some context.

In short, I’ve come to have some rather severe misgivings about WSGI — both as currently constituted and as ...

Entry published August 10, 2009. Read full entry.

When licenses attack

Jacob posted twenty questions about the GPL. Zed followed with an explanation of why he placed Lamson under the GPL. This has provoked some discussion around the internets, some of which I have read and some of which I’ve ignored.

Unfortunately, there’s a lot of noise drowning out useful discussion, much of it centered around alleging — directly or indirectly — that if you ask questions about how the GPL interacts with other licenses, you must be trying to “get around” the GPL, or take someone’s GPL code and use it ...

Entry published July 14, 2009. Read full entry.

A fun little bug

About two months ago, I quietly wrote and released a little Django application which generates cross-domain policy files for Flash (if you’ve never encountered this aspect of Flash, here’s an introductory article on how it works). I’ve done a bit of hacking on it in my spare time, and I’m pretty happy with it as an example of a simple, tightly-focused application; sometime soon I hope to write a bit more about the development process behind it, because it provides a useful real-world guide to some of the abstract best ...

Entry published June 26, 2009. Read full entry.

PyCon ORM panel liveblog

I’m sitting about five rows back in the ballroom at the Hyatt, waiting for the ORM panel to begin.

Panel’s starting. Moderator Alex Gaynor introduces himself and panelists:

First question: brief history of your ORM

SQLALchemy: wanted to do a CMS, but never ended up writing it. Had idea for a modular library to talk to databases, different bits you could use or not use — connection pooling, SQL expressions, ORM — as you liked. Background ...

Entry published March 28, 2009. Read full entry.

Slides

For anyone who missed it: I gave a lightning talk during the morning session here at PyCon today, following up on my earlier questions about distributed version control. Slides are available as a PDF.

Next up: attempting to live-blog the ORM panel.

Entry published March 28, 2009. Read full entry.

Why I like pip

So yesterday I explained some of the reasons why I don’t like setuptools. In essence, my objections boil down to one idea: application packaging and application development should be orthogonal concerns. The way setuptools works, however, seems to tend, inevitably, toward coupling them to each other. I gave one example — the way the default behavior of installing zipped packages (an ironic twist: the man who so eloquently explained how Python is not Java has spent so much time and effort trying to implement Java packaging conventions in Python) leads ...

Entry published December 15, 2008. Read full entry.

On packaging

So currently there’s a bit of a to-do involving Debian’s Ruby packaging team and the Ruby “gem” system. This document does a good job of summarizing the issues from Debian’s perspective. And of course, the Ruby side of it is no less heated; an example is here.

A lot of this is the usual back-and-forth between (on the one side) application developers working in one particular language, who want to distribute their applications to the widest possible audience and so use an operating-system-agnostic but language-specific tool for doing so, and ...

Entry published December 14, 2008. Read full entry.

Let’s talk about Python 3.0

There’s an old joke, so old that I don’t even know for certain where it originated, that’s often used to explain why big corporations do things the way they do. It involves some monkeys, a cage, a banana and a fire hose.

You build a nice big room-sized cage, and in one end of it you put five monkeys. In the other end you put the banana. Then you stand by with the fire hose. Sooner or later one of the monkeys is going to go after the banana, and when ...

Entry published December 5, 2008. Read full entry.

Another take on content negotiation

Today my co-worker Daniel posted a class which performs content negotiation for Django views, allowing you to write a single view which returns any of several types of responses (e.g., HTML, JSON, XML) according to the incoming HTTP Accept header. While it’s certainly cool, he notes a couple of issues with it (including the redundancy it introduces in URL configuration).

So let’s see if we can’t come up with a way to work around this.

Holiday decorations

It seems that the best way to approach this is to avoid the need ...

Entry published November 29, 2008. Read full entry.

Writing custom management commands

The other night in the #django-dev IRC channel, Russ, Eric and I were talking about custom management commands for certain types of common but tedious tasks; Eric was discussing the possibility of a command for automatically generating a tests module in a Django application, since he’s our resident unit-testing freak, and I started toying with the idea of one to generate basic admin declarations for the models in an application.

So I sat down and spent a few minutes writing the code. I haven’t decided yet whether I want to clean ...

Entry published November 14, 2008. Read full entry.

So you want a dynamic form

So I havent really been doing much writing lately. That’s mostly a consequence of the fact that:

  1. Django 1.0 was released, and meeting the schedule for that took up an enormous amount of time.
  2. After that, there was DjangoCon.
  3. Oh, and there are all sorts of things in my life, including a book, a gigantic codebase and dozens of sites, which all need to be updated. Our entire team here in Lawrence has been running short on sleep for a good long while now.

Anyway, I’m going to force myself to break out ...

Entry published November 9, 2008. Read full entry.

Let’s talk about documentation

One of the most active threads on reddit’s programming section right now discusses things people look for when reviewing someone else’s code; the article being discussed treats this as a great interview question and points to things like algorithm choices and object-oriented design as good responses. While these are important considerations, I’ve found I tend to make snap judgments long before I get to that level of analysis, and they’re almost always based on one key factor: documentation.

Of course, I have the luxury of mostly reviewing Python code, and ...

Entry published June 21, 2008. Read full entry.

Batteries sold separately

At first glance, Google’s App Engine looks like a great way to build the next big web application; you get access to a massively scalable infrastructure, you get access to a huge existing authentication system, you get baked-in stats, you get all sorts of cool goodies.

Oh, and you get Python, which is a great language for writing web applications, and I’d be remiss if I didn’t take some pleasure in Django being available out of the box.

Personally I don’t really care one way or another about hosting code with Google ...

Entry published April 8, 2008. Read full entry.

Where to find me at PyCon

My flight arrived in Chicago a while ago, and despite an attempt by the hotel to screw it up, I’m currently sitting in my room enjoying a beer before bed. Here’s my plan for the next few days:

Outside of these predictable time slots, I’ll probably be all over the place; I haven’t yet made up my mind ...

Entry published March 12, 2008. Read full entry.

Managers versus class methods

In the triumphant return of “James answers questions from the django-users list”, today I’d like to take a few moments to talk something that’s recently become something of a hot topic, spawning not one but two threads, as well as a couple off-list email discussions: what, exactly, is appropriate to put into a method on a custom manager as opposed to, say, a class method, and when and how can you tell?

This is a somewhat tricky question to answer, since there’s a substantial grey area where personal preference will be ...

Entry published February 25, 2008. Read full entry.

On “conceptual integrity”

Mark Ramm has a nice post this morning talking about Adam Gomaa’s discussion of “conceptual integrity” in Python web frameworks; I think it’s worth a read.

The key point Mark touches on is good documentation: ensuring that people see and understand the overarching structure of the framework is, ultimately, the key to helping people understand how it can help them get their work done. In the case of Django there is an official “overview” document, but it’s really not as good as it could be; it’s basically a whirlwind tour of ...

Entry published February 11, 2008. Read full entry.

Making magic

In yesterday’s article I spent a fair amount of time talking about the word “magic”, specifically in the context of Clarke’s Third Law, which states that

Any sufficiently advanced technology is indistinguishable from magic.

A big part of what I was getting at was that a lot of things which seem to be explicable only by appealing to “magic” are really just cases of technology — sometimes extremely simple technology — being used in a complex way. Or, to borrow an excellent turn of phrase from Terry Pratchett, “ninety percent of most ...

Entry published December 3, 2007. Read full entry.

Be careful with your URL patterns

Tonight in the Django IRC channel, someone stumbled across a seemingly-odd error when trying to use a generic view:

TypeError: object_list() got multiple values for keyword argument 'queryset'

The problem turned out to be the URL pattern which was routing to the generic view. Consider a simple example, as might be found in a weblog application:

from django.conf.urls.defaults import *
from weblog.models import Entry

info_dict = {
    'queryset': Entry.objects.all()
}

urlpatterns = ('',
    (r'^(index|weblog)/$', 'django.views.generic.list_detail.object_list', info_dict)
)

The idea here is that either of two ...

Entry published October 14, 2007. Read full entry.

PyCon 2007: Web frameworks panel

(this was liveblogged during the panel, and the only later edits were to correct a couple of typos and add this notice — the content has not been changed)

Titus, the moderator, has welcomed everyone. No blood shed so far. Probably not taking audience questions.

Introduction of the panelists:

Titus pauses: “The most interesting thing about Django is, it’s what Guido would do.”

Titus says he’ll spark discussion by targeting Zope. First question: there’s a wide ...

Entry published February 23, 2007. Read full entry.

PyCon 2007: the prelude

So, I’m in Dallas, eagerly awaiting the beginning of the non-tutorial bits of PyCon 2007; I got here around 4:15PM (after a lovely flight where I was inadvertently bumped to first-class), and already things are rocking; I just got back from dinner and drinks with some folks who are doing really cool stuff with and to Django. Already got some interesting ideas and things to continue fleshing out, and I’ve got another four days to spend here.

If anyone’s looking for me, I’m lounging in my room in the Courtyard ...

Entry published February 22, 2007. Read full entry.

Python framework design

Lately I’ve found myself being baited into the same old debate over and over and over again, and I’m getting tired of making the same arguments each time. Usually it begins with someone lamenting how Django is anti-community or too inflexible or generally suffering from a raging case of NIH. From there it progresses into people proclaiming how TurboGears or (more often) Pylons is objectively “better” because of how they’re designed, and how it would be nice for Django to follow their lead.

Before I go any further, I’d like you ...

Entry published February 19, 2007. Read full entry.

Programming tips: learn optimization strategies

Recently I spent a little time talking about the tradeoffs between “concise” code and readable code in Python. Throughout that entry, I was using as an example a simple function which calculates numbers in the Fibonacci sequence; here’s one variation:

def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

The Fibonacci sequence is a classic example from introductory programming materials, because it teaches recursion, and recursion is an important core concept for productive programming. But it can also be useful for another educational topic ...

Entry published November 5, 2006. Read full entry.

Python tips: don’t be too concise

There’s an inherent tendency programmers have to take a piece of code and reduce it to the shortest possible form. The holy grail is, of course, cutting something down to a single line of code while still providing the same functionality; reducing a particular piece of code to a “one-liner”, especially if the code is somewhat complex, is sometimes viewed as a measure of a programmer’s intelligence or talent or both, and is often used as a sort of game (see Perl Golf, for example — the cult of the ...

Entry published October 28, 2006. Read full entry.