





'''simple docstring'''







import itertools
from dataclasses import dataclass
from typing import Optional

import pyarrow as pa

import datasets
from datasets.table import table_cast


_a					: Optional[Any]      =    datasets.utils.logging.get_logger(__name__)





@dataclass
class     _UpperCAmelCase							(						datasets.BuilderConfig   ):
		a   :   Optional[datasets.Features]     =None





class     _UpperCAmelCase							(						datasets.ArrowBasedBuilder   ):
		a   :   int     =ArrowConfig
		def   lowerCamelCase__			(  self			):



					'''simple docstring'''

					return datasets.DatasetInfo(features=self.config.features			)
		def   lowerCamelCase__			(  self,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					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}'			)
					__lowerCAmelCase											=				dl_manager.download_and_extract(self.config.data_files			)
					if isinstance(__SCREAMING_SNAKE_CASE,(str, list, tuple)			):
								__lowerCAmelCase											=				data_files
								if isinstance(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):
											__lowerCAmelCase											=				[files]
								# Use `dl_manager.iter_files` to skip hidden files in an extracted archive
								__lowerCAmelCase											=				[dl_manager.iter_files(__SCREAMING_SNAKE_CASE			) for file in files]
								return [datasets.SplitGenerator(name=datasets.Split.TRAIN,gen_kwargs={"""files""": files}			)]
					__lowerCAmelCase											=				[]
					for split_name, files in data_files.items():
								if isinstance(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):
											__lowerCAmelCase											=				[files]
								# Use `dl_manager.iter_files` to skip hidden files in an extracted archive
								__lowerCAmelCase											=				[dl_manager.iter_files(__SCREAMING_SNAKE_CASE			) for file in files]
								# Infer features is they are stoed in the arrow schema
								if self.info.features is None:
											for file in itertools.chain.from_iterable(__SCREAMING_SNAKE_CASE			):
														with open(__SCREAMING_SNAKE_CASE,"""rb"""			) as f:
																	__lowerCAmelCase											=				datasets.Features.from_arrow_schema(pa.ipc.open_stream(__SCREAMING_SNAKE_CASE			).schema			)
														break
								splits.append(datasets.SplitGenerator(name=__SCREAMING_SNAKE_CASE,gen_kwargs={"""files""": files}			)			)
					return splits
		def   lowerCamelCase__			(  self,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					if self.info.features is not None:
								# more expensive cast to support nested features with keys in a different order
								# allows str <-> int/float or str to Audio for example
								__lowerCAmelCase											=				table_cast(__SCREAMING_SNAKE_CASE,self.info.features.arrow_schema			)
					return pa_table







		def   lowerCamelCase__			(  self,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					for file_idx, file in enumerate(itertools.chain.from_iterable(__SCREAMING_SNAKE_CASE			)			):
								with open(__SCREAMING_SNAKE_CASE,"""rb"""			) as f:
											try:
														for batch_idx, record_batch in enumerate(pa.ipc.open_stream(__SCREAMING_SNAKE_CASE			)			):
																	__lowerCAmelCase											=				pa.Table.from_batches([record_batch]			)
																	# 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 f'{file_idx}_{batch_idx}', self._cast_table(__SCREAMING_SNAKE_CASE			)
											except ValueError as e:
														logger.error(f'Failed to read file \'{file}\' with error {type(__SCREAMING_SNAKE_CASE			)}: {e}'			)
														raise








'''simple docstring'''







from collections import namedtuple

_a					: Any      =    namedtuple("""from_to""", """from_ to""")

_a					: Dict      =    {
    """millimeter""": """mm""",
    """centimeter""": """cm""",
    """meter""": """m""",
    """kilometer""": """km""",
    """inch""": """in""",
    """inche""": """in""",  # Trailing 's' has been stripped off
    """feet""": """ft""",
    """foot""": """ft""",
    """yard""": """yd""",
    """mile""": """mi""",
}

_a					: Optional[Any]      =    {
    """mm""": from_to(0.001, 1_0_0_0),
    """cm""": from_to(0.01, 1_0_0),
    """m""": from_to(1, 1),
    """km""": from_to(1_0_0_0, 0.001),
    """in""": from_to(0.0254, 39.3701),
    """ft""": from_to(0.3048, 3.28084),
    """yd""": from_to(0.9144, 1.09361),
    """mi""": from_to(1609.34, 0.000621371),
}





def    _lowerCAmelCase						(						lowercase					,	lowercase					,	lowercase	)					->					float:
			__lowerCAmelCase											=				from_type.lower().rstrip("""s"""	)
			__lowerCAmelCase											=				TYPE_CONVERSION.get(lowercase					,	lowercase	)
			__lowerCAmelCase											=				to_type.lower().rstrip("""s"""	)
			__lowerCAmelCase											=				TYPE_CONVERSION.get(lowercase					,	lowercase	)
			if new_from not in METRIC_CONVERSION:
						__lowerCAmelCase											=				(
						    f'Invalid \'from_type\' value: {from_type!r}.\n'
						    f'Conversion abbreviations are: {", ".join(lowercase	)}'
						)
						raise ValueError(lowercase	)
			if new_to not in METRIC_CONVERSION:
						__lowerCAmelCase											=				(
						    f'Invalid \'to_type\' value: {to_type!r}.\n'
						    f'Conversion abbreviations are: {", ".join(lowercase	)}'
						)
						raise ValueError(lowercase	)
			return value * METRIC_CONVERSION[new_from].from_ * METRIC_CONVERSION[new_to].to


if __name__ == "__main__":
	import doctest

	doctest.testmod()

