




import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam




class       a__	( datasets.BeamBasedBuilder				):







     def       lowerCAmelCase_    (     self			)  ->   Optional[Any]:



          '''simple docstring'''



          return datasets.DatasetInfo(
              features=datasets.Features({"content": datasets.Value("string"			)}			)  ,		supervised_keys=A  ,		)







     def       lowerCAmelCase_    (     self  ,		A  ,		A			)  ->   Any:



          '''simple docstring'''



          return [datasets.SplitGenerator(name=datasets.Split.TRAIN  ,		gen_kwargs={"examples": get_test_dummy_examples()}			)]







     def       lowerCAmelCase_    (     self  ,		A  ,		A			)  ->   Optional[Any]:



          '''simple docstring'''



          import apache_beam as beam

          return pipeline | "Load Examples" >> beam.Create(A			)







class       a__	( datasets.BeamBasedBuilder				):







     def       lowerCAmelCase_    (     self			)  ->   List[str]:



          '''simple docstring'''



          return datasets.DatasetInfo(
              features=datasets.Features({"a": datasets.Sequence({"b": datasets.Value("string"			)}			)}			)  ,		supervised_keys=A  ,		)







     def       lowerCAmelCase_    (     self  ,		A  ,		A			)  ->   Any:



          '''simple docstring'''



          return [
              datasets.SplitGenerator(name=datasets.Split.TRAIN  ,		gen_kwargs={"examples": get_test_nested_examples()}			)
          ]







     def       lowerCAmelCase_    (     self  ,		A  ,		A			)  ->   Union[str, Any]:



          '''simple docstring'''



          import apache_beam as beam

          return pipeline | "Load Examples" >> beam.Create(A			)







def  SCREAMING_SNAKE_CASE							(       )      ->					Optional[Any]:
     return [(i, {"content": content}) for i, content in enumerate(["foo", "bar", "foobar"])]

def  SCREAMING_SNAKE_CASE							(       )      ->					Tuple:
     return [(i, {"a": {"b": [content]}}) for i, content in enumerate(["foo", "bar", "foobar"])]





class       a__	( UpperCamelCase__				):







     @require_beam
     def       lowerCAmelCase_    (     self			)  ->   int:



          '''simple docstring'''



          a     =				len(get_test_dummy_examples()			)
          with tempfile.TemporaryDirectory() as tmp_cache_dir:
               a     =				DummyBeamDataset(cache_dir=A  ,		beam_runner="DirectRunner"			)
               builder.download_and_prepare()
               self.assertTrue(
                   os.path.exists(
                       os.path.join(A  ,		builder.name  ,		"default"  ,		"0.0.0"  ,		F'''{builder.name}-train.arrow'''			)			)			)
               self.assertDictEqual(builder.info.features  ,		datasets.Features({"content": datasets.Value("string"			)}			)			)
               a     =				builder.as_dataset()
               self.assertEqual(dset["train"].num_rows  ,		A			)
               self.assertEqual(dset["train"].info.splits["train"].num_examples  ,		A			)
               self.assertDictEqual(dset["train"][0]  ,		get_test_dummy_examples()[0][1]			)
               self.assertDictEqual(
                   dset["train"][expected_num_examples - 1]  ,		get_test_dummy_examples()[expected_num_examples - 1][1]			)
               self.assertTrue(
                   os.path.exists(os.path.join(A  ,		builder.name  ,		"default"  ,		"0.0.0"  ,		"dataset_info.json"			)			)			)
               del dset







     @require_beam
     def       lowerCAmelCase_    (     self			)  ->   Optional[int]:



          '''simple docstring'''



          import apache_beam as beam

          a     =				beam.io.parquetio.WriteToParquet

          a     =				len(get_test_dummy_examples()			)
          with tempfile.TemporaryDirectory() as tmp_cache_dir:
               a     =				DummyBeamDataset(cache_dir=A  ,		beam_runner="DirectRunner"			)
               with patch("apache_beam.io.parquetio.WriteToParquet"			) as write_parquet_mock:
                    a     =				partial(A  ,		num_shards=2			)
                    builder.download_and_prepare()
               self.assertTrue(
                   os.path.exists(
                       os.path.join(
                           A  ,		builder.name  ,		"default"  ,		"0.0.0"  ,		F'''{builder.name}-train-00000-of-00002.arrow'''			)			)			)
               self.assertTrue(
                   os.path.exists(
                       os.path.join(
                           A  ,		builder.name  ,		"default"  ,		"0.0.0"  ,		F'''{builder.name}-train-00000-of-00002.arrow'''			)			)			)
               self.assertDictEqual(builder.info.features  ,		datasets.Features({"content": datasets.Value("string"			)}			)			)
               a     =				builder.as_dataset()
               self.assertEqual(dset["train"].num_rows  ,		A			)
               self.assertEqual(dset["train"].info.splits["train"].num_examples  ,		A			)
               # Order is not preserved when sharding, so we just check that all the elements are there
               self.assertListEqual(sorted(dset["train"]["content"]			)  ,		sorted(["foo", "bar", "foobar"]			)			)
               self.assertTrue(
                   os.path.exists(os.path.join(A  ,		builder.name  ,		"default"  ,		"0.0.0"  ,		"dataset_info.json"			)			)			)
               del dset







     @require_beam
     def       lowerCAmelCase_    (     self			)  ->   Any:



          '''simple docstring'''



          with tempfile.TemporaryDirectory() as tmp_cache_dir:
               a     =				DummyBeamDataset(cache_dir=A			)
               self.assertRaises(datasets.builder.MissingBeamOptions  ,		builder.download_and_prepare			)







     @require_beam
     def       lowerCAmelCase_    (     self			)  ->   Any:



          '''simple docstring'''



          a     =				len(get_test_nested_examples()			)
          with tempfile.TemporaryDirectory() as tmp_cache_dir:
               a     =				NestedBeamDataset(cache_dir=A  ,		beam_runner="DirectRunner"			)
               builder.download_and_prepare()
               self.assertTrue(
                   os.path.exists(
                       os.path.join(A  ,		builder.name  ,		"default"  ,		"0.0.0"  ,		F'''{builder.name}-train.arrow'''			)			)			)
               self.assertDictEqual(
                   builder.info.features  ,		datasets.Features({"a": datasets.Sequence({"b": datasets.Value("string"			)}			)}			)			)
               a     =				builder.as_dataset()
               self.assertEqual(dset["train"].num_rows  ,		A			)
               self.assertEqual(dset["train"].info.splits["train"].num_examples  ,		A			)
               self.assertDictEqual(dset["train"][0]  ,		get_test_nested_examples()[0][1]			)
               self.assertDictEqual(
                   dset["train"][expected_num_examples - 1]  ,		get_test_nested_examples()[expected_num_examples - 1][1]			)
               self.assertTrue(
                   os.path.exists(os.path.join(A  ,		builder.name  ,		"default"  ,		"0.0.0"  ,		"dataset_info.json"			)			)			)
               del dset







# Author: OMKAR PATHAK, Nwachukwu Chidiebere

# Use a Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
from typing import Generic, TypeVar

lowercase__				:      str	   = TypeVar("T")




class       a__	( Generic[T]				):







     def __init__(     self  ,		A = True			)  ->   None:



          '''simple docstring'''



          a     =				{}  # dictionary of lists
          a     =				directed







     def       lowerCAmelCase_    (     self  ,		A  ,		A			)  ->   GraphAdjacencyList[T]:



          '''simple docstring'''



          if not self.directed:  # For undirected graphs
               # if both source vertex and destination vertex are both present in the
               # adjacency list, add destination vertex to source vertex list of adjacent
               # vertices and add source vertex to destination vertex list of adjacent
               # vertices.
               if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                    self.adj_list[source_vertex].append(A			)
                    self.adj_list[destination_vertex].append(A			)
               # if only source vertex is present in adjacency list, add destination vertex
               # to source vertex list of adjacent vertices, then create a new vertex with
               # destination vertex as key and assign a list containing the source vertex
               # as it's first adjacent vertex.
               elif source_vertex in self.adj_list:
                    self.adj_list[source_vertex].append(A			)
                    a     =				[source_vertex]
               # if only destination vertex is present in adjacency list, add source vertex
               # to destination vertex list of adjacent vertices, then create a new vertex
               # with source vertex as key and assign a list containing the source vertex
               # as it's first adjacent vertex.
               elif destination_vertex in self.adj_list:
                    self.adj_list[destination_vertex].append(A			)
                    a     =				[destination_vertex]
               # if both source vertex and destination vertex are not present in adjacency
               # list, create a new vertex with source vertex as key and assign a list
               # containing the destination vertex as it's first adjacent vertex also
               # create a new vertex with destination vertex as key and assign a list
               # containing the source vertex as it's first adjacent vertex.
               else:
                    a     =				[destination_vertex]
                    a     =				[source_vertex]
          else:  # For directed graphs
               # if both source vertex and destination vertex are present in adjacency
               # list, add destination vertex to source vertex list of adjacent vertices.
               if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                    self.adj_list[source_vertex].append(A			)
               # if only source vertex is present in adjacency list, add destination
               # vertex to source vertex list of adjacent vertices and create a new vertex
               # with destination vertex as key, which has no adjacent vertex
               elif source_vertex in self.adj_list:
                    self.adj_list[source_vertex].append(A			)
                    a     =				[]
               # if only destination vertex is present in adjacency list, create a new
               # vertex with source vertex as key and assign a list containing destination
               # vertex as first adjacent vertex
               elif destination_vertex in self.adj_list:
                    a     =				[destination_vertex]
               # if both source vertex and destination vertex are not present in adjacency
               # list, create a new vertex with source vertex as key and a list containing
               # destination vertex as it's first adjacent vertex. Then create a new vertex
               # with destination vertex as key, which has no adjacent vertex
               else:
                    a     =				[destination_vertex]
                    a     =				[]

          return self







     def __repr__(     self			)  ->   str:



          '''simple docstring'''



          return pformat(self.adj_list			)

