<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import warnings <EOL> from . trait_base import SequenceTypes <EOL> from . trait_errors import TraitError <EOL> from . trait_handlers import TraitType , OBJECT_IDENTITY_COMPARE <EOL> from . trait_types import Str , Any , Int as TInt , Float as TFloat <EOL> ndarray = None <EOL> asarray = None <EOL> def dtype2trait ( dtype ) : <EOL> """<STR_LIT>""" <EOL> import numpy <EOL> if dtype . char in numpy . typecodes [ '<STR_LIT>' ] : <EOL> return TFloat <EOL> elif dtype . char in numpy . typecodes [ '<STR_LIT>' ] : <EOL> return TInt <EOL> elif dtype . char [ <NUM_LIT:0> ] == '<STR_LIT:S>' : <EOL> return Str <EOL> else : <EOL> return Any <EOL> class AbstractArray ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dtype = None , shape = None , value = None , <EOL> coerce = False , typecode = None , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> global ndarray , asarray <EOL> try : <EOL> import numpy <EOL> except ImportError : <EOL> raise TraitError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> from numpy import asarray , ndarray <EOL> metadata [ '<STR_LIT>' ] = True <EOL> metadata . setdefault ( '<STR_LIT>' , OBJECT_IDENTITY_COMPARE ) <EOL> if typecode is not None : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' , DeprecationWarning ) <EOL> if ( dtype is not None ) and ( dtype != typecode ) : <EOL> raise TraitError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> dtype = typecode <EOL> if dtype is not None : <EOL> try : <EOL> dtype = numpy . dtype ( dtype ) <EOL> except TypeError : <EOL> raise TraitError ( '<STR_LIT>' % <EOL> dtype ) <EOL> if shape is not None : <EOL> if isinstance ( shape , SequenceTypes ) : <EOL> for item in shape : <EOL> if ( ( item is None ) or ( type ( item ) is int ) or <EOL> ( isinstance ( item , SequenceTypes ) and <EOL> ( len ( item ) == <NUM_LIT:2> ) and <EOL> ( type ( item [ <NUM_LIT:0> ] ) is int ) and ( item [ <NUM_LIT:0> ] >= <NUM_LIT:0> ) and <EOL> ( ( item [ <NUM_LIT:1> ] is None ) or ( ( type ( item [ <NUM_LIT:1> ] ) is int ) and <EOL> ( item [ <NUM_LIT:0> ] <= item [ <NUM_LIT:1> ] ) ) ) ) ) : <EOL> continue <EOL> raise TraitError , "<STR_LIT>" <EOL> else : <EOL> raise TraitError , "<STR_LIT>" <EOL> if value is None : <EOL> value = self . _default_for_dtype_and_shape ( dtype , shape ) <EOL> self . dtype = dtype <EOL> self . shape = shape <EOL> self . coerce = coerce <EOL> super ( AbstractArray , self ) . __init__ ( value , ** metadata ) <EOL> def validate ( self , object , name , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> type_value = type ( value ) <EOL> if not isinstance ( value , ndarray ) : <EOL> if not isinstance ( value , SequenceTypes ) : <EOL> self . error ( object , name , value ) <EOL> if self . dtype is not None : <EOL> value = asarray ( value , self . dtype ) <EOL> else : <EOL> value = asarray ( value ) <EOL> if ( ( self . dtype is not None ) and <EOL> ( value . dtype != self . dtype ) ) : <EOL> if self . coerce : <EOL> value = value . astype ( self . dtype ) <EOL> else : <EOL> value = asarray ( value , self . dtype ) <EOL> trait_shape = self . shape <EOL> if trait_shape is None : <EOL> return value <EOL> value_shape = value . shape <EOL> if len ( trait_shape ) == len ( value_shape ) : <EOL> for i , dim in enumerate ( value_shape ) : <EOL> item = trait_shape [ i ] <EOL> if item is not None : <EOL> if type ( item ) is int : <EOL> if dim != item : <EOL> break <EOL> elif ( ( dim < item [ <NUM_LIT:0> ] ) or <EOL> ( ( item [ <NUM_LIT:1> ] is not None ) and ( dim > item [ <NUM_LIT:1> ] ) ) ) : <EOL> break <EOL> else : <EOL> return value <EOL> except : <EOL> pass <EOL> self . error ( object , name , value ) <EOL> def info ( self ) : <EOL> """<STR_LIT>""" <EOL> dtype = shape = '<STR_LIT>' <EOL> if self . shape is not None : <EOL> shape = [ ] <EOL> for item in self . shape : <EOL> if item is None : <EOL> item = '<STR_LIT:*>' <EOL> elif type ( item ) is not int : <EOL> if item [ <NUM_LIT:1> ] is None : <EOL> item = '<STR_LIT>' % item [ <NUM_LIT:0> ] <EOL> else : <EOL> item = '<STR_LIT>' % item <EOL> shape . append ( item ) <EOL> shape = '<STR_LIT>' % ( tuple ( shape ) , ) <EOL> if self . dtype is not None : <EOL> dtype = '<STR_LIT>' % self . dtype <EOL> return '<STR_LIT>' % ( dtype , shape ) <EOL> def create_editor ( self ) : <EOL> """<STR_LIT>""" <EOL> editor = None <EOL> auto_set = False <EOL> if self . auto_set is None : <EOL> auto_set = True <EOL> enter_set = self . enter_set or False <EOL> if self . shape is not None and len ( self . shape ) == <NUM_LIT:2> : <EOL> from traitsui . api import ArrayEditor <EOL> editor = ArrayEditor ( auto_set = auto_set , enter_set = enter_set ) <EOL> else : <EOL> from traitsui . api import TupleEditor <EOL> if self . dtype is None : <EOL> types = Any <EOL> else : <EOL> types = dtype2trait ( self . dtype ) <EOL> editor = TupleEditor ( types = types , <EOL> labels = self . labels or [ ] , <EOL> cols = self . cols or <NUM_LIT:1> , <EOL> auto_set = auto_set , <EOL> enter_set = enter_set ) <EOL> return editor <EOL> def get_default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( <NUM_LIT:7> , ( self . copy_default_value , <EOL> ( self . validate ( None , None , self . default_value ) , ) , None ) ) <EOL> def copy_default_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value . copy ( ) <EOL> def _default_for_dtype_and_shape ( self , dtype , shape ) : <EOL> """<STR_LIT>""" <EOL> from numpy import zeros <EOL> if dtype is None : <EOL> dt = int <EOL> else : <EOL> dt = dtype <EOL> if shape is None : <EOL> value = zeros ( ( <NUM_LIT:0> , ) , dt ) <EOL> else : <EOL> size = [ ] <EOL> for item in shape : <EOL> if item is None : <EOL> item = <NUM_LIT:1> <EOL> elif type ( item ) in SequenceTypes : <EOL> item = item [ <NUM_LIT:0> ] <EOL> size . append ( item ) <EOL> value = zeros ( size , dt ) <EOL> return value <EOL> class Array ( AbstractArray ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dtype = None , shape = None , value = None , <EOL> typecode = None , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> super ( Array , self ) . __init__ ( dtype , shape , value , False , <EOL> typecode = typecode , ** metadata ) <EOL> class CArray ( AbstractArray ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dtype = None , shape = None , value = None , <EOL> typecode = None , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> super ( CArray , self ) . __init__ ( dtype , shape , value , True , <EOL> typecode = typecode , ** metadata ) <EOL> class ArrayOrNone ( CArray ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** metadata ) : <EOL> metadata . setdefault ( '<STR_LIT>' , OBJECT_IDENTITY_COMPARE ) <EOL> super ( ArrayOrNone , self ) . __init__ ( * args , ** metadata ) <EOL> def validate ( self , object , name , value ) : <EOL> if value is None : <EOL> return value <EOL> return super ( ArrayOrNone , self ) . validate ( object , name , value ) <EOL> def get_default_value ( self ) : <EOL> dv = self . default_value <EOL> if dv is None : <EOL> return ( <NUM_LIT:0> , dv ) <EOL> else : <EOL> return ( <NUM_LIT:7> , ( self . copy_default_value , <EOL> ( self . validate ( None , None , dv ) , ) , None ) ) <EOL> def _default_for_dtype_and_shape ( self , dtype , shape ) : <EOL> return None </s>
