递归函数中的python生成器

时间:2015-12-29 23:59:54

标签: python recursion yield

我看了很多帖子但仍然不太明白。我有以下代码,它在代码中嵌入的sql文本中打印出此查询的外层。

K.a
K.b
I
J
K
1
2

我不清楚在递归调用函数时如何获取内层。我在https://github.com/andialbrecht/sqlparse/blob/master/examples/extract_table_names.py之后对此进行了图案化,这非常相似且有效。提前谢谢。

# This example illustrates how to extract table names from nested
# SELECT statements.

# See:
# http://groups.google.com/group/sqlparse/browse_thread/thread/b0bd9a022e9d4895

sql = """
select K.a,K.b from (select H.b from (select G.c from (select F.d from
(select E.e from A, B, C, D, E), F), G), H), I, J, K order by 1,2;
"""
import sqlparse
from sqlparse.sql import IdentifierList, Identifier
from sqlparse.tokens import Keyword, DML


def is_subselect(parsed):
    if not parsed.is_group():
        return False
    for item in parsed.tokens:
        if item.ttype is DML and item.value.upper() == 'SELECT':
            return True
    return False


def extract_from_part(parsed):
    from_seen = False
    for item in parsed.tokens:
        if item.ttype is Keyword:
            lastKeyword = str(item)
        #if from_seen:
        if is_subselect(item):
            for x in extract_from_part(item):
                if type(item) in(sqlparse.sql.IdentifierList,sqlparse.sql.Identifier):
                    yield x
        else:
            if type(item) in( sqlparse.sql.IdentifierList,sqlparse.sql.Identifier):
                yield item

def extract_table_identifiers(token_stream):
    for item in token_stream:
        if isinstance(item, IdentifierList):
            for identifier in item.get_identifiers():
                    yield str(identifier)
        elif isinstance(item, Identifier):
            #yield item.get_name()
            yield str(identifier)
        # It's a bug to check for Keyword here, but in the example
        # above some tables names are identified as keywords...
        elif item.ttype is Keyword:
            yield item.value

def extract_tables():
    stream = extract_from_part(sqlparse.parse(sql)[0])
    return list(extract_table_identifiers(stream))
    #return stream


if __name__ == '__main__':
    #print('Tables: %s' % ', '.join(extract_tables()))
    ids = extract_tables()
    for x in ids:
        print x

1 个答案:

答案 0 :(得分:1)

在递归调用后,您在循环中执行的类型检查会测试错误的对象的类型:

    if is_subselect(item):
        for x in extract_from_part(item):
            # next line should check type(x) rather than type(item)
            if type(item)  in(sqlparse.sql.IdentifierList,sqlparse.sql.Identifier):
                yield x

但是,类型检查是不必要的,因为非递归情况无论如何都只会产生IdentifierIdentifierList项。所以,只需摆脱那一行(并取消以下yield)。

虽然这不是导致此问题的原因,但我建议您使用isinstance而不是直接比较类型:

if isinstance(item, (sqlparse.sql.Identifier, sqlparse.sql.IdentifierList)):
    yield item