scipy/io/matlab/mio5.py
def to_writeable(source):
    ''' Convert input object ``source`` to something we can write

    Parameters
    ----------
    source : object

    Returns
    -------
    arr : None or ndarray or EmptyStructMarker
        If `source` cannot be converted to something we can write to a matfile,
        return None.  If `source` is equivalent to an empty dictionary, return
        ``EmptyStructMarker``.  Otherwise return `source` converted to an
        ndarray with contents for writing to matfile.
    '''
    if isinstance(source, np.ndarray):
        return source
    if source is None:
        return None
    # Objects that implement mappings
    is_mapping = (hasattr(source, 'keys') and hasattr(source, 'values') and
                  hasattr(source, 'items'))
    # Objects that don't implement mappings, but do have dicts
    if not is_mapping and hasattr(source, '__dict__'):
        source = dict((key, value) for key, value in source.__dict__.items()
                      if not key.startswith('_'))
        is_mapping = True
    if is_mapping:
        dtype = []
        values = []
        for field, value in source.items():
            if (isinstance(field, string_types) and
                    field[0] not in '_0123456789'):
                dtype.append((field, object))
                values.append(value)
        if dtype:
            return np.array([tuple(values)], dtype)
        else:
            return EmptyStructMarker
    # Next try and convert to an array
    narr = np.asanyarray(source)
    if narr.dtype.type in (object, np.object_) and \
       narr.shape == () and narr == source:
        # No interesting conversion possible
        return None
    return narr
 Convert input object ``source`` to something we can write

    Parameters
    ----------
    source : object

    Returns
    -------
    arr : None or ndarray or EmptyStructMarker
        If `source` cannot be converted to something we can write to a matfile,
        return None.  If `source` is equivalent to an empty dictionary, return
        ``EmptyStructMarker``.  Otherwise return `source` converted to an
        ndarray with contents for writing to matfile.
    
Convert input object ``source`` to something we can write
Parameters

source : object

Returns

arr : None or ndarray or EmptyStructMarker
If `source` cannot be converted to something we can write to a matfile,
return None.
Convert input object ` ` source ` ` to something we can write Parameters source : object Returns arr : None or ndarray or EmptyStructMarker If ` source ` cannot be converted to something we can write to a matfile return None .
def to_writeable(source):
    if isinstance(source, np.ndarray):
        return source
    if source is None:
        return None
    is_mapping = (hasattr(source, 'keys') and hasattr(source, 'values') and
                  hasattr(source, 'items'))
    if not is_mapping and hasattr(source, '__dict__'):
        source = dict((key, value) for key, value in source.__dict__.items()
                      if not key.startswith('_'))
        is_mapping = True
    if is_mapping:
        dtype = []
        values = []
        for field, value in source.items():
            if (isinstance(field, string_types) and
                    field[0] not in '_0123456789'):
                dtype.append((field, object))
                values.append(value)
        if dtype:
            return np.array([tuple(values)], dtype)
        else:
            return EmptyStructMarker
    narr = np.asanyarray(source)
    if narr.dtype.type in (object, np.object_) and \
       narr.shape == () and narr == source:
        return None
    return narr
def to_writeable ( source ) : if isinstance ( source , np . ndarray ) : return source if source is None : return None is_mapping = ( hasattr ( source , 'keys' ) and hasattr ( source , 'values' ) and hasattr ( source , 'items' ) ) if not is_mapping and hasattr ( source , '__dict__' ) : source = dict ( ( key , value ) for key , value in source . __dict__ . items ( ) if not key . startswith ( '_' ) ) is_mapping = True if is_mapping : dtype = [ ] values = [ ] for field , value in source . items ( ) : if ( isinstance ( field , string_types ) and field [ 0 ] not in '_0123456789' ) : dtype . append ( ( field , object ) ) values . append ( value ) if dtype : return np . array ( [ tuple ( values ) ] , dtype ) else : return EmptyStructMarker narr = np . asanyarray ( source ) if narr . dtype . type in ( object , np . object_ ) and narr . shape == ( ) and narr == source : return None return narr
Convert input object ``source`` to something we can write
Parameters
Convert input object ` ` source ` ` to something we can write Parameters
''' Convert input object ``source`` to something we can write

    Parameters
    ----------
    source : object

    Returns
    -------
    arr : None or ndarray or EmptyStructMarker
        If `source` cannot be converted to something we can write to a matfile,
        return None.  If `source` is equivalent to an empty dictionary, return
        ``EmptyStructMarker``.  Otherwise return `source` converted to an
        ndarray with contents for writing to matfile.
    ''' # Objects that implement mappings # Objects that don't implement mappings, but do have dicts # Next try and convert to an array # No interesting conversion possible
source