bpeterso2000
5/30/2014 - 3:55 PM

Generator functions to assist with parsing paragraphs, lines and words

Generator functions to assist with parsing paragraphs, lines and words

import re
from itertools import islice

PARAGRAPH = re.compile('\n\s*\n')
WORD = re.compile('\s+|\n')


def lines(text):
    for line in text.splitlines():
        yield line


def paragraphs(text):
    for paragraph in PARAGRAPH.split(text):
        yield paragraph


def words(text):
    for word in WORD.split(text):
        yield word


def quote(text):
    return "'{}'".format(text)


def double_quote(text):
    return '"{}"'.format(text)


def unquote(text):
    return text.strip('\'"')


def get(items, start=None, stop=False, step=None):
    """
    get an index (with IndexError supression) or a slicee list from an iterator
    """
    if not (start or stop or step):
        return list(items)
    if stop is False and (not step or int(step) == 1):
        try:
            return next(islice(items, start, start + 1))
        except StopIteration:
            return
    else:
        if stop is False:
            stop = None
        return list(islice(items, start, stop, step))