[Welcome] [TitleIndex] [WordIndex


The set of legal Python identifiers is a subset of the set of legal URI path component names. Thus, "/1foo/2bar/foo-bar" is a perfectly legal (part of a) URI, but neither "1foo", nor "2bar", nor "foo-bar" are legal Python identifiers; that is, attempts to define classes, functions, methods, or variables with leading digits or containing dash/hyphen characters (or spaces, for that matter) will result in SyntaxError.

So if you want or need to use these illegal Python names as components of Quixote URIs, what can you do?


You have a few options:


Let's make a URI for a Quixote app with an illegal Python name, something like "http://foo/rdf-model".

Providing a mapping in _q_exports

_q_exports = [
    ("rdf-model", "rdf_model"),

This is probably the cleanest solution.

Using _q_lookup

Another solution is to define a _q_lookup():

def rdf_model():

def _q_lookup(self, component):
    if component == "rdf-model":
          return rdf_model(request)

That works and is obvious. And Quixote developers suggest that it won't be a performance problem unless your're serving hundreds of requests per second. That being said, it's a bit hackish, since we're not *really* doing a dynamic lookup.

Using _q_translate

Since _q_translate provides the mapping between path components and Python identifiers, this might be the best solution, depending on your application. For example, you could provide a Directory subclass that uses a dictionary for _q_exports. For example:

class MyDirectory(Directory):

    exports = {'rdf-model': 'rdf_model'}

    def _q_translate(self, component):
        return self.exports.get(component)

    def rdf_model(self):

This would be very fast, even for large set of exports.

Python internals

You can always try to muck about with Python internals directly, though I suspect this approach doesn't offer much advantage over the previous two, and it may just make things blow up in your face.


2010-09-22 22:14