Example #1
0
def replace(selection, pfield_index_list, pgenerator, *args):
    '''Replaces pfield values in selection using a supplied pgenerator,
    function or method.
    
    This will overwrite and existing value, numeric or not, in a
    pfield, including elements such as carry statements and
    expressions.
    
    Use this function instead of operate_numeric() when you want to
    create new data, instead of altering existing pfield data. This
    works wells with python objects that have a persistant state.
    
    
    
    Example::
        
        >>> def return_something(x):
        ...     return x
        ... 
        >>> selection.replace({0: 'i 1 0 4 1.0 440'}, 5, return_something, '$foo')
        {0: 'i 1 0 4 1.0 $foo'}
        
    See :term:`pfield_index_list`, :term:`pgenerator`, :term:`selection`
    '''
    
    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:
            selection[k] = v = event.set(v, pf, pgenerator(*args))

    return selection
Example #2
0
def replace(selection, pfield_index_list, pgenerator, *args):
    '''Replaces pfield values in selection using a supplied pgenerator,
    function or method.
    
    This will overwrite and existing value, numeric or not, in a
    pfield, including elements such as carry statements and
    expressions.
    
    Use this function instead of operate_numeric() when you want to
    create new data, instead of altering existing pfield data. This
    works wells with python objects that have a persistant state.
    
    
    
    Example::
        
        >>> def return_something(x):
        ...     return x
        ... 
        >>> selection.replace({0: 'i 1 0 4 1.0 440'}, 5, return_something, '$foo')
        {0: 'i 1 0 4 1.0 $foo'}
        
    See :term:`pfield_index_list`, :term:`pgenerator`, :term:`selection`
    '''

    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:
            selection[k] = v = event.set(v, pf, pgenerator(*args))

    return selection
Example #3
0
def value_to_carry(score):
    '''Replaces subsequent repeated values with a carry (.)
    
    Identical expressions do no carry, as a carry only copies the first
    value output from an expression.  This breaks the form when multiple
    random evaluations are part of the score.
    
    Macros do no carry as they may contain expressions.
    
    No-carries are not carried.
    '''

    event_list = score.splitlines(True)
    last_identifier = None
    last_values = []
    output = []

    # Explicitly state pfield 3 instead a magic number.  Carry
    # statements only substitute for pfields 3 or higher.
    PFIELD_3 = 3

    # Excluded element token types
    elements = [element.EXPRESSION, element.MACRO, element.NO_CARRY]

    for e in event_list:
        if event.match(e, {0: 'i', 1: last_identifier}):
            lolv = len(last_values)

            for i in range(PFIELD_3, max(event.number_of_pfields(e), lolv)):
                this_pfield = event.get(e, i)

                # Detect NO_CARRY
                if element.token_type(this_pfield) == element.NO_CARRY:
                    last_values[i] = this_pfield
                    break

                elif element.token_type(last_values[i]) == element.NO_CARRY:
                    break

                # Replace pfield with carry
                elif (this_pfield == last_values[i]
                      and element.token_type(this_pfield) not in elements):
                    e = event.set(e, i, '.')

                # Add a carry if one does not exist
                elif this_pfield == None:
                    e = event.push(e, '.')

                else:
                    last_values[i] = this_pfield

            output.append(e)

        else:
            last_identifier = event.get(e, 1)
            last_values = event.get_pfield_list(e)
            output.append(e)

    return ''.join(output)
Example #4
0
def value_to_carry(score):
    '''Replaces subsequent repeated values with a carry (.)
    
    Identical expressions do no carry, as a carry only copies the first
    value output from an expression.  This breaks the form when multiple
    random evaluations are part of the score.
    
    Macros do no carry as they may contain expressions.
    
    No-carries are not carried.
    '''
    
    event_list = score.splitlines(True)    
    last_identifier = None
    last_values = []
    output = []

    # Explicitly state pfield 3 instead a magic number.  Carry
    # statements only substitute for pfields 3 or higher.
    PFIELD_3 = 3

    # Excluded element token types
    elements = [element.EXPRESSION, element.MACRO, element.NO_CARRY];
    
    for e in event_list:
        if event.match(e, {0: 'i', 1: last_identifier}):
            lolv = len(last_values)

            for i in range(PFIELD_3, max(event.number_of_pfields(e), lolv)):
                this_pfield = event.get(e, i)

                # Detect NO_CARRY
                if element.token_type(this_pfield) == element.NO_CARRY:
                    last_values[i] = this_pfield
                    break
                    
                elif element.token_type(last_values[i]) == element.NO_CARRY:
                    break
                    
                # Replace pfield with carry
                elif (this_pfield == last_values[i] and
                      element.token_type(this_pfield) not in elements):
                    e = event.set(e, i, '.')
                    
                # Add a carry if one does not exist
                elif this_pfield == None:
                    e = event.push(e, '.')
                    
                else:
                    last_values[i] = this_pfield

            output.append(e)
            
        else:
            last_identifier = event.get(e, 1)
            last_values = event.get_pfield_list(e)
            output.append(e)
        
    return ''.join(output)
Example #5
0
def operate_string(selection, pfield_index_list, pfunction, *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)
            selection[k] = v = event.set(v, pf, pfunction(pf_value, *args))

    return selection
Example #6
0
def operate_string(selection, pfield_index_list, pfunction, *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)
            selection[k] = v = event.set(v, pf, pfunction(pf_value, *args))

    return selection
Example #7
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
Example #8
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
Example #9
0
def test(n, line, pf, v, expect):
    result = s.set(line, pf, v)
    did_pass = result == expect

    return did_pass, n, 'set()', str(expect), str(result)
Example #10
0
def test(n, line, pf, v, expect):
    result = s.set(line, pf, v)
    did_pass = result == expect

    return did_pass, n, 'set()', str(expect), str(result)