

"""simple docstring"""





import itertools
from dataclasses import dataclass
from io import StringIO
from typing import Optional

import pyarrow as pa

import datasets
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


_a				   =							datasets.utils.logging.get_logger(__name__)




@dataclass
class       _lowerCAmelCase						(			datasets.BuilderConfig     ):







 """simple docstring"""







 __UpperCAmelCase			:      Optional[datasets.Features]     	=			None
 __UpperCAmelCase			:      str     	=			"utf-8"
 __UpperCAmelCase			:      Optional[str]     	=			None
 __UpperCAmelCase			:      int     	=			1_0 << 2_0  # 10MB
 __UpperCAmelCase			:      bool     	=			False
 __UpperCAmelCase			:      str     	=			"line"







class       _lowerCAmelCase						(			datasets.ArrowBasedBuilder     ):







 """simple docstring"""







 __UpperCAmelCase			:      Tuple     	=			TextConfig


 def       _lowercase				(    self							:    str			):
    return datasets.DatasetInfo(features=self.config.features			)


 def       _lowercase				(    self							:    List[Any],      UpperCAmelCase__							:    Union[str, Any]			):
    if not self.config.data_files:
       raise ValueError(F"""At least one data file must be specified, but got data_files={self.config.data_files}"""			)
    __lowercase			=		dl_manager.download_and_extract(self.config.data_files			)
    if isinstance(UpperCAmelCase_,      (str, list, tuple)			):
       __lowercase			=		data_files
       if isinstance(UpperCAmelCase_,      UpperCAmelCase_			):
          __lowercase			=		[files]
       __lowercase			=		[dl_manager.iter_files(UpperCAmelCase_			) for file in files]
       return [datasets.SplitGenerator(name=datasets.Split.TRAIN,      gen_kwargs={"files": files}			)]
    __lowercase			=		[]
    for split_name, files in data_files.items():
       if isinstance(UpperCAmelCase_,      UpperCAmelCase_			):
          __lowercase			=		[files]
       __lowercase			=		[dl_manager.iter_files(UpperCAmelCase_			) for file in files]
       splits.append(datasets.SplitGenerator(name=UpperCAmelCase_,      gen_kwargs={"files": files}			)			)
    return splits


 def       _lowercase				(    self							:    Dict,      UpperCAmelCase__							:    pa.Table			):
    if self.config.features is not None:
       __lowercase			=		self.config.features.arrow_schema
       if all(not require_storage_cast(UpperCAmelCase_			) for feature in self.config.features.values()			):
          # cheaper cast
          __lowercase			=		pa_table.cast(UpperCAmelCase_			)
       else:
          # more expensive cast; allows str <-> int/float or str to Audio for example
          __lowercase			=		table_cast(UpperCAmelCase_,      UpperCAmelCase_			)
       return pa_table
    else:
       return pa_table.cast(pa.schema({"text": pa.string()}			)			)



 def       _lowercase				(    self							:    int,      UpperCAmelCase__							:    Any			):
    __lowercase			=		list(self.config.features			) if self.config.features is not None else ['text']
    for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCAmelCase_			)			):
       # open in text mode, by default translates universal newlines ("\n", "\r\n" and "\r") into "\n"
       with open(UpperCAmelCase_,      encoding=self.config.encoding,      errors=self.config.errors			) as f:
          if self.config.sample_by == "line":
             __lowercase			=		0
             while True:
                __lowercase			=		f.read(self.config.chunksize			)
                if not batch:
                   break
                batch += f.readline()  # finish current line
                # StringIO.readlines, by default splits only on "\n" (and keeps line breaks)
                __lowercase			=		StringIO(UpperCAmelCase_			).readlines()
                if not self.config.keep_linebreaks:
                   __lowercase			=		[line.rstrip("\n"			) for line in batch]
                __lowercase			=		pa.Table.from_arrays([pa.array(UpperCAmelCase_			)],      names=UpperCAmelCase_			)
                # Uncomment for debugging (will print the Arrow table size and elements)
                # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
                # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
                yield (file_idx, batch_idx), self._cast_table(UpperCAmelCase_			)
                batch_idx += 1
          elif self.config.sample_by == "paragraph":
             __lowercase			=		0
             __lowercase			=		''
             while True:
                __lowercase			=		f.read(self.config.chunksize			)
                if not new_batch:
                   break
                batch += new_batch
                batch += f.readline()  # finish current line
                __lowercase			=		batch.split("\n\n"			)
                __lowercase			=		pa.Table.from_arrays(
                    [pa.array([example for example in batch[:-1] if example]			)],      names=UpperCAmelCase_			)
                # Uncomment for debugging (will print the Arrow table size and elements)
                # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
                # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
                yield (file_idx, batch_idx), self._cast_table(UpperCAmelCase_			)
                batch_idx += 1
                __lowercase			=		batch[-1]
             if batch:
                __lowercase			=		pa.Table.from_arrays([pa.array([batch]			)],      names=UpperCAmelCase_			)
                yield (file_idx, batch_idx), self._cast_table(UpperCAmelCase_			)
          elif self.config.sample_by == "document":
             __lowercase			=		f.read()
             __lowercase			=		pa.Table.from_arrays([pa.array([text]			)],      names=UpperCAmelCase_			)
             yield file_idx, self._cast_table(UpperCAmelCase_			)




"""simple docstring"""





import shutil

import requests




def 						_A						(						UpperCamelCase_  : str,    UpperCamelCase_  : bool = False,    UpperCamelCase_  : str = ".")			->		dict:
   '''simple docstring'''


   __lowercase			=		"https://api.nasa.gov/planetary/apod"
   return requests.get(UpperCamelCase_,    params={"api_key": api_key}).json()




def 						_A						(						UpperCamelCase_  : str,    UpperCamelCase_  : str = ".")			->		dict:
   '''simple docstring'''


   __lowercase			=		get_apod_data(UpperCamelCase_)
   __lowercase			=		apod_data["url"]
   __lowercase			=		img_url.split("/")[-1]
   __lowercase			=		requests.get(UpperCamelCase_,    stream=UpperCamelCase_)

   with open(F"""{path}/{img_name}""",    "wb+") as img_file:
      shutil.copyfileobj(response.raw,    UpperCamelCase_)
   del response
   return apod_data




def 						_A						(						UpperCamelCase_  : str)			->		dict:
   '''simple docstring'''


   __lowercase			=		"https://images-api.nasa.gov/search"
   return requests.get(UpperCamelCase_,    params={"q": query}).json()


if __name__ == "__main__":
   print(save_apod('YOUR API KEY'))
   _a				   =							get_archive_data('apollo 2011')['collection']['items']
   print(apollo_aaaa_items[0]['data'][0]['description'])




