




import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


_lowerCamelCase	:    List[Any]          =      '''\
    Text data.
    Second line of data.'''

_lowerCamelCase	:    Union[str, Any]          =      '''file'''






@pytest.fixture(scope='session'			)
def 	a_		(	__lowercase       :     Optional[int]			)			->  List[str]:
			_snake_case          =      tmp_path_factory.mktemp('data'			) / (FILE_PATH + '.zstd')
			_snake_case          =      bytes(__lowercase	, 'utf-8'			)
			with zstd.open(__lowercase	, 'wb'			) as f:
						f.write(__lowercase			)
			return path






@pytest.fixture
def 	a_		(	__lowercase       :     Union[str, Any]			)			->  Any:
			with open(os.path.join(tmpfs.local_root_dir	, __lowercase			)	, 'w'			) as f:
						f.write(__lowercase			)
			return FILE_PATH






@pytest.mark.parametrize('compression_format'	, ['gzip', 'xz', 'zstd']			)
def 	a_		(	__lowercase       :     int	, __lowercase       :     List[str]	, __lowercase       :     Union[str, Any]	, __lowercase       :     Dict	, __lowercase       :     List[str]	, __lowercase       :     List[str]			)			->  Optional[Any]:
			_snake_case          =      {'gzip': gz_file, 'xz': xz_file, 'zstd': zstd_path}
			_snake_case          =      input_paths[compression_format]
			_snake_case          =      tmp_path / 'cache'
			_snake_case          =      DownloadConfig(cache_dir=__lowercase	, extract_compressed_file=__lowercase			)
			_snake_case          =      cached_path(__lowercase	, download_config=__lowercase			)
			with open(__lowercase			) as f:
						_snake_case          =      f.read()
			with open(__lowercase			) as f:
						_snake_case          =      f.read()
			assert extracted_file_content == expected_file_content






@pytest.mark.parametrize('default_extracted'	, [True, False]			)
@pytest.mark.parametrize('default_cache_dir'	, [True, False]			)
def 	a_		(	__lowercase       :     List[Any]	, __lowercase       :     Dict	, __lowercase       :     int	, __lowercase       :     Union[str, Any]	, __lowercase       :     Optional[int]			)			->  int:
			_snake_case          =      'custom_cache'
			_snake_case          =      'custom_extracted_dir'
			_snake_case          =      tmp_path / 'custom_extracted_path'
			if default_extracted:
						_snake_case          =      ('downloads' if default_cache_dir else custom_cache_dir, 'extracted')
			else:
						monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_DIR'	, __lowercase			)
						monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH'	, str(__lowercase			)			)
						_snake_case          =      custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

			_snake_case          =      xz_file
			_snake_case          =      (
			    DownloadConfig(extract_compressed_file=__lowercase			)
			    if default_cache_dir
			    else DownloadConfig(cache_dir=tmp_path / custom_cache_dir	, extract_compressed_file=__lowercase			)
			)
			_snake_case          =      cached_path(__lowercase	, download_config=__lowercase			)
			assert Path(__lowercase			).parent.parts[-2:] == expected






def 	a_		(	__lowercase       :     Union[str, Any]			)			->  Union[str, Any]:
			# absolute path
			_snake_case          =      str(Path(__lowercase			).resolve()			)
			assert cached_path(__lowercase			) == text_file
			# relative path
			_snake_case          =      str(Path(__lowercase			).resolve().relative_to(Path(os.getcwd()			)			)			)
			assert cached_path(__lowercase			) == text_file






def 	a_		(	__lowercase       :     int			)			->  Tuple:
			# absolute path
			_snake_case          =      str(tmp_path.resolve() / '__missing_file__.txt'			)
			with pytest.raises(__lowercase			):
						cached_path(__lowercase			)
			# relative path
			_snake_case          =      './__missing_file__.txt'
			with pytest.raises(__lowercase			):
						cached_path(__lowercase			)






def 	a_		(	__lowercase       :     str			)			->  Any:
			_snake_case          =      get_from_cache(f'''tmp://{tmpfs_file}'''			)
			with open(__lowercase			) as f:
						_snake_case          =      f.read()
			assert output_file_content == FILE_CONTENT






@patch('datasets.config.HF_DATASETS_OFFLINE'	, __lowercase			)
def 	a_		(	)			->  Any:
			with pytest.raises(__lowercase			):
						cached_path('https://huggingface.co'			)






@patch('datasets.config.HF_DATASETS_OFFLINE'	, __lowercase			)
def 	a_		(	__lowercase       :     Any			)			->  List[str]:
			_snake_case          =      tmp_path_factory.mktemp('data'			) / 'file.html'
			with pytest.raises(__lowercase			):
						http_get('https://huggingface.co'	, temp_file=__lowercase			)
			with pytest.raises(__lowercase			):
						http_head('https://huggingface.co'			)






@patch('datasets.config.HF_DATASETS_OFFLINE'	, __lowercase			)
def 	a_		(	__lowercase       :     int			)			->  int:
			_snake_case          =      tmp_path_factory.mktemp('data'			) / 'file.html'
			with pytest.raises(__lowercase			):
						ftp_get('ftp://huggingface.co'	, temp_file=__lowercase			)
			with pytest.raises(__lowercase			):
						ftp_head('ftp://huggingface.co'			)






@patch('datasets.config.HF_DATASETS_OFFLINE'	, __lowercase			)
def 	a_		(	__lowercase       :     Dict			)			->  Tuple:
			_snake_case          =      tmp_path_factory.mktemp('data'			) / 'file.html'
			with pytest.raises(__lowercase			):
						fsspec_get('s3://huggingface.co'	, temp_file=__lowercase			)
			with pytest.raises(__lowercase			):
						fsspec_head('s3://huggingface.co'			)

def 	a_		(	__lowercase       :     int = 50_000_000			)			->  int:
			_snake_case          =      set()
			_snake_case          =      int((limit - 24) ** (1 / 2)			)

			_snake_case          =      set(range(3	, prime_square_limit + 1	, 2			)			)
			primes.add(2			)
			for p in range(3	, prime_square_limit + 1	, 2			):
						if p not in primes:
									continue
						primes.difference_update(set(range(p * p	, prime_square_limit + 1	, __lowercase			)			)			)

			for primea in primes:
						_snake_case          =      primea * primea
						for primea in primes:
									_snake_case          =      primea * primea * primea
									if square + cube >= limit - 16:
												break
									for primea in primes:
												_snake_case          =      primea * primea * primea * primea
												_snake_case          =      square + cube + tetr
												if total >= limit:
															break
												ret.add(__lowercase			)

			return len(__lowercase			)


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