


"""simple docstring"""
import multiprocessing
import os
from typing import BinaryIO, Optional, Union

import fsspec

from .. import Dataset, Features, NamedSplit, config
from ..formatting import query_table
from ..packaged_modules.json.json import Json
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader



class   lowerCAmelCase_    (			lowerCAmelCase					):



				"""simple docstring"""

				def __init__(				self		,    lowerCAmelCase		,    lowerCAmelCase = None		,    lowerCAmelCase = None		,    lowerCAmelCase = None		,    lowerCAmelCase = False		,    lowerCAmelCase = False		,    lowerCAmelCase = None		,    lowerCAmelCase = None		,    **lowerCAmelCase		,    ):






										"""simple docstring"""

										super().__init__(
										    lowerCAmelCase		,    split=lowerCAmelCase		,    features=lowerCAmelCase		,    cache_dir=lowerCAmelCase		,    keep_in_memory=lowerCAmelCase		,    streaming=lowerCAmelCase		,    num_proc=lowerCAmelCase		,    **lowerCAmelCase		,    )
										snake_case								=			field
										snake_case								=			path_or_paths if isinstance(lowerCAmelCase		,    lowerCAmelCase    ) else {self.split: path_or_paths}
										snake_case								=			Json(
										    cache_dir=lowerCAmelCase		,    data_files=lowerCAmelCase		,    features=lowerCAmelCase		,    field=lowerCAmelCase		,    **lowerCAmelCase		,    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										if self.streaming:
																snake_case								=			self.builder.as_streaming_dataset(split=self.split    )
										# Build regular (map-style) dataset
										else:
																snake_case								=			None
																snake_case								=			None
																snake_case								=			None
																snake_case								=			None

																self.builder.download_and_prepare(
																    download_config=lowerCAmelCase		,    download_mode=lowerCAmelCase		,    verification_mode=lowerCAmelCase		,    base_path=lowerCAmelCase		,    num_proc=self.num_proc		,    )
																snake_case								=			self.builder.as_dataset(
																    split=self.split		,    verification_mode=lowerCAmelCase		,    in_memory=self.keep_in_memory    )
										return dataset





class   lowerCAmelCase_    :



				"""simple docstring"""

				def __init__(				self		,    lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase = None		,    lowerCAmelCase = None		,    **lowerCAmelCase		,    ):






										"""simple docstring"""

										if num_proc is not None and num_proc <= 0:
																raise ValueError(F"""num_proc {num_proc} must be an integer > 0."""    )

										snake_case								=			dataset
										snake_case								=			path_or_buf
										snake_case								=			batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
										snake_case								=			num_proc
										snake_case								=			'utf-8'
										snake_case								=			to_json_kwargs

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			self.to_json_kwargs.pop('path_or_buf'		,    lowerCAmelCase    )
										snake_case								=			self.to_json_kwargs.pop('orient'		,    'records'    )
										snake_case								=			self.to_json_kwargs.pop('lines'		,    True if orient == 'records' else False    )
										snake_case								=			self.to_json_kwargs.pop('index'		,    False if orient in ['split', 'table'] else True    )
										snake_case								=			self.to_json_kwargs.pop('compression'		,    lowerCAmelCase    )

										if compression not in [None, "infer", "gzip", "bz2", "xz"]:
																raise NotImplementedError(F"""`datasets` currently does not support {compression} compression"""    )

										if isinstance(self.path_or_buf		,    (str, bytes, os.PathLike)    ):
																with fsspec.open(self.path_or_buf		,    'wb'		,    compression=lowerCAmelCase    ) as buffer:
																						snake_case								=			self._write(file_obj=lowerCAmelCase		,    orient=lowerCAmelCase		,    lines=lowerCAmelCase		,    index=lowerCAmelCase		,    **self.to_json_kwargs    )
										else:
																if compression:
																						raise NotImplementedError(
																						    F"""The compression parameter is not supported when writing to a buffer, but compression={compression}"""
																						    ' was passed. Please provide a local path instead.'    )
																snake_case								=			self._write(
																    file_obj=self.path_or_buf		,    orient=lowerCAmelCase		,    lines=lowerCAmelCase		,    index=lowerCAmelCase		,    **self.to_json_kwargs    )
										return written

				def 		snake_case (				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										snake_case					,snake_case					,snake_case					,snake_case					,snake_case								=			args

										snake_case								=			query_table(
										    table=self.dataset.data		,    key=slice(lowerCAmelCase		,    offset + self.batch_size    )		,    indices=self.dataset._indices		,    )
										snake_case								=			batch.to_pandas().to_json(
										    path_or_buf=lowerCAmelCase		,    orient=lowerCAmelCase		,    lines=lowerCAmelCase		,    index=lowerCAmelCase		,    **lowerCAmelCase    )
										if not json_str.endswith('\n'    ):
																json_str += "\n"
										return json_str.encode(self.encoding    )

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase		,    **lowerCAmelCase		,    ):






										"""simple docstring"""

										snake_case								=			0

										if self.num_proc is None or self.num_proc == 1:
																for offset in logging.tqdm(
																    range(0		,    len(self.dataset    )		,    self.batch_size    )		,    unit='ba'		,    disable=not logging.is_progress_bar_enabled()		,    desc='Creating json from Arrow format'		,    ):
																						snake_case								=			self._batch_json((offset, orient, lines, index, to_json_kwargs)    )
																						written += file_obj.write(lowerCAmelCase    )
										else:
																snake_case					,snake_case								=			len(self.dataset    ), self.batch_size
																with multiprocessing.Pool(self.num_proc    ) as pool:
																						for json_str in logging.tqdm(
																						    pool.imap(
																						        self._batch_json		,    [(offset, orient, lines, index, to_json_kwargs) for offset in range(0		,    lowerCAmelCase		,    lowerCAmelCase    )]		,    )		,    total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size		,    unit='ba'		,    disable=not logging.is_progress_bar_enabled()		,    desc='Creating json from Arrow format'		,    ):
																												written += file_obj.write(lowerCAmelCase    )

										return written



"""simple docstring"""
import os
import string
import sys


SCREAMING_SNAKE_CASE__      						=  1 << 8

SCREAMING_SNAKE_CASE__      						=  {
    "tab": ord("\t"),
    "newline": ord("\r"),
    "esc": 27,
    "up": 65 + ARROW_KEY_FLAG,
    "down": 66 + ARROW_KEY_FLAG,
    "right": 67 + ARROW_KEY_FLAG,
    "left": 68 + ARROW_KEY_FLAG,
    "mod_int": 91,
    "undefined": sys.maxsize,
    "interrupt": 3,
    "insert": 50,
    "delete": 51,
    "pg_up": 53,
    "pg_down": 54,
}

SCREAMING_SNAKE_CASE__      						=  KEYMAP["up"]
SCREAMING_SNAKE_CASE__      						=  KEYMAP["left"]

if sys.platform == "win32":
					SCREAMING_SNAKE_CASE__      						=  []
					SCREAMING_SNAKE_CASE__      						=  {
					    b"\xe0H": KEYMAP["up"] - ARROW_KEY_FLAG,
					    b"\x00H": KEYMAP["up"] - ARROW_KEY_FLAG,
					    b"\xe0P": KEYMAP["down"] - ARROW_KEY_FLAG,
					    b"\x00P": KEYMAP["down"] - ARROW_KEY_FLAG,
					    b"\xe0M": KEYMAP["right"] - ARROW_KEY_FLAG,
					    b"\x00M": KEYMAP["right"] - ARROW_KEY_FLAG,
					    b"\xe0K": KEYMAP["left"] - ARROW_KEY_FLAG,
					    b"\x00K": KEYMAP["left"] - ARROW_KEY_FLAG,
					}

for i in range(10):
					SCREAMING_SNAKE_CASE__      						=  ord(str(i))





def    lowerCAmelCase__    (			)					->				List[Any]:





						"""simple docstring"""






						if os.name == "nt":
												import msvcrt

												snake_case								=			'mbcs'
												# Flush the keyboard buffer
												while msvcrt.kbhit():
																		msvcrt.getch()
												if len(_UpperCamelCase					) == 0:
																		# Read the keystroke
																		snake_case								=			msvcrt.getch()

																		# If it is a prefix char, get second part
																		if ch in (b"\x00", b"\xe0"):
																								snake_case								=			ch + msvcrt.getch()
																								# Translate actual Win chars to bullet char types
																								try:
																														snake_case								=			chr(WIN_KEYMAP[cha]					)
																														WIN_CH_BUFFER.append(chr(KEYMAP['mod_int']					)					)
																														WIN_CH_BUFFER.append(_UpperCamelCase					)
																														if ord(_UpperCamelCase					) in (
																														    KEYMAP["insert"] - 1 << 9,
																														    KEYMAP["delete"] - 1 << 9,
																														    KEYMAP["pg_up"] - 1 << 9,
																														    KEYMAP["pg_down"] - 1 << 9,
																														):
																																				WIN_CH_BUFFER.append(chr(1_2_6					)					)
																														snake_case								=			chr(KEYMAP['esc']					)
																								except KeyError:
																														snake_case								=			cha[1]
																		else:
																								snake_case								=			ch.decode(_UpperCamelCase					)
												else:
																		snake_case								=			WIN_CH_BUFFER.pop(0					)
						elif os.name == "posix":
												import termios
												import tty

												snake_case								=			sys.stdin.fileno()
												snake_case								=			termios.tcgetattr(_UpperCamelCase					)
												try:
																		tty.setraw(_UpperCamelCase					)
																		snake_case								=			sys.stdin.read(1					)
												finally:
																		termios.tcsetattr(_UpperCamelCase ,			termios.TCSADRAIN ,			_UpperCamelCase					)
						return ch





def    lowerCAmelCase__    (			)					->				Union[str, Any]:





						"""simple docstring"""






						snake_case								=			get_raw_chars()
						if ord(_UpperCamelCase					) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
												return char

						elif ord(_UpperCamelCase					) == KEYMAP["esc"]:
												snake_case								=			get_raw_chars()
												if ord(_UpperCamelCase					) == KEYMAP["mod_int"]:
																		snake_case								=			get_raw_chars()
																		if ord(_UpperCamelCase					) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(_UpperCamelCase					) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
																								return chr(ord(_UpperCamelCase					) + ARROW_KEY_FLAG					)
																		else:
																								return KEYMAP["undefined"]
												else:
																		return get_raw_chars()

						else:
												if char in string.printable:
																		return char
												else:
																		return KEYMAP["undefined"]


