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 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 _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'
def align(score, s=1, p=1, c=2, m=1, statements='fi'): '''Auto-aligns groupings of i-events. A grouping is a block of sequential events of the same statement type. ''' score_list = score.splitlines(True) # Each event is individually handled output = [] # Collects events for the new score block = [] # Collects groupings of events # Process each individual event for e in score_list: if event.get(e, 0) in list(statements): block.append(e) else: if block: b = ''.join(block) output.append(_align_block(b, s, p, c, m)) block = [] output.append(e) if block: b = ''.join(block) output.append(_align_block(b, s, p, c, m)) return ''.join(output)
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
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
def test(n, line, pf, expect): result = s.get(line, pf) did_pass = result == expect return did_pass, n, 'get()', str(expect), str(result)