Python embedded like PHP

Jon Ribbens jon+usenet at unequivocal.co.uk
Wed Apr 3 08:18:15 EST 2002


In article <23891c90.0204020047.4d4ae046 at posting.google.com>, Paul Boddie wrote:
> Some common validation cases are numbers, dates and monetary amounts,
> and I've also seen systems which validated measurements. To an extent,
> one can break down complicated fields into collections of simpler
> ones, although some people dislike such interfaces. Nevertheless,
> being able to declaratively associate form fields with the means of
> validation, without the explicit need to code it, could well be
> beneficial, in my opinion.

Well, it could be done as follows:

  class EmailInput(GenericInput):
    def __init__(self, val):
      if not re.match(r"[^@]+@[A-Za-z0-9-]+\.[A-Za-z0-9.-]+$", val):
        raise ValueError, "%s is not a valid email address" % `val`
      self.val = val
    
    def __str__(self):
      return val

The GenericInput class hierarchy could obviously also have any other
class variables or methods which you might find helpful to define
input types.

In jonpy using multiform you would then simply do:

  ...
  class stage0(wt.multiform.Stage):
    ...
    def update_email(self, val):
      try:
        self.container["email"] = EmailInput(val)
      except ValueError:
        self.errors.append("Sorry, the email address you entered is not valid.")

... and there you have it. I don't see how you could get much more
automated than that, really.

Cheers


Jon



More information about the Python-list mailing list