def __str__(self): def info(node, depth = 0): prefix = ' ' * depth if isstring(node): return prefix + node return prefix + node.info(), [info(n, depth+1) for n in node.children] lines = info(self.root) return '\n'.join(flatten(lines))
def extract_regex(regex, text): """Extract a list of strings from the given text using the following rules: * if the regex contains a named group called "extract" that will be returned * if the regex contains multiple numbered groups, all those will be returned (flattened) * if the regex doesn't contain any group the entire regex matching is returned Adapted from Scrapy code. """ if isinstance(regex, basestring): regex = re.compile(regex) try: strings = [regex.search(text).group('extract')] # named group except: strings = regex.findall(text) # full regex or numbered groups return flatten(strings)
def _rewriteNode(self, parnode): children = flatten(self.rewrite(c) for c in parnode.children) return (parnode.start, parnode.end), parnode.expr_name, children # pos, type, children