clipspy:如何获取实例的插槽字典
clipspy: How to get slot dictionary of instance
documentation 包含此示例代码:
...
klass = env.find_class('MyClass')
instance = klass.new_instance('instance-name')
instance['One'] = 1
instance['Two'] = 2
是否有内置功能可以为我提供插槽及其值的字典?就像pyclips
的.Slots()
方法一样。
像这样:
def get_slot_dict(clips_instance):
slot_dict = {}
for s in clips_instance.instance_class.slots():
slot_dict[s.name] = clips_instance[s.name]
return slot_dict
从 clipspy
0.3.0 开始,您可以迭代对象元数据来构建数据结构。
implied_fact = env.assert_string("(implied-fact 1 2 3)")
list(implied_fact)
[1, 2, 3]
template_fact = env.assert_string("(template-fact (template-slot a-symbol))")
list(template_fact)
[('template-slot', 'a-symbol')]
dict(template_fact)
{'template-slot': 'a-symbol'}
klass = env.find_class('MY-CLASS')
instance = klass.new_instance('instance-name')
instance['One'] = 1
instance['Two'] = 2
dict(instance)
{'One': 1, 'Two': 2}
documentation 包含此示例代码:
...
klass = env.find_class('MyClass')
instance = klass.new_instance('instance-name')
instance['One'] = 1
instance['Two'] = 2
是否有内置功能可以为我提供插槽及其值的字典?就像pyclips
的.Slots()
方法一样。
像这样:
def get_slot_dict(clips_instance):
slot_dict = {}
for s in clips_instance.instance_class.slots():
slot_dict[s.name] = clips_instance[s.name]
return slot_dict
从 clipspy
0.3.0 开始,您可以迭代对象元数据来构建数据结构。
implied_fact = env.assert_string("(implied-fact 1 2 3)")
list(implied_fact)
[1, 2, 3]
template_fact = env.assert_string("(template-fact (template-slot a-symbol))")
list(template_fact)
[('template-slot', 'a-symbol')]
dict(template_fact)
{'template-slot': 'a-symbol'}
klass = env.find_class('MY-CLASS')
instance = klass.new_instance('instance-name')
instance['One'] = 1
instance['Two'] = 2
dict(instance)
{'One': 1, 'Two': 2}