在 Python 中创建一个实例
Creating an instance in Python
我在 python 中创建实例时遇到问题。我尝试以多种方式重写它,遵循堆栈中的提示,但不知何故代码仍然给我一个错误,即我创建的实例不是 PetriNet (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():
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)
最后是错误:
断言 isinstance(S, PetriNet) 中的断言错误
非常感谢您的任何建议。
您在两个地方创建了 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
是一组地点的列表。所以 S.places.pop()
不是 return 一个地方,它 return 是一个集合。一种可能的解决方案是通过删除方括号将您的集合列表更改为集合。我不知道这是否是适合您的业务逻辑的解决方案,但至少它使 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 中创建实例时遇到问题。我尝试以多种方式重写它,遵循堆栈中的提示,但不知何故代码仍然给我一个错误,即我创建的实例不是 PetriNet (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():
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)
最后是错误: 断言 isinstance(S, PetriNet) 中的断言错误
非常感谢您的任何建议。
您在两个地方创建了 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
是一组地点的列表。所以 S.places.pop()
不是 return 一个地方,它 return 是一个集合。一种可能的解决方案是通过删除方括号将您的集合列表更改为集合。我不知道这是否是适合您的业务逻辑的解决方案,但至少它使 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)