【问题标题】:Grakn Python client API - How to recieve certain attributes for a thingGrakn Python 客户端 API - 如何接收事物的某些属性
【发布时间】:2019-12-06 16:56:21
【问题描述】:

我目前正在深入研究 Python 客户端的 Grakn phone_calls 示例,并且正在玩一些。我目前尝试的是仅获取 Grakn thing 的某些属性。 Python API documentation 通知我使用 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'

【问题讨论】:

    标签: python vaticle-typedb knowledge-graph


    【解决方案1】:

    问题是我错误地将 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 新手有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 2018-08-06
      • 2016-10-05
      • 2017-05-03
      • 2020-04-14
      相关资源
      最近更新 更多