easy +10 pts

File Open Context Manager

Implement a context manager that emulates open() in memory.

In Python, the built-in `open()` function returns a file object that can be used as a context manager. When used in a `with` statement, the file is automatically closed after the block exits. However, the file object also has useful attributes like `.name` and methods like `.read()` and `.write()`. Your task is to implement a class `FakeFile` that mimics this behavior without actually touching the filesystem. The class will be used as a context manager, but the underlying data is stored in a string. Implement the class `FakeFile` with the following requirements: - The constructor takes a string `name` (the filename) and an optional string `initial_content` (default is an empty string `""`). - It has an attribute `name` that stores the filename. - It has an attribute `closed` (boolean) initialized to `False`. - It has an attribute `content` that stores the current content (initially `initial_content`). - It has a method `read()` that returns the current `content`. - It has a method `write(text)` that appends `text` to `content` and returns the length of `text` (like the real file object). - It has a method `close()` that sets `closed` to `True` and is idempotent (calling it again does nothing). - It must support the context manager protocol: `__enter__` returns the file object itself, `__exit__` calls `close()` and always returns `False` (so exceptions propagate). You do NOT need to handle file modes or encoding. The class is purely in-memory. Write a function `process_log` that uses your `FakeFile` as a context manager. The function takes a filename and a list of log lines (strings). It opens a `FakeFile` with that filename, writes each line (each followed by a newline character `'\n'`) to the file, and after the `with` block, returns `(file.name, file.closed, file.content)`. For example: ```python result = process_log("server.log", ["INFO: started", "ERROR: failed"]) assert result == ("server.log", True, "INFO: started\nERROR: failed\n") ``` Implement both `FakeFile` and `process_log`. **Function signature:** ```python def process_log(filename: str, lines: list) -> tuple: ``` The tuple returned should be `(name, closed, content)`. Your code should work for any non-empty list of lines and any filename string.

Constraints

- `filename` is a non-empty string. - `lines` is a list of strings, each may be empty or contain any characters. - All input sizes are small (less than 1000 lines, each line less than 1000 characters). - Do not use actual file I/O; only `FakeFile` internal in-memory string operations.

Example

>>> process_log("test.log", ["a", "b"])
("test.log", True, "a\nb\n")
>>> f = FakeFile("data.txt", "hello")
>>> with f:
...     f.write(" world")
...     print(f.read())
hello world
>>> f.closed
True
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Remember that __exit__ should close the file and can ignore the exception arguments.
The write method should append to the content and return the length of the appended string.
Ensure close() sets closed to True and is safe to call multiple times.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.