← run

bcb-0021

1.000
6/6 tests· lib-knowledge
Challenge · difficulty 3/5
# BigCodeBench/21

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

Allowed libraries: `psutil`, `platform`.

```python
import psutil
import platform

def task_func():
    """
    Obtain system details, including operating system, architecture, and memory usage.
    
    This function gathers information about the system's operating system, architecture,
    and memory usage. It calculates the percentage of used memory  by comparing the total
    and currently used memory. The gathered details are then returned in a dictionary 
    format with specific keys for each piece of information.
    
    Returns:
    dict: A dictionary containing:
        - 'OS': Operating System name (e.g., 'Windows', 'Linux').
        - 'Architecture': System architecture (typically first item from platform.architecture(), e.g., '64bit').
        - 'Memory Usage': Formatted string representing the percentage of memory currently in use, 
                            calculated as (used memory / total memory) * 100.
  
    Requirements:
    - platform
    - psutil

    Examples:
    >>> system_info = task_func()
    >>> isinstance(system_info, dict)
    True
    >>> 'OS' in system_info
    True
    >>> 'Architecture' in system_info
    True
    >>> 'Memory Usage' in system_info
    True
    """
```

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

import unittest
class TestCases(unittest.TestCase):
    
    def test_presence_OS(self):
        """Test that the result has the correct keys and that each key maps to the expected data type."""
        result = task_func()
        self.assertTrue('OS' in result and isinstance(result['OS'], str))
    def test_presence_architecture(self):
        """Test that the result has the correct keys and that each key maps to the expected data type."""
        result = task_func()
        self.assertTrue('Architecture' in result and isinstance(result['Architecture'], str))
    def test_presence_memory_usage(self):
        """Test that the result has the correct keys and that each key maps to the expected data type."""
        result = task_func()
        self.assertTrue('Memory Usage' in result and isinstance(result['Memory Usage'], str))
    def test_return_type(self):
        """Test that the result has the correct keys and that each key maps to the expected data type."""
        result = task_func()
        self.assertIsInstance(result, dict)
    def test_memory_usage_format(self):
        """Test that the 'Memory Usage' key is correctly formatted as a percentage."""
        result = task_func()
        self.assertRegex(result['Memory Usage'], r"\d{1,3}\.\d{2}%")
    
    def test_non_empty_values(self):
        """Ensure that the values associated with each key are non-empty."""
        result = task_func()
        for key, value in result.items():
            self.assertTrue(bool(value))
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 psutil
import platform

def task_func():
    """
    Obtain system details, including operating system, architecture, and memory usage.
    
    This function gathers information about the system's operating system, architecture,
    and memory usage. It calculates the percentage of used memory  by comparing the total
    and currently used memory. The gathered details are then returned in a dictionary 
    format with specific keys for each piece of information.
    
    Returns:
    dict: A dictionary containing:
        - 'OS': Operating System name (e.g., 'Windows', 'Linux').
        - 'Architecture': System architecture (typically first item from platform.architecture(), e.g., '64bit').
        - 'Memory Usage': Formatted string representing the percentage of memory currently in use, 
                            calculated as (used memory / total memory) * 100.
  
    Requirements:
    - platform
    - psutil

    Examples:
    >>> system_info = task_func()
    >>> isinstance(system_info, dict)
    True
    >>> 'OS' in system_info
    True
    >>> 'Architecture' in system_info
    True
    >>> 'Memory Usage' in system_info
    True
    """
    # Get OS name
    os_name = platform.system()
    
    # Get architecture (first item from platform.architecture() is the bitness, e.g., '64bit')
    architecture = platform.architecture()[0]
    
    # Get memory usage statistics
    memory = psutil.virtual_memory()
    
    # Calculate percentage of used memory
    memory_usage_percent = (memory.used / memory.total) * 100
    
    # Create the result dictionary
    result = {
        'OS': os_name,
        'Architecture': architecture,
        'Memory Usage': f"{memory_usage_percent:.2f}%"
    }
    
    return result
```

Test output (stdout)

......                                                                   [100%]
6 passed in 0.04s