[TransWarp] Constraints on model attributes

Phillip J. Eby pje at telecommunity.com
Sun Jul 27 15:29:00 EDT 2003

At 12:01 AM 7/27/03 +0200, Roché Compaan wrote:

>Are type constraints actually enforced, because I just created a very simple
>class with a model.Attribute with referencedType set to model.String and
>no exception was raised when I assigned an integer to it. Mmm, it seems
>you have to define normalize:
>     def mdl_normalize(value):
>         """Return 'value' normalized to the type, or raise error if invalid
>         If a type does not supply this method, features using the type
>         will accept any value."""
>If this is the case then I will have to subclass most types to enforce
>type constraints.

Yes.  I preferred to err on the side of Pythonicness; ordinarily attributes 
are untyped.

> > >I am very familiar with Formulator and the form generation code in Zope
> > >3 and it would be rather trivial to implement something like it for
> > >PEAK.
> >
> > That's good to hear; I've been worried about that.
>How so?

Just in a general sense, nothing specific.

>The normalize function will be most helpful for what I want to do but it
>feels funny to define 'normalize' on a feature when I actually want to
>'validate'. What is the background to 'normalize'?

To "normalize" a value means to adapt or convert it to the appropriate 
type, range, or whatever for that feature (or raise an error if such 
conversion is not possible).

If what you really want is to "validate", you probably don't want to use 
any of the existing machinery.  Instead, you should define an interface 
expected of features, that defines a validation method, and then require 
features to have that method.

The distinction in my mind between normalization and validation, is that to 
validate, you want to validate *all* your values and assemble a series of 
messages, so that you can give them all to the user.  So, you don't want to 
throw an error in the midst of modifying, say, a  multi-element sequence 
because one of the items is wrong.  You just want to validate the values 
and see if they're okay.

More information about the PEAK mailing list