Beispiel #1
0
def __convert_args_to_numeric(args_tuple):
    '''Returns a list of numeric args.'''

    args = list(args_tuple)
    for i, a in enumerate(args):
        if isinstance(a, str):
            args[i] = element.str_to_numeric(a)
    return args
Beispiel #2
0
def __convert_args_to_numeric(args_tuple):
    '''Returns a list of numeric args.'''

    args = list(args_tuple)
    for i, a in enumerate(args):
        if isinstance(a, str):
            args[i] = element.str_to_numeric(a)
    return args
Beispiel #3
0
def operate_numeric(selection, pfield_index_list, pfunction, *args):
    '''Processes a matrix of pfields and events using the supplied
    :term:`pfunction` and any optional arguments.
    
    In cases where the original numeric pfield was an int, but
    processed with floats, the int will be output as a float in the
    score, even if the output contains no fractional parts.
    
    Example::
    
        >>> def multiply(pf, m): return pf * m
        ... 
        >>> sco.operate_numeric({0: 'i 1 0 4 1.0 440', 1: 'i 1 4 4 0.5 880'},
        ...                     5, multiply, 3)
        {0: 'i 1 0 4 1.0 1320', 1: 'i 1 4 4 0.5 2640'}
        
    A lambda function can specified as the pfunction argument::
    
        # Invert pfield
        operate_numeric(score, pf, lambda x: 1.0 / x)
        
    See :term:`pfield_index_list`, :term:`pfunction`, :term:`selection`

    '''

    # Args need to be numeric
    args = __convert_args_to_numeric(args)
    
    # Convert single single value to list
    pfield_index_list = __pfield_index_to_list(pfield_index_list)
    
    # Operate on all events in selection.  Sorted is a must.
    for k, v in sorted(selection.iteritems()):
        
        # Operate on each pfield
        for pf in pfield_index_list:
            pf_value = event.get(v, pf)
            
            # Preserve non-numeric pfields
            if element.token_type(pf_value) is element.NUMERIC:
                pf_value = element.str_to_numeric(pf_value)
                selection[k] = v = event.set(v, pf, pfunction(pf_value, *args))

    return selection
Beispiel #4
0
def operate_numeric(selection, pfield_index_list, pfunction, *args):
    '''Processes a matrix of pfields and events using the supplied
    :term:`pfunction` and any optional arguments.
    
    In cases where the original numeric pfield was an int, but
    processed with floats, the int will be output as a float in the
    score, even if the output contains no fractional parts.
    
    Example::
    
        >>> def multiply(pf, m): return pf * m
        ... 
        >>> sco.operate_numeric({0: 'i 1 0 4 1.0 440', 1: 'i 1 4 4 0.5 880'},
        ...                     5, multiply, 3)
        {0: 'i 1 0 4 1.0 1320', 1: 'i 1 4 4 0.5 2640'}
        
    A lambda function can specified as the pfunction argument::
    
        # Invert pfield
        operate_numeric(score, pf, lambda x: 1.0 / x)
        
    See :term:`pfield_index_list`, :term:`pfunction`, :term:`selection`

    '''

    # Args need to be numeric
    args = __convert_args_to_numeric(args)

    # Convert single single value to list
    pfield_index_list = __pfield_index_to_list(pfield_index_list)

    # Operate on all events in selection.  Sorted is a must.
    for k, v in sorted(selection.iteritems()):

        # Operate on each pfield
        for pf in pfield_index_list:
            pf_value = event.get(v, pf)

            # Preserve non-numeric pfields
            if element.token_type(pf_value) is element.NUMERIC:
                pf_value = element.str_to_numeric(pf_value)
                selection[k] = v = event.set(v, pf, pfunction(pf_value, *args))

    return selection