嵌套参数不编译我正在尝试将我的代码编译成Python 3模块。当我在空闲中选择“运行模块”时,它运行良好,但是当我试图创建一个发行版时,会收到以下语法错误:File "/usr/local/lib/python3.2/dist-packages/simpletriple.py", line 9
def add(self, (sub, pred, obj)):
^
SyntaxError: invalid syntax有人能帮我指出语法有什么问题吗?以下是完整的代码:import csvclass SimpleGraph:
def __init__(self):
self._spo = {}
self._pos = {}
self._osp = {}
def add(self, (sub, pred, obj)):
"""
Adds a triple to the graph.
"""
self._addToIndex(self._spo, sub, pred, obj)
self._addToIndex(self._pos, pred, obj, sub)
self._addToIndex(self._osp, obj, sub, pred)
def _addToIndex(self, index, a, b, c):
"""
Adds a triple to a specified index.
"""
if a not in index: index[a] = {b:set([c])}
else:
if b not in index[a]: index[a][b] = set([c])
else: index[a][b].add(c)
def remove(self, (sub, pred, obj)):
"""
Remove a triple pattern from the graph.
"""
triples = list(self.triples((sub, pred, obj)))
for (delSub, delPred, delObj) in triples:
self._removeFromIndex(self._spo, delSub, delPred, delObj)
self._removeFromIndex(self._pos, delPred, delObj, delSub)
self._removeFromIndex(self._osp, delObj, delSub, delPred)
def _removeFromIndex(self, index, a, b, c):
"""
Removes a triple from an index and clears up empty indermediate structures.
"""
try:
bs = index[a]
cset = bs[b]
cset.remove(c)
if len(cset) == 0: del bs[b]
if len(bs) == 0: del index[a]
# KeyErrors occur if a term was missing, which means that it wasn't a valid delete:
except KeyError:
pas
3 回答
守候你守候我
TA贡献1802条经验 获得超10个赞
自从那本书(被OP引用)出版以来,编码中有很多变化。我正在研究这段代码,面对同样的问题,我看到了你的帖子。有什么资源可以让我回到原处,以便更清楚地了解代码是如何变化的,因为我正在阅读这本书中的所有代码(最终)。
添加回答
举报
0/150
提交
取消