I have this problem statement:
For optimal performance, records should be processed in batches.Create a generator function "batched" that will yield batches of 1000records at a time and can be used as follows:
for subrange, batch in batched(records, size=1000): print("Processing records %d-%d" %(subrange[0], subrange[-1])) process(batch)I have tried like this:
def myfunc(batched): for subrange, batch in batched(records, size=1000): print("Processing records %d-%d" % (subrange[0], subrange[-1])) yield(batched)But I'm not sure, since I'm new into python generators, this simply doesn't show anything on the console, no error, nothing, any ideas?
- 2have you read anything about generators? For example the first google resultwiki.python.org/moin/Generatorsuser8408080– user84080802018-12-16 19:08:35 +00:00CommentedDec 16, 2018 at 19:08
- 3Your homework assignment says you are supposed to "Create a generator function "batched" …" not call
batchedlike you are doing.Red Cricket– Red Cricket2018-12-16 19:10:34 +00:00CommentedDec 16, 2018 at 19:10
1 Answer1
Generators are lazy, should consume or bootstrap it in order it to do something.
See example:
def g(): print('hello world') yield 3x = g() # nothing is printed. Magic..Should either do:
x = g()x.send(None) # now will printOr:
x = g()x.next()[edit]
Notice that when doing.next() explicitly, eventually you'll getStopIteration error, so you should catch it or suppress it
1 Comment
Explore related questions
See similar questions with these tags.
