简体   繁体   中英

Creating an instance in Python

I am having troubles with creating an instance in python. I tried to rewrite this in many ways, followed the tips from stack but somehow the code still gives me an error that my created instance is not an instance of PetriNet (namedtuple). Could anyone give me a hint what might be wrong? I also add the test code for errors, and error which I got.

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

this is a code to test the error :

S = make_synchronized_petri_net_S()
assert isinstance(S, PetriNet)
assert isinstance(S.places.pop(), Place)
assert isinstance(S.arcs.pop(), Arc)

and finally the error: AssertionError in assert isinstance(S, PetriNet)

Thank you very much for any advices.

You create the PetriNet namedtuple in two places: in the global scope, and in make_synchronized_petri_net_S . This is a problem because instances created using one of these will not pass an isinstance test using the other. isinstance(S, PetriNet) is failing because S is a make_synchronized_petri_net_S-PetriNet, but the second argument is the global-Petrinet.

The simplest solution is to delete all of the namedtuple declarations from your function, and exclusively use the global ones.

Additionally, your isinstance(S.places.pop(), Place) assertion will fail, because pet_places is a list of sets of Places. So S.places.pop() doesn't return a Place, it returns a set. One possible solution is to change your list-of-sets into just sets, by deleting the square brackets. I don't know if this is an appropriate solution for your business logic, but at least it makes the AssertionErrors go away.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM