[mod_python] How to do multi-level dispatch with Vampire

Graham Dumpleton graham.dumpleton at gmail.com
Thu May 3 21:31:41 EDT 2007

On 04/05/07, Jeff Hinrichs - DM&T <jeffh at dundeemt.com> wrote:
> with mod_python + vampire what is the proper way to dispatch something like:
>  /jobs/view/7
> I'm thinking of this idiom since it is common in some other frameworks,
> def _jobs_view(req,path):
>     return 'using _jobs_view'
> def _jobs(req, path):
>     d = path.split('/')
>     if d[1] = 'view':
>         return _jobs_view(req, path)
>     else:
>         return 'unknown action'
> job = vampire.PathInfo(_jobs)
> I'm trying to make a RESTful api, i.e. /jobs/view/N, /jobs/edit/N, etc

>From memory and thus untested, but best to use an object instance to
create the URL hierarchy for the 'job/view' component of path, then
use PathInfo wrapper.

  def _view(req, path):
    return path

  def Node: pass

  job = Node()
  job.view = vampire.PathInfo(_view)

or being more explicit by creating a class for the actual concept.

  class Job:
    def _view(self, req, path):
      return path
    view = vampire.PathInfo(_view)

  job = Job()

If using version of Python with decorators, sure you could come up
with a decorator which allowed you to say:

  class Job:
    def view(self, req, path):
      return path


More information about the Mod_python mailing list