sidpy/base/string_utils.py
def clean_string_att(att_val):
    """
    Replaces any unicode objects within lists with their string counterparts to
    ensure compatibility with python 3. If the attribute is indeed a list of
    unicodes, the changes will be made in-place

    Parameters
    ----------
    att_val : object
        Attribute object

    Returns
    -------
    att_val : object
        Attribute object

    Notes
    -----
    The ``h5py`` package used for reading and manipulating HDF5 files has
    issues which necessitate the encoding of attributes whose values are lists
    of strings. This method encodes lists of strings correctly so that they can
    directly be written to HDF5 without causing any errors. All other kinds of
    simple attributes - single strings, numbers, lists of numbers are
    unmodified by this function.

    Examples
    --------
    >>> import sidpy
    >>> expected = ['a', 'bc', 'def']
    >>> returned = sidpy.string_utils.clean_string_att(expected)
    >>> print('List of strings value: {} encoded to: {}'.format(expected, returned))
    >>>
    >>> expected = [1, 2, 3.456]
    >>> returned = sidpy.string_utils.clean_string_att(expected)
    >>> print('List of numbers value: {} returned as is: {}'.format(expected, returned))
    """
    try:
        if isinstance(att_val, Iterable):
            if type(att_val) in [unicode, str]:
                return att_val
            elif np.any([type(x) in [str, unicode, bytes, np.str_] for x in att_val]):
                return np.array(att_val, dtype='S')
            elif isinstance(att_val, (list, tuple)):
                # Not sure how to do this elegantly,
                for item in att_val:
                    if not isinstance(item, (str, unicode, bytes, np.str_,
                                             Number)):
                        raise TypeError('Provided object was a list or tuple '
                                         'whose element was not a string or '
                                         'number but was of type: {}'
                                         ''.format(type(item)))
        if type(att_val) == np.str_:
            return str(att_val)
        return att_val
    except TypeError:
        raise TypeError('Failed to clean: {}'.format(att_val))

    Replaces any unicode objects within lists with their string counterparts to
    ensure compatibility with python 3. If the attribute is indeed a list of
    unicodes, the changes will be made in-place

    Parameters
    ----------
    att_val : object
        Attribute object

    Returns
    -------
    att_val : object
        Attribute object

    Notes
    -----
    The ``h5py`` package used for reading and manipulating HDF5 files has
    issues which necessitate the encoding of attributes whose values are lists
    of strings. This method encodes lists of strings correctly so that they can
    directly be written to HDF5 without causing any errors. All other kinds of
    simple attributes - single strings, numbers, lists of numbers are
    unmodified by this function.

    Examples
    --------
    >>> import sidpy
    >>> expected = ['a', 'bc', 'def']
    >>> returned = sidpy.string_utils.clean_string_att(expected)
    >>> print('List of strings value: {} encoded to: {}'.format(expected, returned))
    >>>
    >>> expected = [1, 2, 3.456]
    >>> returned = sidpy.string_utils.clean_string_att(expected)
    >>> print('List of numbers value: {} returned as is: {}'.format(expected, returned))
    
Replaces any unicode objects within lists with their string counterparts to
ensure compatibility with python 3. If the attribute is indeed a list of
unicodes, the changes will be made in-place

Parameters

att_val : object
Attribute object

Returns

att_val : object
Attribute object

Notes

The ``h5py`` package used for reading and manipulating HDF5 files has
issues which necessitate the encoding of attributes whose values are lists
of strings. This method encodes lists of strings correctly so that they can
directly be written to HDF5 without causing any errors. All other kinds of
simple attributes - single strings, numbers, lists of numbers are
unmodified by this function.

Examples


Replaces any unicode objects within lists with their string counterparts to ensure compatibility with python 3 . If the attribute is indeed a list of unicodes the changes will be made in - place Parameters att_val : object Attribute object Returns att_val : object Attribute object Notes The ` ` h5py ` ` package used for reading and manipulating HDF5 files has issues which necessitate the encoding of attributes whose values are lists of strings . This method encodes lists of strings correctly so that they can directly be written to HDF5 without causing any errors . All other kinds of simple attributes - single strings numbers lists of numbers are unmodified by this function . Examples
def clean_string_att(att_val):
    try:
        if isinstance(att_val, Iterable):
            if type(att_val) in [unicode, str]:
                return att_val
            elif np.any([type(x) in [str, unicode, bytes, np.str_] for x in att_val]):
                return np.array(att_val, dtype='S')
            elif isinstance(att_val, (list, tuple)):
                for item in att_val:
                    if not isinstance(item, (str, unicode, bytes, np.str_,
                                             Number)):
                        raise TypeError('Provided object was a list or tuple '
                                         'whose element was not a string or '
                                         'number but was of type: {}'
                                         ''.format(type(item)))
        if type(att_val) == np.str_:
            return str(att_val)
        return att_val
    except TypeError:
        raise TypeError('Failed to clean: {}'.format(att_val))
def clean_string_att ( att_val ) : try : if isinstance ( att_val , Iterable ) : if type ( att_val ) in [ unicode , str ] : return att_val elif np . any ( [ type ( x ) in [ str , unicode , bytes , np . str_ ] for x in att_val ] ) : return np . array ( att_val , dtype = 'S' ) elif isinstance ( att_val , ( list , tuple ) ) : for item in att_val : if not isinstance ( item , ( str , unicode , bytes , np . str_ , Number ) ) : raise TypeError ( 'Provided object was a list or tuple ' 'whose element was not a string or ' 'number but was of type: {}' '' . format ( type ( item ) ) ) if type ( att_val ) == np . str_ : return str ( att_val ) return att_val except TypeError : raise TypeError ( 'Failed to clean: {}' . format ( att_val ) )
Replaces any unicode objects within lists with their string counterparts to
ensure compatibility with python 3.
Replaces any unicode objects within lists with their string counterparts to ensure compatibility with python 3 .
"""
    Replaces any unicode objects within lists with their string counterparts to
    ensure compatibility with python 3. If the attribute is indeed a list of
    unicodes, the changes will be made in-place

    Parameters
    ----------
    att_val : object
        Attribute object

    Returns
    -------
    att_val : object
        Attribute object

    Notes
    -----
    The ``h5py`` package used for reading and manipulating HDF5 files has
    issues which necessitate the encoding of attributes whose values are lists
    of strings. This method encodes lists of strings correctly so that they can
    directly be written to HDF5 without causing any errors. All other kinds of
    simple attributes - single strings, numbers, lists of numbers are
    unmodified by this function.

    Examples
    --------
    >>> import sidpy
    >>> expected = ['a', 'bc', 'def']
    >>> returned = sidpy.string_utils.clean_string_att(expected)
    >>> print('List of strings value: {} encoded to: {}'.format(expected, returned))
    >>>
    >>> expected = [1, 2, 3.456]
    >>> returned = sidpy.string_utils.clean_string_att(expected)
    >>> print('List of numbers value: {} returned as is: {}'.format(expected, returned))
    """ # Not sure how to do this elegantly,
att_val