class_docstr: Shuffles and writes Examples to sharded TFRecord files.
  
  The number of shards is computed automatically.
class_docstr_tok: ["Shuffles","and","writes","Examples","to","sharded","TFRecord","files",".","The","number","of","shards","is","computed","automatically","."]
docstr: Effectively writes examples to the tfrecord files.
docstr_tok: ["Effectively","writes","examples","to","the","tfrecord","files","."]
code: def finalize(self) -> Tuple[List[int], int]:
      """Effectively writes examples to the tfrecord files."""
      filename = self._filename_template.sharded_filepaths_pattern()
      shard_specs = _get_shard_specs(
          self._num_examples,
          self._shuffler.size,
          self._shuffler.bucket_lengths,
          self._filename_template,
          self._shard_config,
      )
      # Here we just loop over the examples, and don't use the instructions, just
      # the final number of examples in every shard. Instructions could be used to
      # parallelize, but one would need to be careful not to sort buckets twice.
      examples_generator = iter(
          utils.tqdm(
              self._shuffler,
              desc=f"Shuffling {filename}...",
              total=self._num_examples,
              unit=" examples",
              leave=False,
          )
      )
      try:
          for shard_spec in shard_specs:
              iterator = itertools.islice(
                  examples_generator, 0, shard_spec.examples_number
              )
              record_keys = _write_examples(
                  shard_spec.path, iterator, self._file_format
              )
  
              # No shard keys returned (e.g: TFRecord format), index cannot be
              # created.
              if not record_keys:
                  continue
  
              # Number of `shard_keys` received should match the number of examples
              # written in this shard.
              if len(record_keys) != int(shard_spec.examples_number):
                  raise RuntimeError(
                      f"Length of example `keys` ({len(record_keys)}) does not match "
                      f"`shard_spec.examples_number: (`{shard_spec.examples_number})"
                  )
              _write_index_file(shard_spec.index_path, record_keys)
  
      except shuffle.DuplicatedKeysError as err:
          _raise_error_for_duplicated_keys(
              err.item1, err.item2, self._serializer.example_specs
          )
  
      # Finalize the iterator to clear-up TQDM
      try:
          val = next(examples_generator)
      except StopIteration:
          pass
      else:
          raise ValueError(
              f"Shuffling more elements than expected. Additional element: {val}"
          )
  
      shard_lengths = [
          int(spec.examples_number) for spec in shard_specs
      ]
  
      logging.info(
          "Done writing %s. Number of examples: %s (shards: %s)",
          filename,
          sum(shard_lengths),
          shard_lengths,
      )
      return shard_lengths, self._shuffler.size
  
imports: import dataclasses
  import functools
  import itertools
  import json
  import os
  from typing import Any, Iterable, List, Optional, Tuple
  from absl import logging
  from etils import epath
  from tensorflow_datasets.core import example_parser
  from tensorflow_datasets.core import example_serializer
  from tensorflow_datasets.core import file_adapters
  from tensorflow_datasets.core import hashing
  from tensorflow_datasets.core import lazy_imports_lib
  from tensorflow_datasets.core import naming
  from tensorflow_datasets.core import shuffle
  from tensorflow_datasets.core import utils
  from tensorflow_datasets.core.utils import file_utils
  from tensorflow_datasets.core.utils import shard_utils
  from tensorflow_datasets.core.utils import type_utils
  from tensorflow_datasets.core.utils.lazy_imports_utils import apache_beam
label_desc: Use image node_id 3 for calling the Writer obj's underlying member method code with example usage: obj.finalize() and returns: shard_lengths,  self