'''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 ):




    '''simple docstring'''







    def       _UpperCamelCase    (       self )							-> Union[str, Any]:
         return datasets.DatasetInfo(
             features=datasets.Features({'content': datasets.Value('string' )} )    ,							supervised_keys=lowercase__    ,							)






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ )							-> List[Any]:
         return [datasets.SplitGenerator(name=datasets.Split.TRAIN    ,							gen_kwargs={'examples': get_test_dummy_examples()} )]






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ )							-> Tuple:
         import apache_beam as beam

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






class 				UpperCAmelCase (       datasets.BeamBasedBuilder ):




    '''simple docstring'''







    def       _UpperCamelCase    (       self )							-> Any:
         return datasets.DatasetInfo(
             features=datasets.Features({'a': datasets.Sequence({'b': datasets.Value('string' )} )} )    ,							supervised_keys=lowercase__    ,							)






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ )							-> Dict:
         return [
             datasets.SplitGenerator(name=datasets.Split.TRAIN    ,							gen_kwargs={'examples': get_test_nested_examples()} )
         ]






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ )							-> str:
         import apache_beam as beam

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






def     __lowerCAmelCase						(       )      ->	int:





     '''simple docstring'''





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


def     __lowerCAmelCase						(       )      ->	Tuple:





     '''simple docstring'''





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






class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







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






    @require_beam
    def       _UpperCamelCase    (       self )							-> str:
         import apache_beam as beam

         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       beam.io.parquetio.WriteToParquet

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






    @require_beam
    def       _UpperCamelCase    (       self )							-> List[str]:
         with tempfile.TemporaryDirectory() as tmp_cache_dir:
              SCREAMING_SNAKE_CASE					:			Tuple    	=       DummyBeamDataset(cache_dir=lowercase__ )
              self.assertRaises(datasets.builder.MissingBeamOptions    ,							builder.download_and_prepare )






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




'''simple docstring'''





def     __lowerCAmelCase						(       a_       ,      a_		)      ->	str:





     '''simple docstring'''





     if number < 0 or shift_amount < 0:
          raise ValueError('both inputs must be positive integers'		)

     SCREAMING_SNAKE_CASE					:			str    	=       str(bin(a_		)		)
     binary_number += "0" * shift_amount
     return binary_number


def     __lowerCAmelCase						(       a_       ,      a_		)      ->	str:





     '''simple docstring'''





     if number < 0 or shift_amount < 0:
          raise ValueError('both inputs must be positive integers'		)

     SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       str(bin(a_		)		)[2:]
     if shift_amount >= len(a_		):
          return "0b0"
     SCREAMING_SNAKE_CASE					:			Dict    	=       binary_number[: len(a_		) - shift_amount]
     return "0b" + shifted_binary_number


def     __lowerCAmelCase						(       a_       ,      a_		)      ->	str:





     '''simple docstring'''





     if number >= 0:  # Get binary representation of positive number
          SCREAMING_SNAKE_CASE					:			Tuple    	=       '0' + str(bin(a_		)		).strip('-'		)[2:]
     else:  # Get binary (2's complement) representation of negative number
          SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       len(bin(a_		)[3:]		)  # Find 2's complement of number
          SCREAMING_SNAKE_CASE					:			Any    	=       bin(abs(a_		) - (1 << binary_number_length)		)[3:]
          SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       (
              '1' + '0' * (binary_number_length - len(a_		)) + binary_number
          )

     if shift_amount >= len(a_		):
          return "0b" + binary_number[0] * len(a_		)
     return (
         "0b"
         + binary_number[0] * shift_amount
         + binary_number[: len(a_		) - shift_amount]
     )


if __name__ == "__main__":
 import doctest

 doctest.testmod()



