python - Take a few elements from an iterable, do something, take a few elements more, and so on -


Here a python code is moving one server from one server to another server on another server:

  cursor1.execute ("" "d1.Doc_Id, d2.Doc_Id, select from d1.Doc_Id = r.Doc_Id Inside Reference D1 from within document D1R.R9 = d2.T9 document D2 Join "" "(" Transaction Start ") cursor2.executemany cursor2.execute (" citation_t check ignore enter (citing_doc_id, cited_doc_id) values ​​(?,?) ", Cursor1) cursor2.execute (" COMMIT ")  

Now, for the exhibition, assume that the transaction Before the commitment, the goal goes out of place in the hard drive, and thus the commission is lost but I am using the transaction for performance reasons, not for the atom. Therefore, I want to store the data with hard work so that it is full and I can show it to my boss. Again, this is for the exhibition, the actual question is below. In that case, I would rather do:

  select cursor1.execute ("" "d1.Doc_Id, d2.Doc_Id d1.Doc_Id = r.Doc_Id interiors on D1 interiors from document Refer to the R reference Dark_spawn in MAX_ELEMENTS_TO_MOVE_TOGETHER = 1000 dark_spawn = some_dark_magic_with_iterable (cursor 1, MAX_ELEMENTS_TO_MOVE_TOGETHER) for partial_itable: Cursor 2. Execute ("Start Transaction") cursor 2.executemany ("quotation_t in the INITERT IGNORE (citing_doc_id, cited_doc_id) values ​​( ?,?) ", Partial_iterable) cursor2.execute (" COMMIT ")  

My question is to fill in some_dark_magic_with_iterable < Is the only way / code>, which pauses some kind of Iterator in between?

Make a Generator!: P

  def some_dark_magic_with_iterable (curs, nelems): race = curs.fetchmany (nelems) while race: produce race race = curs.fetchmany (nelems)  < / Pre> 

OK, okay ... generic etter ...

  def some_dark_magic_with_iterable (repeat, blue): try: while correct: res = [] while len (res ) & Lt; Hose: res.append (iterable.next ()), in addition to rotation yield leakage: If the ridge: yield leak  

Comments