[TransWarp] model.Attribute questions

Radek Kanovsky rk at dat.cz
Mon Mar 24 15:31:37 EST 2003


Hi,

I have found PEAK features very useful and decided to use it for the
first time in my application. One of my goals is to bother less with SQL
because designing in Python and SQL together is often very uncomfortable
and doubles work. I have started to code SQLEntityDM with infrastructure
for making SQL data definition and data manipulation more pythonic
way. Automatic creation of database tables from model.Elements seems
relatively easy and straightforward but anyway I am not able to catch
some basics:

class Book (model.Element) :

    class Title (model.Attribute) :
        referencedType = model.String

    class Pages (model.Attribute) :
        referencedType = model.Integer

* Is there a standard PEAK way for defining maximum length of Book.Title value?
  I want to generate appropriate VARCHAR(<N>). I think that attributes
  model.String.length or model.Atribute.upperBound are for other purposes.
  Same question arises about UNIQUE, NOT NULL, etc.
  
* Why doesn't peak check values assigned to element attributes? Do I need
  to make it manualy as follows?

    class Title (model.Attribute) :
        referencedType = model.String
        def set (attrcls, obj, val) :
            val = str(val) # need string
            if length(val) > 100 :
                raise ValueError('Nobody reads such long titles')
            return super(attrcls, attrcls).set(obj, val)

    class Pages (model.Attribute) :
        referencedType = model.Integer
        def set (attrcls, obj, val) :
            val = int(val) # need int
            if val < 0 : raise ValueError("Antibook!!!")
            return super(attrcls, attrcls).set(obj, val)

  Or methods storage.EntityDM.(_new|_save) are the right place for doing
  such things? But this relates naturaly to model and not to storage.
  I must confess that I am slightly confused by contents of model/datatypes.py
  file and its purpose. 

Thanks.

Radek


PS: I think that there is typo in model.datatypes.Double that parent
    of Double should be Float and not PrimitiveType. Python float
    is defined as double at C level so it is able to hold both
    float and double values.

    class Float(PrimitiveType):
        mdl_fromString = float

    class Double(PrimitiveType):
        pass



More information about the PEAK mailing list