2016-06-07 60 views
0

這裏是我的名字代碼文件CreateNode.py類型錯誤:創建()採用2個位置參數,但分別給予4

#!/usr/bin/python 
import py2neo 

from py2neo import Graph, Node 

def createNodeWithLabelProperties(): 
     print("Start Create label with prperties") 
     py2neo.authenticate ("localhost:7474", "neo4j", "XXXXXXX") 
     graph = Graph("http://localhost:7474/db/data") 
     #Create Node with properties 
     node1 = Node("LableFirst", name="Chuvindra Singh", age="27") 
     #create Node with 2 label 
     node2 = Node("Labelfirst", "LabelSecond",name="Koki Sing", age="27") 
     node3 = Node("Labelk", "LabelB",name="Manzil", age="27") 
     #now use Graph Object to create node 
     resultNodes = graph.create(node1, node2, node3) 
     for index in range(len(resultNodes)): 
       print("Created Node - ", index, ", ", resultNodes[inedx]) 
       print("End Printing the node") 

if __name__ =='__main__': 
     print("start Creating nodes") 
     createNodeWithLabelProperties() 
     print("End Creating nodes") 

當我運行這個文件,那麼它顯示的錯誤:

start Creating nodes 
    Start Create label with prperties 
    Traceback (most recent call last): 
     File "CreateNode.py", line 23, in <module> 
     createNodeWithLabelProperties() 
     File "CreateNode.py", line 16, in createNodeWithLabelProperties 
     resultNodes = graph.create(node1, node2, node3) 
    TypeError: create() takes 2 positional arguments but 4 were given 

代碼中的錯誤在哪裏?我無法理解,你能幫我一個人嗎?

回答

1

此消息:

TypeError: create() takes 2 positional arguments but 4 were given 

指此呼叫:

graph.create(node1, node2, node3) 

該呼叫被傳遞四個參數(計數調用者以及node1node2,和node3)到create方法,但該方法只需要兩個(包括invocant)。

在回顧API文檔,我相信,你正在使用py2neo(記錄here)第3版,其中Graph.create只需要一個單一的非調用者的參數,但試圖調用它,就好像它是版本2(記錄here),它可能需要多於一個。

+0

感謝@馬克卻沒有,它不接受列表參數,創建是圖形類的方法,可以接受一個以上的參數。 – csr

+0

在py2neo v2中,是的。你似乎在使用py2neo v3。請參閱編輯。 –

+0

謝謝,你是對的,我在v3工作 – csr

3

在py2neo v3中,create方法(以及所有類似方法)只接受一個參數,該參數可以是任何graphy對象(請參見manual page on types)。因此,您可以通過將它們合併到一個子圖中作爲參數來創建多個節點。在你的情況,你會希望是這樣的:

graph.create(node1 | node2 | node3) 
+0

謝謝,你可以建議任何來源,我可以學習py2neo版本3,在文檔中沒有具體的細節使用代碼。我正在閱讀僅包含v2細節的書。 – csr

+0

我不知道任何包含任一版本細節的書籍!我爲這個項目編寫的文檔在這裏 - > http://py2neo.org/v3/ –

相關問題