Example #1
0
def _align_block(block, s=1, p=1, c=2, m=1):
    # Split score block into individual events
    event_list = block.splitlines()

    # Get the number of columns in block
    n_columns = max(event.number_of_pfields(e) for e in event_list)

    # Initialize pfield lengths with zeros
    pf_lengths = [0] * n_columns

    # Get the longest pfield lengths for each column
    for row in event_list:
        for i, col in enumerate(event.get_pfield_list(row)):
            pf_lengths[i] = max(len(col), pf_lengths[i])

    # Align pfields, without trailing comments
    align_pfields = []
    for e in event_list:
        line = []

        # Append score statement with whitespace determined by s
        line.append(event.get(e, 0).ljust(s + 1))

        # Appened each pfield with whitespace determined by p and m
        for i, L in enumerate(pf_lengths[1:event.number_of_pfields(e)]):
            line.append(event.get(e, i + 1).ljust(max(L, m)))
            line.append(''.ljust(p))

        # Strip end of any potential whitespace
        align_pfields.append(''.join(line).rstrip(' '))

    # Add trailing comments
    align_comments = []
    longest_event = max(len(e) for e in align_pfields)

    for i, e in enumerate(event_list):
        line = []
        line.append(''.join(align_pfields[i]))

        # Add whitespace and comment if comment exists
        if event.get_trailing_comment(e):
            line.append(''.ljust(longest_event - len(align_pfields[i])))
            line.append(''.ljust(c))
            line.append(event.get_trailing_comment(e))

        align_comments.append(''.join(line))

    return '\n'.join(align_comments) + '\n'
Example #2
0
def _align_block(block, s=1, p=1, c=2, m=1):
    # Split score block into individual events
    event_list = block.splitlines()

    # Get the number of columns in block
    n_columns = max(event.number_of_pfields(e) for e in event_list)

    # Initialize pfield lengths with zeros
    pf_lengths = [0] * n_columns

    # Get the longest pfield lengths for each column
    for row in event_list:
        for i, col in enumerate(event.get_pfield_list(row)):
            pf_lengths[i] = max(len(col), pf_lengths[i])
        
    # Align pfields, without trailing comments
    align_pfields = []
    for e in event_list:
        line = []
        
        # Append score statement with whitespace determined by s
        line.append(event.get(e, 0).ljust(s + 1))
        
        # Appened each pfield with whitespace determined by p and m
        for i, L in enumerate(pf_lengths[1:event.number_of_pfields(e)]):
            line.append(event.get(e, i + 1).ljust(max(L, m)))
            line.append(''.ljust(p))
            
        # Strip end of any potential whitespace
        align_pfields.append(''.join(line).rstrip(' '))

    # Add trailing comments
    align_comments = []
    longest_event = max(len(e) for e in align_pfields)

    for i, e in enumerate(event_list):
        line = []
        line.append(''.join(align_pfields[i]))

        # Add whitespace and comment if comment exists
        if event.get_trailing_comment(e):
            line.append(''.ljust(longest_event - len(align_pfields[i])))
            line.append(''.ljust(c))
            line.append(event.get_trailing_comment(e))

        align_comments.append(''.join(line))

    return '\n'.join(align_comments) + '\n'
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)
def test(n, line, expect):
    result = s.number_of_pfields(line)
    did_pass = result == expect

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