[SciPy-user] Q:ReadOnly Trait combined with other Trait

Norbert Klamann Norbert.Klamann at klamann-software.de
Thu May 1 11:32:39 EDT 2003


Hello, all,
at first I like to say that I like scipy and especially the traits 
concept very much.
I think it is very elaborate and useful.

At the moment I try to achieve that instances of certain classes can 
have traits which make an attribute 'write-once' _and_ assure that 
the value belongs to a certain class and I wonder how to do that.

Example : I want to create Values with units and I want to make sure 
that certain classes of Values can only have Units of certain 
classes. Each value with unit can have its unit set once (for the 
time being).

At the moment I have the following :
import traits
from fixedpoint import FixedPoint as fp

def _1timeonlyFixedPoint( object, name, value ):
    if name in object.__dict__ :
        raise AssertionError
    return fp(value,12)

class ValueUnit(traits.HasTraits) :
    __traits__ = { 'unit' : Unit,
                   'value' : traits.Trait(fp(0),_1timeonlyFixedPoint)
                   }
    def __init__(self,
                 unit = Unit(),
                 value = 0):
        self.unit = unit
        self.value = fp(value)

#some units exist, Currency is a subclass of Unit
EUR = Currency(name = 'Euro') 
USD = Currency(name = 'US Dollar')

# several kinds of ValueUnit exist, for example :

class MoneyValue(ValueUnit):
    __traits__ = { 'unit' : Currency()}
    def __init__(self,
                 unit = EUR,
                 value = 0):
        ValueUnit.__init__(self,unit,value)

Now the point : Because I am not able and willing to convert the 
value  when I change the unit (Currency), I want to make 'unit' a 
write-once attribute. I know this can be done with the ReadOnly 
trait, but I don't know how to combine this with the already defined 
trait 'unit' .
TraitComplex would result in ORing the both traits, I want them 
ANDed.

I could work around the problem if I create subclasses like 
EURMoneyValue or USDMoneyValue, but I don't like this proliferation 
of classes. 
I hope that I made my point clear and thank you for listening !

mit freundlichen Grüßen / with kind regards
Norbert Klamann
Norbert.Klamann at klamann-software.de
--
Klamann Software & Beratung GmbH        
Franz-Marc-Str.174                                  
D-50374 Erftstadt




More information about the SciPy-User mailing list