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 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?

2
  • 2
    have you read anything about generators? For example the first google result wiki.python.org/moin/Generators Commented Dec 16, 2018 at 19:08
  • 3
    Your homework assignment says you are supposed to "Create a generator function "batched" …" not call batched like you are doing. Commented Dec 16, 2018 at 19:10

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You very much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.