2017-03-12 62 views
2

嘗試使用py2neo版本3將數據導入乾淨的neo4j圖形數據庫。我已經定義了幾個節點類型,如下所示,並且所有似乎進展順利 - 除我沒有看到節點出現在我的neo4j瀏覽器中。py2neo v3 AttributeError:object has no attribute'db_exists'

下面是相關的導入代碼;我已驗證記錄正確加載到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,要麼。事實上,我發現這個屬性的唯一地方是在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') 

回答

0

好吧,我想我想通了。我一直在Flask的上下文中學習py2neo,所有這些類定義對於生成給定節點上關係的視圖(網頁)都很重要和有用。

但是對於我目前正在編寫的數據導入腳本,即首先實際創建節點和關係,我需要使用「節點」和「關係」的香草類,並指定類型作爲函數的參數。上面的原代碼的更新版本不產生錯誤,並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) 

其他兩個發現:

  1. 我的類繼承是沒譜開始說起,因爲我應該一直在試圖從Node繼承,而不是GraphObject(即使GraphObject是正確的類在瓶的情況下繼承回)
  2. 對於Node類,我個人有T o使用dict樣式的屬性分配,方括號和鍵名稱作爲引用字符串;點符號在這裏基本沒有了,我很驚訝我沒有得到更多的錯誤,並且更快。
相關問題