

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



    '''simple docstring'''




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




    def UpperCamelCase__		( self ,lowerCamelCase_ ,lowerCamelCase_    )    ->      str:
          return [datasets.SplitGenerator(name=datasets.Split.TRAIN ,gen_kwargs={"""examples""": get_test_dummy_examples()}    )]




    def UpperCamelCase__		( self ,lowerCamelCase_ ,lowerCamelCase_    )    ->      Optional[int]:
          import apache_beam as beam

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





class lowerCamelCase__   (				datasets.BeamBasedBuilder    ):



    '''simple docstring'''




    def UpperCamelCase__		( self    )    ->      List[Any]:
          return datasets.DatasetInfo(
              features=datasets.Features({"""a""": datasets.Sequence({"""b""": datasets.Value("""string"""    )}    )}    ) ,supervised_keys=lowerCamelCase_ ,)




    def UpperCamelCase__		( self ,lowerCamelCase_ ,lowerCamelCase_    )    ->      Tuple:
          return [
              datasets.SplitGenerator(name=datasets.Split.TRAIN ,gen_kwargs={"""examples""": get_test_nested_examples()}    )
          ]




    def UpperCamelCase__		( self ,lowerCamelCase_ ,lowerCamelCase_    )    ->      Any:
          import apache_beam as beam

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




def   _A					(	):



      """simple docstring"""






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



def   _A					(	):



      """simple docstring"""






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





class lowerCamelCase__   (				SCREAMING_SNAKE_CASE    ):



    '''simple docstring'''




    @require_beam
    def UpperCamelCase__		( self    )    ->      Optional[Any]:
          A				  =		len(get_test_dummy_examples()    )
          with tempfile.TemporaryDirectory() as tmp_cache_dir:
                A				  =		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"""    )}    )    )
                A				  =		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    )    ->      List[str]:
          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=lowerCamelCase_ ,beam_runner="""DirectRunner"""    )
                with patch("""apache_beam.io.parquetio.WriteToParquet"""    ) as write_parquet_mock:
                      A				  =		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"""    )}    )    )
                A				  =		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    )    ->      Optional[int]:
          with tempfile.TemporaryDirectory() as tmp_cache_dir:
                A				  =		DummyBeamDataset(cache_dir=lowerCamelCase_    )
                self.assertRaises(datasets.builder.MissingBeamOptions ,builder.download_and_prepare    )




    @require_beam
    def UpperCamelCase__		( self    )    ->      Optional[Any]:
          A				  =		len(get_test_nested_examples()    )
          with tempfile.TemporaryDirectory() as tmp_cache_dir:
                A				  =		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"""    )}    )}    )    )
                A				  =		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"""


import unittest

from knapsack import greedy_knapsack as kp





class lowerCamelCase__   (				unittest.TestCase    ):



    '''simple docstring'''




    def UpperCamelCase__		( self    )    ->      Any:
          A				  =		[1_0, 2_0, 3_0, 4_0, 5_0, 6_0]
          A				  =		[2, 4, 6, 8, 1_0, 1_2]
          A				  =		1_0_0
          self.assertEqual(kp.calc_profit(lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_    ) ,2_1_0    )




    def UpperCamelCase__		( self    )    ->      List[Any]:
          self.assertRaisesRegex(lowerCamelCase_ ,"""max_weight must greater than zero."""    )




    def UpperCamelCase__		( self    )    ->      Any:
          self.assertRaisesRegex(lowerCamelCase_ ,"""Weight can not be negative."""    )




    def UpperCamelCase__		( self    )    ->      str:
          self.assertRaisesRegex(lowerCamelCase_ ,"""Profit can not be negative."""    )




    def UpperCamelCase__		( self    )    ->      Optional[int]:
          self.assertRaisesRegex(lowerCamelCase_ ,"""max_weight must greater than zero."""    )




    def UpperCamelCase__		( self    )    ->      str:
          self.assertRaisesRegex(
              lowerCamelCase_ ,"""The length of profit and weight must be same."""    )


if __name__ == "__main__":
  unittest.main()




