Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
0
def statement_spacer(event, spacer=1):
    '''Returns a new string with the whitespace between a statement
    and the following element altered.

    The spacer attribute is the number of whitespace characters to
    place between the statement and the following pfield.
    
    Example::
        
        >>> event.statement_spacer('i1 0 4 1.0 440')
        'i 1 0 4 1.0 440'
    
    See :term:`event`

    '''

    tokens = tokenize(event)

    # Index of statement requires initializing, in case of zero tokens.
    i = 0

    # Get index for the statement and identifier
    for i, e in enumerate(tokens):
        if element.token_type(e) == element.STATEMENT:
            break

    # Modify tokens[] item proceeding the statement
    i = i + 1
    if i < len(tokens):
        tokens[i] = ' ' * spacer + tokens[i].lstrip()

    return ''.join(tokens)
Beispiel #4
0
def statement_spacer(event, spacer=1):
    '''Returns a new string with the whitespace between a statement
    and the following element altered.

    The spacer attribute is the number of whitespace characters to
    place between the statement and the following pfield.
    
    Example::
        
        >>> event.statement_spacer('i1 0 4 1.0 440')
        'i 1 0 4 1.0 440'
    
    See :term:`event`

    '''
    
    tokens = tokenize(event)

    # Index of statement requires initializing, in case of zero tokens.
    i = 0

    # Get index for the statement and identifier    
    for i, e in enumerate(tokens):
        if element.token_type(e) == element.STATEMENT:
            break
            
    # Modify tokens[] item proceeding the statement
    i = i + 1
    if i < len(tokens):
        tokens[i] = ' ' * spacer + tokens[i].lstrip()

    return ''.join(tokens)
Beispiel #5
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 #6
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 #7
0
def set(event, pfield_index, value):
    '''Returns a new event string with the specified pfield set with
    the new value.
    
    Example::
        
        >>> event.set('i 1 0 4 1.0 440  ; A440', 5, 1138)
        'i 1 0 4 1.0 1138  ; A440'
        
    See :term:`event`, :term:`pfield_index`

    '''

    # Pfield must be of type int, as it refers to an index in a list.
    pfield_index = int(pfield_index)

    # Skip if pfield is out of range
    if pfield_index not in xrange(number_of_pfields(event)):
        return event

    tokens = tokenize(event)
    pf_index = -1
    for i, t in enumerate(tokens):
        if pf_index == -1:
            if element.token_type(t) == element.STATEMENT:
                pf_index += 1
        else:
            if element.is_valid_pfield(t):
                pf_index += 1

        if pf_index == pfield_index:

            # Create test case for str(value)
            tokens[i] = str(value)
            break

    return ''.join(tokens)
Beispiel #8
0
def set(event, pfield_index, value):
    '''Returns a new event string with the specified pfield set with
    the new value.
    
    Example::
        
        >>> event.set('i 1 0 4 1.0 440  ; A440', 5, 1138)
        'i 1 0 4 1.0 1138  ; A440'
        
    See :term:`event`, :term:`pfield_index`

    '''

    # Pfield must be of type int, as it refers to an index in a list.
    pfield_index = int(pfield_index)
    
    # Skip if pfield is out of range
    if pfield_index not in xrange(number_of_pfields(event)):
        return event
    
    tokens = tokenize(event)
    pf_index = -1
    for i, t in enumerate(tokens):
        if pf_index == -1:
            if element.token_type(t) == element.STATEMENT:
                pf_index += 1
        else:
            if element.is_valid_pfield(t):
                pf_index += 1

        if pf_index == pfield_index:
            
            # Create test case for str(value)
            tokens[i] = str(value)
            break

    return ''.join(tokens)
Beispiel #9
0
def test(n, line, expect):
    result = s.token_type(line)
    did_pass = result == expect

    return did_pass, n, 'token_type()', str(expect), str(result)
def test(n, line, expect):
    result = s.token_type(line)
    did_pass = result == expect

    return did_pass, n, "token_type()", str(expect), str(result)