在Python中创建实例

时间:2018-09-24 13:50:50

标签: python instanceof

我在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)

最后是错误: 断言isinstance(S,PetriNet)中的AssertionError

非常感谢您提供任何建议。

1 个答案:

答案 0 :(得分:3)

您可以在两个地方创建PetriNet命名元组:在全局范围和make_synchronized_petri_net_S中。这是一个问题,因为使用其中一个创建的实例不会通过使用另一个的isinstance测试。 isinstance(S, PetriNet)失败,因为S是make_synchronized_petri_net_S-PetriNet,但是第二个参数是global-Petrinet。

最简单的解决方案是从函数中删除所有namedtuple声明,并仅使用全局声明。

另外,您的isinstance(S.places.pop(), Place)断言将失败,因为pet_places是一组位置列表。因此S.places.pop()不返回Place,而是返回set。一种可能的解决方案是通过删除方括号将集合列表更改为仅集合。我不知道这是否适合您的业务逻辑,但至少可以使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)