gpt4 book ai didi

python-3.x - py2neo v3 属性错误 : object has no attribute 'db_exists'

转载 作者:行者123 更新时间:2023-12-02 03:06:06 25 4
gpt4 key购买 nike

尝试使用 py2neo 版本 3 将数据导入到一个干净的 Neo4j 图形数据库中。我定义了几个节点类型,如下所示,一切似乎都很顺利——除了我没有看到节点显示在我的 neo4j 浏览器中。

这是相关的导入代码;我已经验证记录正确加载到 Python 变量中。

for row in data:    
ds = DataSource()
# parse Source of Information column as a list, trimming whitespace
ds.uri = list(map(str.strip, row['data_source'].split(',')))
ds.description = row['data_source_description']
graph.merge(ds)

但是当我尝试做 graph.exists(ds) ,我得到了以下一组错误/回溯:
Traceback (most recent call last):
File "mydir/venv/lib/python3.5/site-packages/py2neo/database/__init__.py", line 1139, in exists
return subgraph.__db_exists__(self)
AttributeError: 'DataSource' object has no attribute '__db_exists__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mydir/venv/lib/python3.5/site-packages/py2neo/database/__init__.py", line 478, in exists
return self.begin(autocommit=True).exists(subgraph)
File "mydir/venv/lib/python3.5/site-packages/py2neo/database/__init__.py", line 1141, in exists
raise TypeError("No method defined to determine the existence of object %r" % subgraph)
TypeError: No method defined to determine the existence of object <DataSource uri=['my_uri']>

令我惊讶的是,我找不到其他论坛帖子讨论这个问题。我猜是从 GraphObject 继承的问题,但是 there doesn't seem to be an explicit definition of a __db_exists__ property for GraphObject , 任何一个。事实上,我唯一能找到提到的属性(property)的地方是 definition of the exists function ,当它产生这个错误时。

谁能看到我在这里做错了什么?

节点类定义如下:
class Content(GraphObject):             # group Person and Institution
pass

class Person(Content):
__primarykey__ = 'name'

name = Property()
in_scholar_names = Property()
#
mentored = RelatedTo('Person')
mentored_by = RelatedFrom('Person', 'MENTORED')
worked_alongside = Related('Person', 'WORKED_ALONGSIDE')
studied_at = RelatedTo('Institution')
worked_at = RelatedTo('Institution')
tagged = RelatedTo('Tag')
member_of = RelatedTo('Institution')

last_update = RelatedTo('UpdateLog')

def __lt__(self, other):
return self.name.split()[-1] < other.name.split()[-1]

class Institution(Content):
__primarykey__ = 'name'
#
name = Property()
location = Property()
type = Property()
carnegie_class = Property()
#
students = RelatedFrom('Person', 'STUDIED_AT')
employees = RelatedFrom('Person', 'WORKED_AT')
members = RelatedFrom('Person', 'MEMBER_OF')

last_update = RelatedTo('UpdateLog')

def __lt__(self, other):
return self.name < other.name


class User(GraphObject):
__primarykey__ = 'username'

username = Property()
joined = Property()
last_access = Property()
active = Property()

contributed = RelatedTo('UpdateLog')


class Provenance(GraphObject): # group UpdateLog and DataSource
pass
#
class UpdateLog(Provenance):
__primarykey__ = 'id'

id = Property()
timestamp = Property()
query = Property()

previous = RelatedTo('UpdateLog', 'LAST_UPDATE')
next = RelatedFrom('UpdateLog', 'LAST_UPDATE')
based_on = RelatedTo('Provenance', 'BASED_ON')

affected_nodes = RelatedFrom('Content', 'LAST_UPDATE')
contributed_by = RelatedFrom('User', 'CONTRIBUTED')

class DataSource(Provenance):
__primarykey__ = 'uri'

id = Property()
description = Property()
uri = Property()

source_for = RelatedFrom('UpdateLog', 'BASED_ON')


class Tag(GraphObject):
__primarykey__ = 'name'

name = Property()
description = Property()

see_also = Related('Tag')
tagged = RelatedFrom('Content')

最佳答案

好吧,我想我想通了。我一直在 Flask 的上下文中学习 py2neo,其中所有这些类定义对于生成给定节点上的关系的 View (网页)都很重要和有用。

但是对于我目前正在编写的数据导入脚本,即首先要实际创建节点和关系,我需要使用“节点”和“关系”的原版类,并且只需将类型指定为参数功能。上面原始代码的这个更新版本不会产生错误,并且 graph.exists(ds)返回 true之后:

for row in data:    
ds = Node("DataSource")
# parse Source of Information column as a list, trimming whitespace
ds['uri'] = list(map(str.strip, row['data_source'].split(',')))
ds['description'] = row['data_source_description']
graph.merge(ds)

另外两个值得注意的发现:
  • 我的类继承一开始就不合时宜,因为我应该尝试从 Node 继承。 ,不是 GraphObject (尽管 GraphObject 是在 Flask 上下文中继承的正确类)
  • 对于 Node类,我必须使用 dict 样式的属性分配,方括号和键名作为带引号的字符串;点符号在这里偏离了基础,我很惊讶我没有抛出更多错误,而且更快。
  • 关于python-3.x - py2neo v3 属性错误 : object has no attribute 'db_exists' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42751642/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com