Giter Site home page Giter Site logo

@<transition>.cond decorator bug, general question about running code on every transition even if TransitionNotAllowed is raised about python-statemachine HOT 3 CLOSED

lucaswhipple-sfn avatar lucaswhipple-sfn commented on June 18, 2024
@.cond decorator bug, general question about running code on every transition even if TransitionNotAllowed is raised

from python-statemachine.

Comments (3)

lucaswhipple-sfn avatar lucaswhipple-sfn commented on June 18, 2024 1

Well, this is embarrassing - I was using statemachines version 1.0.3. Now that I've upgraded, the decorator works. Sorry!

Working example -

from statemachine import StateMachine, State, exceptions
from statemachine.exceptions import TransitionNotAllowed
class ExampleStateMachine(StateMachine):
    idle = State("idle", initial = True)
    moving = State("Moving!")
    stopped = State("Stopped")
    forbidden_state = State("Forbidden!")

    start_moving = moving.from_(idle,stopped)
    move_from_stop = stopped.to(moving)
    stop_from_moving = moving.to(stopped)
    cycle = move_from_stop | stop_from_moving
    forbidden_transition = idle.to(forbidden_state, cond = "always_return_false")
    
    @cycle.cond
    def cycle_cond(self):
        print("This should run")
        return True
    
    def on_transition(self):
        print("This should print on a transition!")

    def always_return_false(self):
        return False
    
    
    #cycle._add_callback(cycle_cond, "cond")

exm = ExampleStateMachine()
try:
    exm.forbidden_transition()
except TransitionNotAllowed:
    print("Transition not allowed")
exm.start_moving()
exm.cycle()
print(exm.current_state)
exm.move_from_stop()

output

Transition not allowed
This should print on a transition!
This should run
This should print on a transition!
State('Stopped', id='stopped', value='stopped', initial=False, final=False)
This should run
This should print on a transition!

from python-statemachine.

lucaswhipple-sfn avatar lucaswhipple-sfn commented on June 18, 2024

Update:

I made a mock machine that works, but it uses the private method _add_callback to add a callback to a transition list.

This has a disadvantage - it binds the condition to every transition in the transitionlist, which for some cases may be desirable, but I can imagine only wanting it bound to the transitionlist instead of the elements of the transitionlist.

I think this will resolve my issue in the short-term, but I think it would be worthwhile to fix the decorator bug and also perhaps correct the documentation.

from statemachine import StateMachine, State, exceptions
from statemachine.exceptions import TransitionNotAllowed
class ExampleStateMachine(StateMachine):
    idle = State("idle", initial = True)
    moving = State("Moving!")
    stopped = State("Stopped")
    forbidden_state = State("Forbidden!")

    start_moving = moving.from_(idle,stopped)
    move_from_stop = stopped.to(moving)
    stop_from_moving = moving.to(stopped)
    cycle = move_from_stop | stop_from_moving
    
    #@cycle.cond
    def cycle_cond(self):
        print("This should run")
        return True
    
    def on_transition(self):
        print("This should print on a transition!")

    def always_return_false(self):
        return False
    
    forbidden_transition = idle.to(forbidden_state, cond = always_return_false)
    cycle._add_callback(cycle_cond, "cond")

exm = ExampleStateMachine()
try:
    exm.forbidden_transition()
except TransitionNotAllowed:
    print("Transition not allowed")
exm.start_moving()
exm.cycle()
print(exm.current_state)
exm.move_from_stop()

output:

Transition not allowed
This should print on a transition!
This should run
This should print on a transition!
State('Stopped', id='stopped', value='stopped', initial=False, final=False)
This should run
This should print on a transition!

from python-statemachine.

fgmacedo avatar fgmacedo commented on June 18, 2024

Hi @lucaswhipple-sfn , nice it worked as expected!

from python-statemachine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.