python - 在 Python 中创建实例
问题描述
我在 python 中创建实例时遇到了麻烦。我尝试以多种方式重写它,遵循堆栈中的提示,但不知何故,代码仍然给我一个错误,即我创建的实例不是 PetriNet 的实例(命名元组)。谁能给我一个提示可能是什么问题?我还添加了错误的测试代码,以及我得到的错误。
PetriNet = namedtuple(typename='PetriNet', field_names=['places', 'transitions', 'arcs'])
Place = namedtuple('Place', ['label', 'marking'])
Arc = namedtuple('Arc', ['source', 'target', 'weight'])
# You can use the ploting function to plot your petri net for S
example = PetriNet(places={Place('p1', 1), Place('p2', 0)},
transitions={'t1'},
arcs={Arc('p1', 't1', 1), Arc('t1', 'p2', 1)})
def make_synchronized_petri_net_S():
PetriNet = namedtuple(typename='PetriNet', field_names=['places', 'transitions', 'arcs'])
Place = namedtuple('Place', ['label', 'marking'])
Arc = namedtuple('Arc', ['source', 'target', 'weight'])
pet_places = [{Place('p11', 1), Place('p12', 0), Place('p21',1), Place('p22',0)}]
pet_transitions = [{'a','b','c','d','e'}]
pet_arcs = [{Arc('p11', 'a', 1), Arc('a', 'p12', 1), Arc('p12', 'b', 0), Arc('b', 'p11', 1), Arc('p12', 'c', 0), Arc('c', 'p12', 0), Arc('p21', 'c', 1), Arc('c', 'p22', 1), Arc('p22', 'e', 0), Arc('e', 'p21', 1), Arc('p22', 'd', 0), Arc('d', 'p21', 1)}]
places = set()
transitions = set()
arcs = set()
S = PetriNet(places,transitions,arcs)
S = S._replace(places= pet_places, transitions=pet_transitions, arcs =pet_arcs)
print("Our S is",S)
return S
这是测试错误的代码:
S = make_synchronized_petri_net_S()
assert isinstance(S, PetriNet)
assert isinstance(S.places.pop(), Place)
assert isinstance(S.arcs.pop(), Arc)
最后是错误: assert isinstance(S, PetriNet) 中的 AssertionError
非常感谢您的任何建议。
解决方案
您在两个地方创建PetriNet
namedtuple:在全局范围内和在make_synchronized_petri_net_S
. 这是一个问题,因为使用其中一个创建的实例将无法通过isinstance
使用另一个的测试。isinstance(S, PetriNet)
失败是因为 S 是 make_synchronized_petri_net_S-PetriNet,但第二个参数是全局 Petrinet。
最简单的解决方案是从您的函数中删除所有 namedtuple 声明,并专门使用全局声明。
此外,您的isinstance(S.places.pop(), Place)
断言将失败,因为pet_places
它是一组 Places 的列表。所以S.places.pop()
不返回一个地方,它返回一个集合。一种可能的解决方案是通过删除方括号将您的集合列表更改为集合。我不知道这是否适合您的业务逻辑,但至少它使 AssertionErrors 消失。
from collections import namedtuple
PetriNet = namedtuple(typename='PetriNet', field_names=['places', 'transitions', 'arcs'])
Place = namedtuple('Place', ['label', 'marking'])
Arc = namedtuple('Arc', ['source', 'target', 'weight'])
# You can use the ploting function to plot your petri net for S
example = PetriNet(places={Place('p1', 1), Place('p2', 0)},
transitions={'t1'},
arcs={Arc('p1', 't1', 1), Arc('t1', 'p2', 1)})
def make_synchronized_petri_net_S():
pet_places = {Place('p11', 1), Place('p12', 0), Place('p21',1), Place('p22',0)}
pet_transitions = {'a','b','c','d','e'}
pet_arcs = {Arc('p11', 'a', 1), Arc('a', 'p12', 1), Arc('p12', 'b', 0), Arc('b', 'p11', 1), Arc('p12', 'c', 0), Arc('c', 'p12', 0), Arc('p21', 'c', 1), Arc('c', 'p22', 1), Arc('p22', 'e', 0), Arc('e', 'p21', 1), Arc('p22', 'd', 0), Arc('d', 'p21', 1)}
places = set()
transitions = set()
arcs = set()
S = PetriNet(places,transitions,arcs)
S = S._replace(places= pet_places, transitions=pet_transitions, arcs =pet_arcs)
print("Our S is",S)
return S
S = make_synchronized_petri_net_S()
assert isinstance(S, PetriNet)
assert isinstance(S.places.pop(), Place)
assert isinstance(S.arcs.pop(), Arc)
推荐阅读
- python - Python Scrapy 不断返回“无效语法”
- c# - LINQ 序列 - 它们如何在 IL 中链接?
- r - Beamer 幻灯片中的 RMarkdown 绘图在图上方生成 -.bb 文本
- bash - 使用 MacOS 终端对同一目录下的多个 VCF 文件执行 Plink
- javascript - 将具有属性值的数组字符串化/序列化为文本并重现
- r - 使用 igraph 绘制不对称关系
- firebase - Firebase/谷歌云存储文件安全
- javascript - 在javascript中用空格分割后获取第一个带冒号的字符串
- c++ - 如何根据输入分配变量?C++
- android - 当我的应用程序处于后台时,如何检测网络状态/wifi 状态已更改?