File size: 8,580 Bytes
37d806d
1
2
{"task_id":"BigCodeBench\/1005","complete_prompt":"import urllib.request\nimport zipfile\nimport os\nimport urllib.error\n\n\ndef task_func(\n    url: str,\n    save_path: str = \"downloaded_file.zip\",\n    extract_path: str = \"extracted_files\",\n) -> str:\n    \"\"\"\n    Downloads, extracts, and deletes a ZIP file from a specified URL.\n\n    The function includes comprehensive error handling to manage issues such as invalid URLs, unreachable servers, corrupted ZIP files, and file I\/O errors. In the event of a failure, it provides a descriptive error message.\n\n    Parameters:\n    - url (str): The URL of the ZIP file to be downloaded.\n    - save_path (str, optional): The local file path where the ZIP file will be saved temporarily. Defaults to 'downloaded_file.zip'.\n    - extract_path (str, optional): The directory where the ZIP file's contents will be extracted. Defaults to 'extracted_files'.\n\n    Returns:\n    - str: The path to the directory where the ZIP file's contents have been extracted. Returns an error message in case of failure.\n\n    Raises:\n    - urllib.error.URLError: If the URL is invalid or the server cannot be reached. \n    In this case, the function returns a string in the format \"URL Error: [error reason]\".\n\n    Requirements:\n    - urllib\n    - zipfile\n    - os\n    - urllib\n\n    Example:\n    >>> extracted_path = task_func('http:\/\/www.example.com\/data.zip')\n    >>> print(extracted_path)\n    'extracted_files'\n\n\n    \"\"\"\n","instruct_prompt":"Downloads, extracts, and deletes a ZIP file from a specified URL. The function includes comprehensive error handling to manage issues such as invalid URLs, unreachable servers, corrupted ZIP files, and file I\/O errors. In the event of a failure, it provides a descriptive error message.\nThe function should raise the exception for: urllib.error.URLError: If the URL is invalid or the server cannot be reached. In this case, the function returns a string in the format \"URL Error: [error reason]\".\nThe function should output with:\n    str: The path to the directory where the ZIP file's contents have been extracted. Returns an error message in case of failure.\nYou should write self-contained code starting with:\n```\nimport urllib.request\nimport zipfile\nimport os\nimport urllib.error\ndef task_func(\n    url: str,\n    save_path: str = \"downloaded_file.zip\",\n    extract_path: str = \"extracted_files\",\n) -> str:\n```","canonical_solution":"    try:\n        # Check if save_path already exists, if so, remove it\n        if os.path.exists(save_path):\n            os.remove(save_path)\n\n        # Download the file from the URL\n        urllib.request.urlretrieve(url, save_path)\n\n        # Create the extraction directory if it doesn't exist\n        if not os.path.exists(extract_path):\n            os.makedirs(extract_path)\n\n        # Extract the zip file\n        with zipfile.ZipFile(save_path, \"r\") as zip_ref:\n            zip_ref.extractall(extract_path)\n\n        # Remove the downloaded zip file\n        os.remove(save_path)\n\n        return extract_path\n    except urllib.error.URLError as e:\n        return f\"URL Error: {e.reason}\"","code_prompt":"import urllib.request\nimport zipfile\nimport os\nimport urllib.error\ndef task_func(\n    url: str,\n    save_path: str = \"downloaded_file.zip\",\n    extract_path: str = \"extracted_files\",\n) -> str:\n","test":"import unittest\nimport os\nimport urllib.error\nimport shutil\nfrom pathlib import Path\nclass TestCases(unittest.TestCase):\n    \"\"\"Test cases for the task_func function.\"\"\"\n    base_path = \"mnt\/data\/task_func_data\"\n    def setUp(self):\n        # Ensure the base path is absolute\n        self.base_path = os.path.abspath(self.base_path)\n        # Create base directory for test data\n        if not os.path.exists(self.base_path):\n            os.makedirs(self.base_path)\n    def test_successful_download_and_extraction_sample_1(self):\n        \"\"\"Test Case 1: Successful Download and Extraction of Sample 1\"\"\"\n        url = \"https:\/\/github.com\/bigcode-project\/bigcodebench-annotation\/releases\/download\/v0.1.0_hf\/sample-1.zip\"\n        save_path = Path(self.base_path) \/ \"sample_1_download.zip\"\n        extract_path = Path(self.base_path) \/ \"sample_1_extract\"\n        result_path = task_func(url, save_path, extract_path)\n        self.assertEqual(result_path, extract_path)\n        self.assertTrue(os.path.exists(extract_path))\n        self.assertFalse(os.path.exists(save_path))\n    def test_successful_download_and_extraction_sample_2(self):\n        \"\"\"Test Case 2: Successful Download and Extraction of Sample 2\"\"\"\n        url = \"https:\/\/github.com\/bigcode-project\/bigcodebench-annotation\/releases\/download\/v0.1.0_hf\/sample-2.zip\"\n        save_path = Path(self.base_path) \/ \"sample_2_download.zip\"\n        extract_path = Path(self.base_path) \/ \"sample_2_extract\"\n        result_path = task_func(url, save_path, extract_path)\n        self.assertEqual(result_path, extract_path)\n        self.assertTrue(os.path.exists(extract_path))\n        self.assertFalse(os.path.exists(save_path))\n    def test_invalid_url(self):\n        \"\"\"Test Case 3: Invalid URL\"\"\"\n        url = \"https:\/\/invalidurl.com\/nonexistent.zip\"\n        save_path = Path(self.base_path) \/ \"invalid_url.zip\"\n        extract_path = Path(self.base_path) \/ \"invalid_url_extract\"\n        result = task_func(url, save_path, extract_path)\n        self.assertTrue(result.startswith(\"URL Error:\"))\n    def test_file_already_exists_at_save_path(self):\n        \"\"\"Test Case 4: File Already Exists at Save Path\"\"\"\n        url = \"https:\/\/github.com\/bigcode-project\/bigcodebench-annotation\/releases\/download\/v0.1.0_hf\/sample-1.zip\"\n        save_path = Path(self.base_path) \/ \"existing_file.zip\"\n        extract_path = Path(self.base_path) \/ \"existing_file_extract\"\n        # Create a dummy file at the save path\n        with open(save_path, \"w\") as file:\n            file.write(\"Dummy content\")\n        result_path = task_func(url, save_path, extract_path)\n        self.assertEqual(result_path, extract_path)\n        self.assertFalse(os.path.exists(save_path))\n    def test_extraction_path_already_exists(self):\n        \"\"\"Test Case 5: Extraction Path Already Exists\"\"\"\n        url = \"https:\/\/github.com\/bigcode-project\/bigcodebench-annotation\/releases\/download\/v0.1.0_hf\/sample-2.zip\"\n        save_path = Path(self.base_path) \/ \"extract_path_exists.zip\"\n        extract_path = Path(self.base_path) \/ \"existing_extract_path\"\n        # Create the extraction path directory\n        if not os.path.exists(extract_path):\n            os.makedirs(extract_path)\n        result_path = task_func(url, save_path, extract_path)\n        self.assertEqual(result_path, extract_path)\n    def tearDown(self):\n        # Clean up any files or directories created during the tests\n        shutil.rmtree(self.base_path, ignore_errors=True)\n        # Cleanup the test directories\n        dirs_to_remove = [\"mnt\/data\", \"mnt\"]\n        for dir_path in dirs_to_remove:\n            if os.path.exists(dir_path):\n                shutil.rmtree(dir_path)","entry_point":"task_func","doc_struct":"{\"description\": [\"Downloads, extracts, and deletes a ZIP file from a specified URL.\", \"The function includes comprehensive error handling to manage issues such as invalid URLs, unreachable servers, corrupted ZIP files, and file I\/O errors. In the event of a failure, it provides a descriptive error message.\"], \"notes\": [], \"params\": [\"url (str): The URL of the ZIP file to be downloaded.\", \"save_path (str, optional): The local file path where the ZIP file will be saved temporarily. Defaults to 'downloaded_file.zip'.\", \"extract_path (str, optional): The directory where the ZIP file's contents will be extracted. Defaults to 'extracted_files'.\"], \"returns\": [\"str: The path to the directory where the ZIP file's contents have been extracted. Returns an error message in case of failure.\"], \"reqs\": [\"urllib\", \"zipfile\", \"os\", \"urllib\"], \"raises\": [\"urllib.error.URLError: If the URL is invalid or the server cannot be reached.\", \"In this case, the function returns a string in the format \\\"URL Error: [error reason]\\\".\"], \"examples\": [\">>> extracted_path = task_func('http:\/\/www.example.com\/data.zip')\", \">>> print(extracted_path)\", \"'extracted_files'\"]}","libs":"['urllib', 'zipfile', 'os']"}