import sys from quixote import get_response, redirect from quixote.directory import Directory from quixote.errors import TraversalError def fact(n): f = 1L while n > 1: f *= n n -= 1 return f class IntegerUI(Directory): _q_exports = ["", "factorial", "prev", "next"] def __init__(self, component): try: self.n = int(component) except ValueError, exc: raise TraversalError(str(exc)) def factorial(self): if self.n > 10000: sys.stderr.write("warning: possible denial-of-service attack " "(request for factorial(%d))\n" % self.n) get_response().set_content_type("text/plain") return "%d! = %d\n" % (self.n, fact(self.n)) def _q_index(self): return """\ The Number %d You have selected the integer %d.

You can compute its factorial (%d!)

Or, you can visit the web page for the previous or next integer.

Or, you can use redirects to visit the previous or next integer. This makes it a bit easier to generate this HTML code, but it's less efficient -- your browser has to go through two request/response cycles. And someone still has to generate the URLs for the previous/next pages -- only now it's done in the prev() and next() methods for this integer.

""" % (self.n, self.n, self.n, self.n-1, self.n+1) def prev(self): return redirect("../%d/" % (self.n-1)) def next(self): return redirect("../%d/" % (self.n+1))