← run

bcb-0018

1.000
5/5 tests· lib-knowledge
Challenge · difficulty 5/5
# BigCodeBench/18

Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; define `task_func` at module level.

Allowed libraries: `glob`, `subprocess`, `random`, `os`, `csv`.

```python
import subprocess
import csv
import glob
import random
import os

def task_func(file):
    """
    Divide a CSV file into several smaller files and shuffle the lines in each file.
    
    This function takes a CSV file path as input, divides it into smaller files using 
    the shell 'split' command, and shuffles the rows in each of the resulting files.
    The output files are named with a 'split_' prefix.

    Parameters:
    - file (str): The path to the CSV file.

    Returns:
    - list: The paths to the split files. Returns an empty list if the file does not exist, is not a CSV file, or if an error occurs during processing.
    
    Requirements:
    - subprocess
    - csv
    - glob
    - random
    - os

    Example:
    >>> task_func('/path/to/file.csv')
    ['/path/to/split_00', '/path/to/split_01', ...]
    """
```

<!-- imported from BigCodeBench (BigCodeBench/18) -->
tests/test_bcb_0018.py
# Auto-generated from BigCodeBench BigCodeBench/18. Do not edit by hand.
import pathlib as _pathlib
exec(_pathlib.Path(__file__).with_name("solution.py").read_text(), globals())

import unittest
import csv
import os
import tempfile
class TestCases(unittest.TestCase):
    def setUp(self):
        # Create a temporary directory to hold the files
        self.test_dir = tempfile.mkdtemp()
        self.small_csv_path = os.path.join(self.test_dir, "small.csv")
        self.medium_csv_path = os.path.join(self.test_dir, "medium.csv")
        self.large_csv_path = os.path.join(self.test_dir, "large.csv")
        self.non_csv_path = os.path.join(self.test_dir, "test.txt")
        
        # Create dummy CSV files of different sizes
        with open(self.small_csv_path, "w", newline="") as file:
            writer = csv.writer(file)
            for i in range(10):  # Small CSV
                writer.writerow([f"row{i}", f"value{i}"])
        
        with open(self.medium_csv_path, "w", newline="") as file:
            writer = csv.writer(file)
            for i in range(100):  # Medium CSV
                writer.writerow([f"row{i}", f"value{i}"])
        
        with open(self.large_csv_path, "w", newline="") as file:
            writer = csv.writer(file)
            for i in range(1000):  # Large CSV
                writer.writerow([f"row{i}", f"value{i}"])
        
        # Create a non-CSV file
        with open(self.non_csv_path, "w") as file:
            file.write("This is a test text file.")
    def tearDown(self):
        # Remove all files created in the directory
        for filename in os.listdir(self.test_dir):
            file_path = os.path.join(self.test_dir, filename)
            os.remove(file_path)  # Remove each file
    def test_small_csv(self):
        """Test splitting and shuffling a small CSV file."""
        split_files = task_func(self.small_csv_path)
        self.assertTrue(len(split_files) > 0, "No files were split.")
        self.assertNotEqual(self._read_csv(self.small_csv_path), self._read_csv(split_files[0]), "Rows are not shuffled.")
        for filename in split_files:
            os.remove(filename)
    def test_medium_csv(self):
        """Test splitting and shuffling a medium CSV file."""
        split_files = task_func(self.medium_csv_path)
        self.assertTrue(len(split_files) > 0, "No files were split.")
        self.assertNotEqual(self._read_csv(self.medium_csv_path), self._read_csv(split_files[0]), "Rows are not shuffled.")
        for filename in split_files:
            os.remove(filename)
    def test_large_csv(self):
        """Test splitting and shuffling a large CSV file."""
        split_files = task_func(self.large_csv_path)
        self.assertTrue(len(split_files) > 0, "No files were split.")
        self.assertNotEqual(self._read_csv(self.large_csv_path), self._read_csv(split_files[0]), "Rows are not shuffled.")
        for filename in split_files:
            os.remove(filename)
    def test_invalid_file(self):
        """Test behavior with a non-existent file path."""
        split_files = task_func("/path/that/does/not/exist.csv")
        self.assertEqual(split_files, [], "Expected an empty list for an invalid file path.")
    def test_non_csv_file(self):
        """Test behavior with a non-CSV file."""
        split_files = task_func(self.non_csv_path)
        self.assertEqual(split_files, [], "Expected an empty list for a non-CSV file.")
    def _read_csv(self, filepath):
        """Helper method to read CSV file and return content."""
        with open(filepath, "r") as f:
            reader = csv.reader(f)
            return list(reader)
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
```python
import subprocess
import csv
import glob
import random
import os

def task_func(file):
    """
    Divide a CSV file into several smaller files and shuffle the lines in each file.

    This function takes a CSV file path as input, divides it into smaller files using
    the shell 'split' command, and shuffles the rows in each of the resulting files.
    The output files are named with a 'split_' prefix.

    Parameters:
    - file (str): The path to the CSV file.

    Returns:
    - list: The paths to the split files. Returns an empty list if the file does not exist, is not a CSV file, or if an error occurs during processing.

    Requirements:
    - subprocess
    - csv
    - glob
    - random
    - os

    Example:
    >>> task_func('/path/to/file.csv')
    ['/path/to/split_00', '/path/to/split_01', ...]
    """
    if not os.path.exists(file):
        return []

    if not file.lower().endswith('.csv'):
        return []

    try:
        # Read the CSV file
        with open(file, 'r', newline='') as csvfile:
            reader = csv.reader(csvfile)
            rows = list(reader)

        if not rows:
            return []

        # Shuffle all rows
        random.shuffle(rows)

        # Split into smaller files
        split_files = []
        num_splits = 3  # Default number of splits
        rows_per_split = len(rows) // num_splits

        for i in range(num_splits):
            start = i * rows_per_split
            end = (i + 1) * rows_per_split if i != num_splits - 1 else len(rows)
            split_rows = rows[start:end]

            # Write to split file
            split_file = f"{os.path.splitext(file)[0]}_split_{i:02d}.csv"
            with open(split_file, 'w', newline='') as splitfile:
                writer = csv.writer(splitfile)
                writer.writerows(split_rows)

            split_files.append(split_file)

        return split_files
    except Exception:
        return []
```

Test output (stdout)

.....                                                                    [100%]
5 passed in 0.02s