def from_lines(self, lines, source=None, join=''.join): ''' Wrap an iterator over lines (strings, by default, but could be a list of lists for example). ''' if source is None: source = sample('lines: ', repr(lines)) return self(LineSource(lines, source, join))
def from_items(self, items, source=None, join=list_join, sub_list=False, line_length=80): ''' Wrap an iterator over items (or a list). ''' if source is None: source = sample('items: ', repr(items)) return self(CharacterSource(items, source, join=join, line_length=line_length, sub_list=sub_list))
def from_string(self, text, source=None, join=''.join, base=None): ''' Wrap a string. ''' if source is None: source = sample('str: ', repr(text)) if base is None: base = text return self(LineSource(StringIO(str(text)), source, join, base=base))
def from_string(self, text, source=None, join=''.join, base=None): ''' Wrap a string. ''' if source is None: source = sample('str: ', repr(text)) if base is None: base = text return self(LineSource(StringIO(text), source, join, base=base))
def fmt_stream(self): ''' Provide a standard fmt for location. ''' try: (offset, line_no, char) = s_delta(self.generator.stream) locn = fmt('{0}/{1}.{2}', offset, line_no, char) try: stream = sample('', s_line(self.generator.stream, False)[0], 9) except StopIteration: stream = '<EOS>' return (stream, offset, locn) except StopIteration: return ('<EOS>', -1, '') except TypeError: return (self.generator.stream, -1, '')
def fmt_stream(self): ''' Provide a standard fmt for location. ''' try: (offset, lineno, char) = s_delta(self.generator.stream) locn = fmt('{0}/{1}.{2}', offset, lineno, char) try: stream = sample('', s_line(self.generator.stream, False)[0], 9) except StopIteration: stream = '<EOS>' return (stream, offset, locn) except StopIteration: return ('<EOS>', -1, '') except TypeError: return (self.generator.stream, -1, '')
def from_items(self, items, source=None, join=list_join, sub_list=False, line_length=80): ''' Wrap an iterator over items (or a list). ''' if source is None: source = sample('items: ', repr(items)) return self( CharacterSource(items, source, join=join, line_length=line_length, sub_list=sub_list))
def fmt_stream(self): ''' Provide a standard format for location. ''' try: (lineno, offset, depth, _text, _source) = \ self.generator.stream.location if not isinstance(lineno, int): locn = format('{0:<3d}', offset) elif lineno < 0: locn = ' eof ' else: locn = format('{0:3d}.{1:<3d}', lineno, offset) except AttributeError: # no .location above depth = -len(self.generator.stream) locn = '<unknown>' stream = sample('', str(self.generator.stream), 9) return (stream, depth, locn)