Reputation: 3907
I have this problem statement:
For optimal performance, records should be processed in batches. Create a generator function "batched" that will yield batches of 1000 records 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?
Upvotes: 1
Views: 164
Reputation: 3758
Generators are lazy, should consume or bootstrap it in order it to do something.
See example:
def g():
print('hello world')
yield 3
x = g() # nothing is printed. Magic..
Should either do:
x = g()
x.send(None) # now will print
Or:
x = g()
x.next()
[edit]
Notice that when doing .next()
explicitly, eventually you'll get StopIteration
error, so you should catch it or suppress it
Upvotes: 2