在产生物品时检测for-loop项是否是最后一项?

时间:2017-02-15 11:36:50

标签: python postgresql python-3.x iterator yield

我正在处理一个巨大的postgresql数据库,我已经创建了一个" fetch"功能

def fetch(cursor, batch_size=1e3):
    """An iterator that uses fetchmany to keep memory usage down"""
    while True:
        records = cursor.fetchmany(int(batch_size))
        if not records:
            break
        for record in records:
            yield record

对于我正在进行一些处理的每个项目,但是现在我有一个问题,在某些情况下最后一项将被省略,因为我正在对项目进行一些比较。只要这个比较没有在最后一个项目上产生,就不会做任何事情。

connection = psycopg2.connect(<url>)
cursor = connection.cursor()

cursor.execute(<some query>)

temp_today = 0

for row in fetch(cursor):
    item = extract_variables(row)
    date = item['datetime']
    today = date.date()
    if temp_today is 0:
        # do something with first row
        temp_today = date
    # -----------------------------------------
    # I feel like I am missing a statement here
    # something like:
    # if row == rows[-1]:
    #     do something with last row..
    # -----------------------------------------
    elif temp_today.date() == today:
        # do something with every row where 
        # the date is the same
    else:
        # do something with every row where
        # the dates ain't the same

当我使用收益时,如何处理最后一项?

对我来说,使用yield是非常重要的,因为我正在处理非常庞大的数据集,如果我不这样做,我将耗尽内存。

2 个答案:

答案 0 :(得分:0)

您可以定义另一个生成器,以便迭代返回的项目和前一个项目(如果有):

def pair( sequence):
    previous = None
    for item in sequence:
        yield (item, previous)
        previous = item

for item, previous_item in pair( mygenerator( args))
    if previous_item is None:
        # process item: first one returned
    else:
        # you can compare item and previous_item

答案 1 :(得分:0)

感谢评论中的@Peter Smit我使用了以下解决方案:

connection = psycopg2.connect(<url>)
cursor = connection.cursor()

cursor.execute(<some query>)

temp_today = 0
parsed_count = 0
cursor_count = cursor.rowcount

for row in fetch(cursor):
    item = extract_variables(row)
    date = item['datetime']
    today = date.date()
    if temp_today is 0:
        # do something with first row
        temp_today = date
    elif parsed_count == cursor_count:
        # do something with the last row
    elif temp_today.date() == today:
        # do something with every row where 
        # the date is the same
    else:
        # do something with every row where
        # the dates ain't the same