1

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?

Aaron_ab's user avatar
Aaron_ab
3,8044 gold badges33 silver badges44 bronze badges
askedDec 16, 2018 at 19:04
NeoVe's user avatar
2
  • 2
    have you read anything about generators? For example the first google resultwiki.python.org/moin/GeneratorsCommentedDec 16, 2018 at 19:08
  • 3
    Your homework assignment says you are supposed to "Create a generator function "batched" …" not callbatched like you are doing.CommentedDec 16, 2018 at 19:10

1 Answer1

2

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 print

Or:

x = g()x.next()

[edit]

Notice that when doing.next() explicitly, eventually you'll getStopIteration error, so you should catch it or suppress it

answeredDec 16, 2018 at 19:35
Aaron_ab's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You very much

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.