






"""simple docstring"""




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        UpperCamelCase			(	datasets.BeamBasedBuilder      ):


 def _lowercase	(     self		:						List[Any]							)					->       List[str]:
   return datasets.DatasetInfo(
       features=datasets.Features({"""content""": datasets.Value("""string"""							)}							)							,						supervised_keys=UpperCAmelCase__							,						)


 def _lowercase	(     self		:						str							,						UpperCAmelCase__		:						Union[str, Any]							,						UpperCAmelCase__		:						Union[str, Any]							)					->       List[Any]:
   return [datasets.SplitGenerator(name=datasets.Split.TRAIN							,						gen_kwargs={"""examples""": get_test_dummy_examples()}							)]







 def _lowercase	(     self		:						Optional[Any]							,						UpperCAmelCase__		:						Any							,						UpperCAmelCase__		:						Dict							)					->       int:
   import apache_beam as beam

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







class        UpperCamelCase			(	datasets.BeamBasedBuilder      ):


 def _lowercase	(     self		:						Optional[int]							)					->       Optional[Any]:
   return datasets.DatasetInfo(
       features=datasets.Features({"""a""": datasets.Sequence({"""b""": datasets.Value("""string"""							)}							)}							)							,						supervised_keys=UpperCAmelCase__							,						)


 def _lowercase	(     self		:						List[str]							,						UpperCAmelCase__		:						Optional[int]							,						UpperCAmelCase__		:						Any							)					->       Tuple:
   return [
       datasets.SplitGenerator(name=datasets.Split.TRAIN							,						gen_kwargs={"""examples""": get_test_nested_examples()}							)
   ]







 def _lowercase	(     self		:						List[str]							,						UpperCAmelCase__		:						Union[str, Any]							,						UpperCAmelCase__		:						Optional[int]							)					->       int:
   import apache_beam as beam

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







def 			lowerCAmelCase__     (     ):



  '''simple docstring'''
  return [(i, {"content": content}) for i, content in enumerate(["""foo""", """bar""", """foobar"""]				)]

def 			lowerCAmelCase__     (     ):



  '''simple docstring'''
  return [(i, {"a": {"b": [content]}}) for i, content in enumerate(["""foo""", """bar""", """foobar"""]				)]






class        UpperCamelCase			(	__UpperCAmelCase      ):


 @require_beam
 def _lowercase	(     self		:						Tuple							)					->       Any:
   _a  :       int             =	len(get_test_dummy_examples()							)
   with tempfile.TemporaryDirectory() as tmp_cache_dir:
     _a  :       Tuple             =	DummyBeamDataset(cache_dir=UpperCAmelCase__							,						beam_runner="""DirectRunner"""							)
     builder.download_and_prepare()
     self.assertTrue(
         os.path.exists(
             os.path.join(UpperCAmelCase__							,						builder.name							,						"""default"""							,						"""0.0.0"""							,						f"""{builder.name}-train.arrow"""							)							)							)
     self.assertDictEqual(builder.info.features							,						datasets.Features({"""content""": datasets.Value("""string"""							)}							)							)
     _a  :       Dict             =	builder.as_dataset()
     self.assertEqual(dset["""train"""].num_rows							,						UpperCAmelCase__							)
     self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples							,						UpperCAmelCase__							)
     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(UpperCAmelCase__							,						builder.name							,						"""default"""							,						"""0.0.0"""							,						"""dataset_info.json"""							)							)							)
     del dset


 @require_beam
 def _lowercase	(     self		:						Optional[Any]							)					->       List[Any]:
   import apache_beam as beam

   _a  :       int             =	beam.io.parquetio.WriteToParquet

   _a  :       str             =	len(get_test_dummy_examples()							)
   with tempfile.TemporaryDirectory() as tmp_cache_dir:
     _a  :       Union[str, Any]             =	DummyBeamDataset(cache_dir=UpperCAmelCase__							,						beam_runner="""DirectRunner"""							)
     with patch("""apache_beam.io.parquetio.WriteToParquet"""							) as write_parquet_mock:
       _a  :       Tuple             =	partial(UpperCAmelCase__							,						num_shards=2							)
       builder.download_and_prepare()
     self.assertTrue(
         os.path.exists(
             os.path.join(
                 UpperCAmelCase__							,						builder.name							,						"""default"""							,						"""0.0.0"""							,						f"""{builder.name}-train-00000-of-00002.arrow"""							)							)							)
     self.assertTrue(
         os.path.exists(
             os.path.join(
                 UpperCAmelCase__							,						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  :       Any             =	builder.as_dataset()
     self.assertEqual(dset["""train"""].num_rows							,						UpperCAmelCase__							)
     self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples							,						UpperCAmelCase__							)
     # 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(UpperCAmelCase__							,						builder.name							,						"""default"""							,						"""0.0.0"""							,						"""dataset_info.json"""							)							)							)
     del dset


 @require_beam
 def _lowercase	(     self		:						str							)					->       List[str]:
   with tempfile.TemporaryDirectory() as tmp_cache_dir:
     _a  :       List[Any]             =	DummyBeamDataset(cache_dir=UpperCAmelCase__							)
     self.assertRaises(datasets.builder.MissingBeamOptions							,						builder.download_and_prepare							)







 @require_beam
 def _lowercase	(     self		:						str							)					->       Optional[int]:
   _a  :       str             =	len(get_test_nested_examples()							)
   with tempfile.TemporaryDirectory() as tmp_cache_dir:
     _a  :       str             =	NestedBeamDataset(cache_dir=UpperCAmelCase__							,						beam_runner="""DirectRunner"""							)
     builder.download_and_prepare()
     self.assertTrue(
         os.path.exists(
             os.path.join(UpperCAmelCase__							,						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  :       Any             =	builder.as_dataset()
     self.assertEqual(dset["""train"""].num_rows							,						UpperCAmelCase__							)
     self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples							,						UpperCAmelCase__							)
     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(UpperCAmelCase__							,						builder.name							,						"""default"""							,						"""0.0.0"""							,						"""dataset_info.json"""							)							)							)
     del dset






'''simple docstring'''




# 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

lowerCamelCase							:str				    =					TypeVar('''T''')







class 	_lowerCAmelCase							(			Generic[T] ):


	def __init__(self       ,      lowercase = True							):
				A_	:						dict[T, list[T]]	=     {}  # dictionary of lists
				A_	:						Any	=     directed


	def     _a	(self       ,      lowercase       ,      lowercase							):

				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(lowercase							)
										self.adj_list[destination_vertex].append(lowercase							)
							# 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(lowercase							)
										A_	:						Dict	=     [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(lowercase							)
										A_	:						int	=     [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_	:						Optional[Any]	=     [destination_vertex]
										A_	:						Tuple	=     [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(lowercase							)
							# 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(lowercase							)
										A_	:						Tuple	=     []
							# 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_	:						Tuple	=     [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_	:						int	=     [destination_vertex]
										A_	:						List[str]	=     []

				return self


	def __repr__(self							):
				return pformat(self.adj_list							)