def get_trailing_comment(event): '''Returns a string of a trailing inline comment for an event. Returns an empty string if no trailing comment is found. Example:: >>> event.get_trailing_comment('i1 0 4 1.0 440 ; comment') '; comment' See :term:`event` ''' tokens = tokenize(event) tokens.reverse() # Get the index of the first valid pfield statement for i, t in enumerate(tokens): if element.is_valid_pfield(t): break # Un-reverse list and compensate index tokens.reverse() i = len(tokens) - i return ''.join(tokens[i:]).strip()
def get_trailing_comment(event): '''Returns a string of a trailing inline comment for an event. Returns an empty string if no trailing comment is found. Example:: >>> event.get_trailing_comment('i1 0 4 1.0 440 ; comment') '; comment' See :term:`event` ''' tokens = tokenize(event) tokens.reverse() # Get the index of the first valid pfield statement for i, t in enumerate(tokens): if element.is_valid_pfield(t): break # Un-reverse list and compensate index tokens.reverse() i = len(tokens) - i return ''.join(tokens[i:]).strip()
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)
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)
def test(n, e, expect): result = element.is_valid_pfield(e) did_pass = result == expect return did_pass, n, 'is_valid_pfield', str(expect), str(result)
def test(n, e, expect): result = element.is_valid_pfield(e) did_pass = result == expect return did_pass, n, 'is_valid_pfield', str(expect), str(result)