





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



     '''simple docstring'''






     def 	UpperCamelCase(					self	):
       return datasets.DatasetInfo(
           features=datasets.Features({"content": datasets.Value("string"	)}	)						,   supervised_keys=lowerCamelCase						,   )

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






     def 	UpperCamelCase(					self						,   lowerCamelCase						,   lowerCamelCase	):
       import apache_beam as beam

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







class       __SCREAMING_SNAKE_CASE	(						datasets.BeamBasedBuilder			):



     '''simple docstring'''






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

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






     def 	UpperCamelCase(					self						,   lowerCamelCase						,   lowerCamelCase	):
       import apache_beam as beam

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







def 				snake_case_      (		):







  '''simple docstring'''



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




def 				snake_case_      (		):







  '''simple docstring'''



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




class       __SCREAMING_SNAKE_CASE	(						__UpperCamelCase			):



     '''simple docstring'''






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

     @require_beam
     def 	UpperCamelCase(					self	):
       import apache_beam as beam

       _snake_case     		=				beam.io.parquetio.WriteToParquet

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

     @require_beam
     def 	UpperCamelCase(					self	):
       with tempfile.TemporaryDirectory() as tmp_cache_dir:
         _snake_case     		=				DummyBeamDataset(cache_dir=lowerCamelCase	)
         self.assertRaises(datasets.builder.MissingBeamOptions						,   builder.download_and_prepare	)






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









'''simple docstring'''



def 				snake_case_      (		SCREAMING_SNAKE_CASE__       ):







  '''simple docstring'''



  _snake_case     		=				[0 for i in range(len(SCREAMING_SNAKE_CASE__       )       )]

  # initialize interval's left pointer and right pointer
  _snake_case		,		_snake_case     		=				0, 0

  for i in range(1					,	len(SCREAMING_SNAKE_CASE__       )       ):
    # case when current index is inside the interval
    if i <= right_pointer:
      _snake_case     		=				min(right_pointer - i + 1					,	z_result[i - left_pointer]       )
      _snake_case     		=				min_edge

    while go_next(SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__       ):
      z_result[i] += 1

    # if new index's result gives us more right interval,
    # we've to update left_pointer and right_pointer
    if i + z_result[i] - 1 > right_pointer:
      _snake_case		,		_snake_case     		=				i, i + z_result[i] - 1

  return z_result




def 				snake_case_      (		SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__       ):







  '''simple docstring'''



  return i + z_result[i] < len(SCREAMING_SNAKE_CASE__       ) and s[z_result[i]] == s[i + z_result[i]]




def 				snake_case_      (		SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__       ):







  '''simple docstring'''



  _snake_case     		=				0
  # concatenate 'pattern' and 'input_str' and call z_function
  # with concatenated string
  _snake_case     		=				z_function(pattern + input_str       )

  for val in z_result:
    # if value is greater then length of the pattern string
    # that means this index is starting position of substring
    # which is equal to pattern string
    if val >= len(SCREAMING_SNAKE_CASE__       ):
      answer += 1

  return answer


if __name__ == "__main__":
 import doctest

 doctest.testmod()


