gpt4 book ai didi

python - Graphviz - 属性错误 : object has no attribute 'partition'

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:27 24 4
gpt4 key购买 nike

我正在使用 graphviz使用我的 Django 模型为每个自动机呈现来自以下 python 脚本的图形:

alphabets = automata.alphabet_set.all()
states = automata.states_set.all()
transitions = automata.transition_set.all()
dot = gv.Graph()
for state in states:
dot.node(state.state, state.state)
for transition in transitions:
dot.edge(transition.current_state, transition.next_state, transition.input)
dot.render( automata.id + '.gv', view=True)

这些是我的模型:

class Automata(models.Model):
pass

class Alphabet(models.Model):
alphabet = models.CharField()
automata = models.ForeignKey(Automata, on_delete = models.CASCADE)

class States(models.Model):
state = models.CharField()
automata = models.ForeignKey(Automata, on_delete = models.CASCADE)

class Transition(models.Model):
current_state = models.ForeignKey(States, on_delete = models.CASCADE, related_name = 'current')
input = models.ForeignKey(Alphabet, on_delete = models.CASCADE)
next_state = models.ForeignKey(States, on_delete = models.CASCADE, related_name = 'next')
automata = models.ForeignKey(Automata, on_delete = models.CASCADE)

但每次我尝试执行我的脚本时,我都会收到以下错误:

Traceback (most recent call last):
File "make_graph.py", line 36, in <module>
dot.edge(transition.current_state, transition.next_state, transition.input)
File "/home/nids/automata/auto/lib/python3.5/site-packages/graphviz/dot.py", line 116, in edge
tail_name = self.quote_edge(tail_name)
File "/home/nids/automata/auto/lib/python3.5/site-packages/graphviz/lang.py", line 63, in quote_edge
node, _, rest = identifier.partition(':')
AttributeError: 'States' object has no attribute 'partition'

知道如果我简单地做就没有错误:dot.edge('A', 'B', 'edge label')

最佳答案

graphvis 代码需要传递给它的字符串,而不是 States 对象(您的模型)。

你可以在 source code 中看到这个在 quote_edge 函数中。

关于python - Graphviz - 属性错误 : object has no attribute 'partition' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41860513/

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