I’ve added uri_templates.py to the described_routes-py repository. Feature-wise, it differs from Joe’s in that it supports a partial expansion mode (see this post for motivation). Implementation-wise, it’s all done with regexps – no special parsers required.
There are a couple of missing features (I own up to them in the docstring) but they probably won’t get fixed. Not only do I not need them, but we’re expecting the spec to undergo some some significant change soon and it would probably be wasted effort.
Fun with decorators
For a bit of Python newbie fun, I used a decorator to populate the operator dispatch table:
def operator(name): def save_operator(func): operators[name] = func return func return save_operator @operator('opt') def op_opt(arg, variables, params, encoding, partial): etc
They then get called by:
operators[operator](arg, variables, params, encoding, partial)
Easy! Perhaps this is overkill, but it seems nicer than techniques like calling functions via constructed function names and it might allow new operators to be added from outside the module.
This must be the simplest example of parameterised decorators you’re ever likely to find as there’s no function wrapping involved. The one thing to understand is that
operator('opt') returns a new function that “remembers” the name ‘opt’.