easy +10 pts

Async Context Manager Lifecycle

Implement an async context manager that tracks acquisition and ensures cleanup.

Implement a function `AsyncResourceLifecycle(name)` that simulates an async context manager lifecycle. The function should internally define a class `AsyncResource` that behaves as an asynchronous context manager. The class should have an `__init__` method that takes a `name` parameter and stores it as an instance attribute. When entering the context (via `async with`), the class should set an attribute `entered` to `True` and return `self`. When exiting the context, it should set `entered` to `False`. The exit method should be `async` and accept the standard exception parameters. The class should also have a method `status()` that returns a string in the format `'{name}: entered={entered}'`, using the `name` attribute and the current `entered` value. The function `AsyncResourceLifecycle` should run an async scenario that creates a resource with the given `name`, enters the context, calls `status()` inside, and returns that `status()` string (which should show `entered=True`). The async scenario should be run using `asyncio.run`. For example, `AsyncResourceLifecycle('db')` should return `'db: entered=True'`. Ensure `__aenter__` is async and returns the resource instance (`self`).

Constraints

- The `name` parameter is a non-empty string. - The function must return a string. - Only standard library is allowed.

Example

```python
>>> AsyncResourceLifecycle('db')
'db: entered=True'
>>> AsyncResourceLifecycle('file')
'file: entered=True'
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

In `__aenter__`, set `self.entered = True` and then `return self`.
In `__aexit__`, set `self.entered = False` and return `None` to let exceptions propagate.
In `status`, use an f-string with `self.name` and `self.entered`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.