2017-02-15 4 views
0

私は "フェッチ"機能を作成した巨大なpostgresqlデータベースを処理しています。各項目についてはfor-loopアイテムがアイテムを生成するときに最後に検出されたかどうかを検出しますか?

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 

収量を使用しているときに最後の項目で何かしますか?

私は非常に巨大なデータセットを扱っているので、私は歩留まりを使用することが非常に重要であり、そうでなければ私はメモリが足りなくなります。あなたがアイテムを反復処理することができるようにあなたが別の発電機を定義することができ

+1

カーソルから結果セットの行数を取得することは可能でしょうか?次に、カウンター(列挙)とその番号を比較するだけです。 –

+1

'...私はアイテム間の比較をしています.'あなたはデータベースでこれを行うことができます(ウィンドウ関数を使うか、ある種の自己結合によって) – wildplasser

答えて

0

ありがとう:

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 
0

が返され、前のもの(もしあれば):私は以下のソリューションを使用したコメントから@Peterスミットへ

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 
関連する問題