code: def test_create_batches_groups_correctly_with_max_instances(self):
      # If we knew all the instances, the correct order is 4 -> 2 -> 0 -> 1 -> 3.
      # Here max_instances_in_memory is 3, so we load instances [0, 1, 2]
      # and then bucket them by size into batches of size 2 to get [2, 0] -> [1].
      # Then we load the remaining instances and bucket them by size to get [4, 3].
      iterator = BucketIterator(
          batch_size=2,
          padding_noise=0,
          sorting_keys=[("text", "num_tokens")],
          max_instances_in_memory=3,
      )
      for test_instances in (self.instances, self.lazy_instances):
          batches = list(
              iterator._create_batches(test_instances, shuffle=False)
          )
          grouped_instances = [batch.instances for batch in batches]
          assert grouped_instances == [
              [self.instances[2], self.instances[0]],
              [self.instances[1]],
              [self.instances[4], self.instances[3]],
          ]
  
file_path: magnitude/pymagnitude/third_party/allennlp/tests/data/iterators/bucket_iterator_test.py
imports: from __future__ import with_statement
  from __future__ import absolute_import
  import pytest
  from allennlp.common import Params
  from allennlp.common.checks import ConfigurationError
  from allennlp.data.iterators import BucketIterator
  from allennlp.tests.data.iterators.basic_iterator_test import IteratorTest
label_desc: Use image node_id 2 for calling the TestBucketIterator obj's underlying member method code with example usage: obj.test_create_batches_groups_correctly_with_max_instances() without return types