中级
实现发布订阅模式
未完成
中级参考
代码结构已给出,请填写 ____ 处
def solve():
from pyodide.http import open_url
from io import StringIO
messages_csv = open_url(____).read()
import pandas as pd, json
from io import StringIO
msgs = pd.read_csv(StringIO(____))
class EventBus:
def __init__(____):
self.subscribers = {}
self.delivered = []
def subscribe(____):
self.subscribers.setdefault(____).append(____)
def publish(____):
for handler in self.subscribers.get(____):
try:
handler(____)
self.delivered.append((____))
except Exception as e:
self.delivered.append((____))
bus = EventBus()
notifications = []
inventory_ops = []
bus.subscribe('____', lambda t, p: inventory_ops.append(____))
bus.subscribe('____', lambda t, p: notifications.append(____))
bus.subscribe('____', lambda t, p: notifications.append(____))
bus.subscribe('____', lambda t, p: notifications.append(____))
for _, row in msgs.head(____).iterrows():
try:
payload = json.loads(____)
bus.publish(____)
except: pass
print(____)
print(____)
print(____)
示例
输入
solve()
期望输出
inventory_ops: [] notifications: ['notify_paid:ORD004', 'notify_pay:ORD007', 'notify_paid:ORD010'] 总投递: 3, ok=3
👑
升级 VIP
解锁全部题目,畅通无阻地学习
- ✓ 解锁全部训练包所有题目
- ✓ 查看完整参考代码和提示
- ✓ 浏览器内直接运行 Python 代码
- ✓ 自动批改 + 进度追踪
30天
¥18
1年
¥99
2年
¥158
3年
¥199