예제 #1
0
파일: fieldtypes.py 프로젝트: menpo/vrml97
 def coerce( self, value ):
     if isinstance( value, (str,unicode)):
         value = [ 
             float(x) 
             for x in value.replace( ',', ' ').replace('[',' ').replace(']').split()
         ]
     if isinstance( value, arrays.ArrayType ):
         if arrays.typeCode(value) not in self.acceptedTypes:
             value = value.astype( self.targetType )
     elif isinstance( value, field.SEQUENCE_TYPES):
         try:
             value = arrays.array( value, self.targetType)
         except ValueError:
             value = arrays.array(
                 map( float, collapse( value) ),
                 self.targetType,
             )
     elif isinstance( value, (int,long,float)):
         value = arrays.array( [value], self.targetType )
     else:
         try:
             value = arrays.asarray( value, self.targetType )
         except Exception:
             raise ValueError( """Attempted to set value for an %s field which is not compatible: %s"""%( self.typeName(), repr(value) ))
     # special casing, again, for explicitly structured arrays 
     if not arrays.typeCode( value ) == 'V':
         value = arrays.contiguous( self.reshape(value) )
     return value
예제 #2
0
 def coerce(self, value):
     if isinstance(value, (str, unicode)):
         value = [
             float(x) for x in value.replace(',', ' ').replace(
                 '[', ' ').replace(']').split()
         ]
     if isinstance(value, arrays.ArrayType):
         if arrays.typeCode(value) not in self.acceptedTypes:
             value = value.astype(self.targetType)
     elif isinstance(value, field.SEQUENCE_TYPES):
         try:
             value = arrays.array(value, self.targetType)
         except ValueError:
             value = arrays.array(
                 map(float, collapse(value)),
                 self.targetType,
             )
     elif isinstance(value, (int, long, float)):
         value = arrays.array([value], self.targetType)
     else:
         try:
             value = arrays.asarray(value, self.targetType)
         except Exception:
             raise ValueError(
                 """Attempted to set value for an %s field which is not compatible: %s"""
                 % (self.typeName(), repr(value)))
     # special casing, again, for explicitly structured arrays
     if not arrays.typeCode(value) == 'V':
         value = arrays.contiguous(self.reshape(value))
     return value
예제 #3
0
 def coerce(self, value):
     """Base coercion mechanism for vector-like field types"""
     if isinstance(value, (str, unicode)):
         value = [float(x) for x in value.replace(',', ' ').split()]
     if isinstance(value, (int, long, float)):
         value = arrays.zeros(self.dimension, self.targetType)
         value[:] = float(value)
     elif isinstance(value, arrays.ArrayType):
         if arrays.typeCode(value) not in self.acceptedTypes:
             value = value.astype(self.targetType)
         value = value.reshape(self.dimension)
     elif isinstance(value, field.SEQUENCE_TYPES):
         value = arrays.asarray(map(float, collapse(value)),
                                self.targetType)
         value.reshape(self.dimension)
     else:
         try:
             value = arrays.asarray(value, self.targetType)
         except Exception:
             raise ValueError(
                 """Attempted to set value for an %s field which is not compatible: %s"""
                 % (self.typeName(), repr(value)))
         else:
             value.reshape(self.dimension)
     if value.shape != self.dimension:
         raise ValueError(
             """%s value of incorrect shape (is %s, should be %s)""" % (
                 self.__class__.__name__,
                 value.shape,
                 self.dimension,
             ))
     value = arrays.contiguous(value)
     return value
예제 #4
0
파일: fieldtypes.py 프로젝트: menpo/vrml97
 def coerce( self, value ):
     """Base coercion mechanism for vector-like field types"""
     if isinstance( value, (str,unicode)):
         value = [ float(x) for x in value.replace( ',', ' ').split()]
     if isinstance(value, (int,long,float)):
         value = arrays.zeros( self.dimension, self.targetType )
         value[:] = float(value)
     elif isinstance( value, arrays.ArrayType ):
         if arrays.typeCode(value) not in self.acceptedTypes:
             value = value.astype(self.targetType)
         value = value.reshape( self.dimension )
     elif isinstance( value, field.SEQUENCE_TYPES):
         value = arrays.asarray(
             map(float, collapse(value)),
             self.targetType
         )
         value.reshape( self.dimension )
     else:
         try:
             value = arrays.asarray( value, self.targetType )
         except Exception:
             raise ValueError( """Attempted to set value for an %s field which is not compatible: %s"""%( self.typeName(), repr(value) ))
         else:
             value.reshape( self.dimension )
     if value.shape != self.dimension:
         raise ValueError(
             """%s value of incorrect shape (is %s, should be %s)"""%(
                 self.__class__.__name__,
                 value.shape,
                 self.dimension,
             )
         )
     value = arrays.contiguous( value )
     return value
예제 #5
0
파일: fieldtypes.py 프로젝트: menpo/vrml97
 def check( self, value ):
     """Check that the given value is of exactly the expected type"""
     if isinstance( value, arrays.ArrayType ):
         if arrays.typeCode(value) in self.acceptedTypes:
             s = arrays.shape(value)
             if len(s) == len(self.dimension)+1 and s[1:] == self.dimension:
                 return 1
     return 0
예제 #6
0
 def check(self, value):
     """Check that the given value is of exactly the expected type"""
     if isinstance(value, arrays.ArrayType):
         if arrays.typeCode(value) in self.acceptedTypes:
             s = arrays.shape(value)
             if len(s) == len(
                     self.dimension) + 1 and s[1:] == self.dimension:
                 return 1
     return 0
예제 #7
0
파일: fieldtypes.py 프로젝트: menpo/vrml97
 def coerce( self, value ):
     """Base coercion mechanism for floating point field types"""
     if isinstance( value, (str,unicode)):
         value = [ float(x) for x in value.replace( ',', ' ').split()]
     if isinstance(value, field.NUMERIC_TYPES):
         return arrays.array([float(value)],self.targetType)
     elif isinstance( value, arrays.ArrayType ):
         if arrays.typeCode(value) not in self.acceptedTypes:
             value = value.astype(self.targetType)
         return arrays.contiguous( arrays.ravel(value) )
     elif isinstance( value, field.SEQUENCE_TYPES):
         return arrays.array(
             map( float, collapse( value) ),
             self.targetType,
         )
     elif not value:
         return arrays.array([],self.targetType)
     raise ValueError( """Attempted to set value for an %s field which is not compatible: %s"""%( self.typeName(), repr(value) ))
예제 #8
0
 def coerce(self, value):
     """Base coercion mechanism for floating point field types"""
     if isinstance(value, (str, unicode)):
         value = [float(x) for x in value.replace(',', ' ').split()]
     if isinstance(value, field.NUMERIC_TYPES):
         return arrays.array([float(value)], self.targetType)
     elif isinstance(value, arrays.ArrayType):
         if arrays.typeCode(value) not in self.acceptedTypes:
             value = value.astype(self.targetType)
         return arrays.contiguous(arrays.ravel(value))
     elif isinstance(value, field.SEQUENCE_TYPES):
         return arrays.array(
             map(float, collapse(value)),
             self.targetType,
         )
     elif not value:
         return arrays.array([], self.targetType)
     raise ValueError(
         """Attempted to set value for an %s field which is not compatible: %s"""
         % (self.typeName(), repr(value)))
예제 #9
0
 def coerce( self, value ):
     """Base coercion mechanism for multiple-value integer fields"""
     if isinstance( value, (str,unicode)):
         value = [ 
             self.base_converter(x) 
             for x in value.replace( ',', ' ').split()
         ]
     if isinstance(value, field.NUMERIC_TYPES):
         return arrays.array([int(value)],self.arrayDataType)
     elif isinstance( value, arrays.ArrayType ):
         if arrays.typeCode(value) not in self.acceptedTypes:
             value = value.astype( self.arrayDataType )
         return arrays.contiguous( arrays.ravel(value) )
     elif isinstance( value, field.SEQUENCE_TYPES):
         return arrays.array(
             [int(obj) for obj in value],
             self.arrayDataType,
         )
     elif not value:
         return arrays.array([],self.arrayDataType)
     raise ValueError( """Attempted to set value for an %s field which is not compatible: %s"""%( self.typeName(), repr(value) ))
예제 #10
0
 def copyValue(self, value, copier=None):
     """Copy a value for copier"""
     return arrays.array(value, arrays.typeCode(value))
예제 #11
0
These are all of the "low-level" field-types
(i.e. not nodes) defined by VRML97.  Each has
a canonical in-memory storage format so that
code can rely on that format when dealing with
the field values.

We use Numeric Python arrays whereever possible.
"""
import operator
from vrml import protonamespace, field, csscolors, arrays

import types, sys
from types import ListType, TupleType

DOUBLE_TYPE = arrays.typeCode(arrays.array([0], 'd'))
FLOAT_TYPE = arrays.typeCode(arrays.array([0], 'f'))
INT_TYPE = arrays.typeCode(arrays.array([0], 'i'))
UINT_TYPE = arrays.typeCode(arrays.array([0], 'I'))


def _collapse(inlist, isinstance=isinstance, ltype=list, maxint=sys.maxint):
    '''
    Destructively flatten a list hierarchy to a single level. 
    Non-recursive, and (as far as I can see, doesn't have any
    glaring loopholes).
    Further speedups and obfuscations by Tim Peters :)
    '''
    try:
        # for every possible index
        for ind in xrange(maxint):
예제 #12
0
파일: fieldtypes.py 프로젝트: menpo/vrml97
 def copyValue( self, value, copier=None ):
     """Copy a value for copier"""
     return arrays.array(value, arrays.typeCode(value) )
예제 #13
0
파일: fieldtypes.py 프로젝트: menpo/vrml97
These are all of the "low-level" field-types
(i.e. not nodes) defined by VRML97.  Each has
a canonical in-memory storage format so that
code can rely on that format when dealing with
the field values.

We use Numeric Python arrays whereever possible.
"""
import operator
from vrml import protonamespace, field, csscolors, arrays

import types, sys
from types import ListType, TupleType

DOUBLE_TYPE = arrays.typeCode( arrays.array( [0],'d') )
FLOAT_TYPE = arrays.typeCode( arrays.array( [0],'f') )
INT_TYPE = arrays.typeCode( arrays.array( [0],'i') )
UINT_TYPE = arrays.typeCode( arrays.array( [0],'I') )

def _collapse(inlist, isinstance=isinstance, ltype=list, maxint= sys.maxint):
    '''
    Destructively flatten a list hierarchy to a single level. 
    Non-recursive, and (as far as I can see, doesn't have any
    glaring loopholes).
    Further speedups and obfuscations by Tim Peters :)
    '''
    try:
        # for every possible index
        for ind in xrange( maxint):
            # while that index currently holds a list