



'''simple docstring'''


import os
import textwrap

import pyarrow as pa
import pytest

from datasets import ClassLabel, Features, Image
from datasets.packaged_modules.csv.csv import Csv

from ..utils import require_pil






@pytest.fixture
def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     a_       		=tmp_path / "file.csv"
     a_       		=textwrap.dedent(
         "\\n        header1,header2\n        1,2\n        10,20\n        "       )
     with open(lowercase__       ,	"w"       ) as f:
          f.write(lowercase__       )
     return str(lowercase__       )






@pytest.fixture
def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     a_       		=tmp_path / "malformed_file.csv"
     a_       		=textwrap.dedent(
         "\\n        header1,header2\n        1,2\n        10,20,\n        "       )
     with open(lowercase__       ,	"w"       ) as f:
          f.write(lowercase__       )
     return str(lowercase__       )






@pytest.fixture
def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     a_       		=tmp_path / "csv_with_image.csv"
     a_       		=textwrap.dedent(
         F"""\
        image
        {image_file}
        """       )
     with open(lowercase__       ,	"w"       ) as f:
          f.write(lowercase__       )
     return str(lowercase__       )






@pytest.fixture
def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     a_       		=tmp_path / "csv_with_label.csv"
     a_       		=textwrap.dedent(
         "\\n        label\n        good\n        bad\n        good\n        "       )
     with open(lowercase__       ,	"w"       ) as f:
          f.write(lowercase__       )
     return str(lowercase__       )






@pytest.fixture
def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     a_       		=tmp_path / "csv_with_int_list.csv"
     a_       		=textwrap.dedent(
         "\\n        int_list\n        1 2 3\n        4 5 6\n        7 8 9\n        "       )
     with open(lowercase__       ,	"w"       ) as f:
          f.write(lowercase__       )
     return str(lowercase__       )






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     a_       		=Csv()
     a_       		=csv._generate_tables([[csv_file, malformed_csv_file]]       )
     with pytest.raises(lowercase__       ,	match="Error tokenizing data"       ):
          for _ in generator:
               pass
     assert any(
         record.levelname == "ERROR"
         and "Failed to read file" in record.message
         and os.path.basename(lowercase__       ) in record.message
         for record in caplog.records       )






@require_pil
def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     with open(lowercase__       ,	encoding="utf-8"       ) as f:
          a_       		=f.read().splitlines()[1]
     a_       		=Csv(encoding="utf-8"       ,	features=Features({"image": Image()}       )       )
     a_       		=csv._generate_tables([[csv_file_with_image]]       )
     a_       		=pa.concat_tables([table for _, table in generator]       )
     assert pa_table.schema.field("image"       ).type == Image()()
     a_       		=pa_table.to_pydict()["image"]
     assert generated_content == [{"path": image_file, "bytes": None}]






def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     with open(lowercase__       ,	encoding="utf-8"       ) as f:
          a_       		=f.read().splitlines()[1:]
     a_       		=Csv(encoding="utf-8"       ,	features=Features({"label": ClassLabel(names=["good", "bad"]       )}       )       )
     a_       		=csv._generate_tables([[csv_file_with_label]]       )
     a_       		=pa.concat_tables([table for _, table in generator]       )
     assert pa_table.schema.field("label"       ).type == ClassLabel(names=["good", "bad"]       )()
     a_       		=pa_table.to_pydict()["label"]
     assert generated_content == [ClassLabel(names=["good", "bad"]       ).straint(lowercase__       ) for label in labels]






def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     a_       		=Csv(encoding="utf-8"       ,	sep=","       ,	converters={"int_list": lambda lowercase__       : [int(lowercase__       ) for i in x.split()]}       )
     a_       		=csv._generate_tables([[csv_file_with_int_list]]       )
     a_       		=pa.concat_tables([table for _, table in generator]       )
     assert pa.types.is_list(pa_table.schema.field("int_list"       ).type       )
     a_       		=pa_table.to_pydict()["int_list"]
     assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]







'''simple docstring'''


from __future__ import annotations






def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     a_       		=str(lowercase__       )
     return len(lowercase__       ) == 9 and set(lowercase__       ) == set("123456789"       )






def  UpperCAmelCase_       (		):


     '''simple docstring'''







     for base_num in range(9_9_9_9       ,	4_9_9_9       ,	-1       ):
          a_       		=1_0_0_0_0_2 * base_num
          if is_9_pandigital(lowercase__       ):
               return candidate

     for base_num in range(3_3_3       ,	9_9       ,	-1       ):
          a_       		=1_0_0_2_0_0_3 * base_num
          if is_9_pandigital(lowercase__       ):
               return candidate

     return None


if __name__ == "__main__":
  print(F"""{solution() = }""")


