def __setattr__(self, key, value):
        """specifically restrict key and value as defined by ALLOWED_TYPES"""
        if key != "type":
            raise KeyError("given key '%s' is not allowed.  Expected: 'type'"%(key))
        allowed = type(self).ALLOWED_TYPES
        if value == None and value in allowed: # if this type allows a 'None' value, it means the value is not yet defined
            super(RestrictedType, self).__setattr__(key, value)
            return
        result = []
        if isinstance(allowed, dict):
            for k, v in iteritems(allowed):
                if value != k and value != v: continue
                result.append(MultiType(k, v))
        elif isinstance(allowed, list):
            result = [v for v in allowed if value == v]
        if len(result) == 0:
            raise ValueError("given value '%s' %s is not a %s value.  Allowed:"\
                "%s"%(value, type(value), type(self), list(allowed)))
        elif len(result) > 1:
            raise ValueError("given value '%s' %s defined too many matching "\
                "values: %s"%(value, type(value), result))
        value = result.pop() # allow use the key value, not the value-value (yay wording)
        super(RestrictedType, self).__setattr__(key, value)
def __setattr__ ( self , key , value ) : if key != "type" : raise KeyError ( "given key '%s' is not allowed.  Expected: 'type'" % ( key ) ) allowed = type ( self ) . ALLOWED_TYPES if value == None and value in allowed : super ( RestrictedType , self ) . __setattr__ ( key , value ) return result = [ ] if isinstance ( allowed , dict ) : for k , v in iteritems ( allowed ) : if value != k and value != v : continue result . append ( MultiType ( k , v ) ) elif isinstance ( allowed , list ) : result = [ v for v in allowed if value == v ] if len ( result ) == 0 : raise ValueError ( "given value '%s' %s is not a %s value.  Allowed:" "%s" % ( value , type ( value ) , type ( self ) , list ( allowed ) ) ) elif len ( result ) > 1 : raise ValueError ( "given value '%s' %s defined too many matching " "values: %s" % ( value , type ( value ) , result ) ) value = result . pop ( ) super ( RestrictedType , self ) . __setattr__ ( key , value )
specifically restrict key and value as defined by ALLOWED_TYPES