我有一个dict,它将字符串映射到类,以便可以实例化它们:
process_mapper = {VTNotifications.name: VTNotifications}
其中类看起来像:
class VTNotifications(AbstractNormaliser):
# String name for use defining KV
name = 'vt-notifications-feed'
def __init__(self, config):
super().__init__(config)
self.config = config
# Add source meta-data
self.meta_normaliser = MetaDataNormaliser(source='vt-notifications', doc_type='notification',
event_type='vt-notification')
我编写了一个测试,检查字符串是否确实正确返回,该测试通过:
def test_getter():
class_type = process_mapper.get(VTNotifications.name)
assert class_type == VTNotifications
但是,当我试图将实例化类所需的完整配置传递给一个方法时,它会执行所有的ISN指示,突然,它出现了以下错误:
def test_mapped_normaliser():
processor_name = VTNotifications.name
normaliser = {'name': processor_name,
'config': {'consumer': {'topic': 'some_topic',
'subscription': 'some_subscription'}},
'publisher': {'topic': 'some_other_topic'},
'project-id': 'some_id'}
config = {'normalisers': [normaliser]}
runner = NormaliserRunner(config)
assert isinstance(runner.processes[processor_name], VTNotifications)
class NormaliserRunner(object):
def __init__(self, config):
self.normalisers = config['normalisers']
self.processes = {}
self.config = config
self.map_processes()
def run(self):
for process in self.processes.values():
process.normalise()
# returns a dict of normaliser processes, and their mapped class implementations instantiated
def map_processes(self):
print(f"[*] Using processes mapper with {process_mapper.keys()} keys")
for normaliser in self.normalisers:
print(normaliser)
self.processes[normaliser['name']] = process_mapper.get(
normaliser['name'])(self.config[normaliser['name']]['config'])
============================= test session starts ==============================
platform linux -- Python 3.7.7, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /mnt/c/Users/ARES/Desktop/work/phoenix/normaliser
collected 3 items
test.py ..F [100%]
=================================== FAILURES ===================================
____________________________ test_mapped_normaliser ____________________________
def test_mapped_normaliser():
processor_name = VTNotifications.name
normaliser = {'name': processor_name,
'config': {'consumer': {'topic': 'some_topic',
'subscription': 'some_subscription'}},
'publisher': {'topic': 'some_other_topic'},
'project-id': 'some_id'}
config = {'normalisers': [normaliser]}
> runner = NormaliserRunner(config)
test.py:31:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
main.py:14: in __init__
self.map_processes()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <normaliser.main.NormaliserRunner object at 0x7ff3d0e7fc10>
def map_processes(self):
print(f"[*] Using processes mapper with {process_mapper.keys()} keys")
for normaliser in self.normalisers:
print(normaliser)
self.processes[normaliser['name']] = process_mapper.get(
> normaliser['name'])(self.config[normaliser['name']]['config'])
E KeyError: 'vt-notifications-feed'
main.py:26: KeyError
----------------------------- Captured stdout call -----------------------------
[*] Using processes mapper with dict_keys(['vt-notifications-feed']) keys
{'name': 'vt-notifications-feed', 'config': {'consumer': {'topic': 'some_topic', 'subscription': 'some_subscription'}}, 'publisher': {'topic': 'some_other_topic'}, 'project-id': 'some_id'}
=========================== short test summary info ============================
FAILED test.py::test_mapped_normaliser - KeyError: 'vt-notifications-feed'
========================= 1 failed, 2 passed in 0.33s ==========================
您甚至可以看到它打印钥匙,并且所需的钥匙就在那里。因此,我不确定Python在导致此问题的各种实现中所做的不同之处
您混淆了值和键。存在一个键值对,其值为
'vt-notifications-feed'
,但其键为'name'
相关问题 更多 >
编程相关推荐