sidpy/base/string_utils.py
def format_quantity(value, unit_names, factors, decimals=2):
    """
    Formats the provided quantity such as time or size to appropriate strings

    Parameters
    ----------
    value : number
        value in some base units. For example - time in seconds
    unit_names : array-like
        List of names of units for each scale of the value
    factors : array-like
        List of scaling factors for each scale of the value
    decimals : uint, optional. default = 2
        Number of decimal places to which the value needs to be formatted

    Returns
    -------
    str
        String with value formatted correctly

    Examples
    --------
    >>> # If ``sidpy.string_utils.format_time()`` were not available, we could
    >>> # get the same functionality via:
    >>> import sidpy
    >>> units = ['msec', 'sec', 'mins', 'hours']
    >>> factors = [0.001, 1, 60, 3600]
    >>> time_value = 14497.34
    >>> str_form = sidpy.string_utils.format_quantity(time_value,units,factors)
    >>> print('{} seconds = {}'.format(14497.34, str_form))

    See Also
    --------
    sidpy.string_utils.format_size
    sidpy.string_utils.format_time
    """
    # assert isinstance(value, (int, float))
    if not isinstance(unit_names, Iterable):
        raise TypeError('unit_names must an Iterable')
    if not isinstance(factors, Iterable):
        raise TypeError('factors must be an Iterable')
    if len(unit_names) != len(factors):
        raise ValueError('unit_names and factors must be of the same length')
    unit_names = validate_list_of_strings(unit_names, 'unit_names')
    index = None

    for index, val in enumerate(factors):
        if value < val:
            index -= 1
            break

    index = max(0, index)  # handles sub msec

    return '{} {}'.format(np.round(value / factors[index], decimals),
                          unit_names[index])

    Formats the provided quantity such as time or size to appropriate strings

    Parameters
    ----------
    value : number
        value in some base units. For example - time in seconds
    unit_names : array-like
        List of names of units for each scale of the value
    factors : array-like
        List of scaling factors for each scale of the value
    decimals : uint, optional. default = 2
        Number of decimal places to which the value needs to be formatted

    Returns
    -------
    str
        String with value formatted correctly

    Examples
    --------
    >>> # If ``sidpy.string_utils.format_time()`` were not available, we could
    >>> # get the same functionality via:
    >>> import sidpy
    >>> units = ['msec', 'sec', 'mins', 'hours']
    >>> factors = [0.001, 1, 60, 3600]
    >>> time_value = 14497.34
    >>> str_form = sidpy.string_utils.format_quantity(time_value,units,factors)
    >>> print('{} seconds = {}'.format(14497.34, str_form))

    See Also
    --------
    sidpy.string_utils.format_size
    sidpy.string_utils.format_time
    
Formats the provided quantity such as time or size to appropriate strings
Parameters

value : number
value in some base units. For example - time in seconds
unit_names : array-like
List of names of units for each scale of the value
factors : array-like
List of scaling factors for each scale of the value
decimals : uint, optional. default = 2
Number of decimal places to which the value needs to be formatted

Returns

str
String with value formatted correctly

Examples



See Also


Formats the provided quantity such as time or size to appropriate strings Parameters value : number value in some base units . For example - time in seconds unit_names : array - like List of names of units for each scale of the value factors : array - like List of scaling factors for each scale of the value decimals : uint optional . default = 2 Number of decimal places to which the value needs to be formatted Returns str String with value formatted correctly Examples See Also
def format_quantity(value, unit_names, factors, decimals=2):
    if not isinstance(unit_names, Iterable):
        raise TypeError('unit_names must an Iterable')
    if not isinstance(factors, Iterable):
        raise TypeError('factors must be an Iterable')
    if len(unit_names) != len(factors):
        raise ValueError('unit_names and factors must be of the same length')
    unit_names = validate_list_of_strings(unit_names, 'unit_names')
    index = None
    for index, val in enumerate(factors):
        if value < val:
            index -= 1
            break
    index = max(0, index)  
    return '{} {}'.format(np.round(value / factors[index], decimals),
                          unit_names[index])
def format_quantity ( value , unit_names , factors , decimals = 2 ) : if not isinstance ( unit_names , Iterable ) : raise TypeError ( 'unit_names must an Iterable' ) if not isinstance ( factors , Iterable ) : raise TypeError ( 'factors must be an Iterable' ) if len ( unit_names ) != len ( factors ) : raise ValueError ( 'unit_names and factors must be of the same length' ) unit_names = validate_list_of_strings ( unit_names , 'unit_names' ) index = None for index , val in enumerate ( factors ) : if value < val : index -= 1 break index = max ( 0 , index ) return '{} {}' . format ( np . round ( value / factors [ index ] , decimals ) , unit_names [ index ] )
Formats the provided quantity such as time or size to appropriate strings
Parameters
Formats the provided quantity such as time or size to appropriate strings Parameters
"""
    Formats the provided quantity such as time or size to appropriate strings

    Parameters
    ----------
    value : number
        value in some base units. For example - time in seconds
    unit_names : array-like
        List of names of units for each scale of the value
    factors : array-like
        List of scaling factors for each scale of the value
    decimals : uint, optional. default = 2
        Number of decimal places to which the value needs to be formatted

    Returns
    -------
    str
        String with value formatted correctly

    Examples
    --------
    >>> # If ``sidpy.string_utils.format_time()`` were not available, we could
    >>> # get the same functionality via:
    >>> import sidpy
    >>> units = ['msec', 'sec', 'mins', 'hours']
    >>> factors = [0.001, 1, 60, 3600]
    >>> time_value = 14497.34
    >>> str_form = sidpy.string_utils.format_quantity(time_value,units,factors)
    >>> print('{} seconds = {}'.format(14497.34, str_form))

    See Also
    --------
    sidpy.string_utils.format_size
    sidpy.string_utils.format_time
    """ # assert isinstance(value, (int, float)) # handles sub msec
value
unit_names
factors
decimals