0

我目前正在研究 Python 客户端的 Grakn phone_calls 示例,并且正在玩一些。我目前尝试的是只获得 Grakn事物的某些属性。Python API 文档通知我使用并thing.attributes(attribute_types)声明 attribute_types 应该是 AttributeTypes 列表。

我尝试了以下传递 AttributeTypes 的 python 列表:

for attr in answer.attributes([transaction.get_schema_concept('first-name'), transaction.get_schema_concept('phone-number')]):
    print("  {}: {}".format(attr.type().label(), attr.value()))

导致以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-24-277eb53a924b> in <module>()
     31                     print("{}. {}: {} - Type: {} - Thing: {}".format(i, answer.type().label(), answer.id, answer.is_type(), answer.is_thing()))
     32 
---> 33                     for attr in answer.attributes([transaction.get_schema_concept('first-name'), transaction.get_schema_concept('phone-number'), transaction.get_schema_concept('is-customer')]):
     34                         print("  {}: {}".format(attr.type().label(), attr.value()))
     35                     #for role in answer.roles():

~\Anaconda3\envs\grakn\lib\site-packages\grakn\service\Session\Concept\Concept.py in attributes(self, *attribute_types)
    481     def attributes(self, *attribute_types):
    482         """ Retrieve iterator of this Thing's attributes, filtered by optionally provided attribute types """
--> 483         attrs_req = RequestBuilder.ConceptMethod.Thing.attributes(attribute_types)
    484         method_response = self._tx_service.run_concept_method(self.id, attrs_req)
    485         from grakn.service.Session.util import ResponseReader

~\Anaconda3\envs\grakn\lib\site-packages\grakn\service\Session\util\RequestBuilder.py in attributes(attribute_types)
    549                 attributes_req = concept_messages.Thing.Attributes.Req()
    550                 for attribute_type_concept in attribute_types:
--> 551                     grpc_attr_type_concept = RequestBuilder.ConceptMethod._concept_to_grpc_concept(attribute_type_concept)
    552                     attributes_req.attributeTypes.extend([grpc_attr_type_concept])
    553                 concept_method_req = concept_messages.Method.Req()

~\Anaconda3\envs\grakn\lib\site-packages\grakn\service\Session\util\RequestBuilder.py in _concept_to_grpc_concept(concept)
    205             """ Takes a concept from ConceptHierarcy and converts to GRPC message """
    206             grpc_concept = concept_messages.Concept()
--> 207             grpc_concept.id = concept.id
    208             base_type_name = concept.base_type
    209             grpc_base_type = BaseTypeMapping.name_to_grpc_base_type[base_type_name]

AttributeError: 'list' object has no attribute 'id'
4

1 回答 1

0

问题是我错误地将 AttributeTypes 列表解释为 Python 列表,而是可以将一个或多个 AttributeTypes 作为参数传递。例如:

for attr in answer.attributes(transaction.get_schema_concept('first-name'), transaction.get_schema_concept('phone-number'), transaction.get_schema_concept('is-customer')):
    print("  {}: {}".format(attr.type().label(), attr.value())) 

我希望这对其他 Grakn 新手有所帮助。

于 2019-12-06T16:56:21.757 回答