Code Monkey home page Code Monkey logo

Comments (6)

aleneum avatar aleneum commented on May 18, 2024

Hi,

they are created in core.py in __getattr__:

def __getattr__(self, name):
        if name.startswith('__'):
            if name in self.__dict__:
                return self.__dict__[name]
            else:
                raise AttributeError("{} does not exist".format(name))
        terms = name.split('_')
        if terms[0] in ['before', 'after']:
            name = '_'.join(terms[1:])
            if name not in self.events:
                raise MachineError('Event "%s" is not registered.' % name)
            return partial(self.events[name].add_callback, terms[0])

        # that's where the magic happens
        # on_enter / on_exit queries are concatenated with the current state 
        # and predefined with partial
        elif name.startswith('on_enter') or name.startswith('on_exit'):
            state = self.get_state('_'.join(terms[2:]))
            return partial(state.add_callback, terms[1])
        else:
            if name in self.__dict__:
                return self.__dict__[name]
            else:
                raise AttributeError("{} does not exist".format(name))

on_exit_solid has to be called from the machine, not from the model.

machine.on_exit_solid('say_goodbye')

should work.

from transitions.

aleneum avatar aleneum commented on May 18, 2024

Okay,

after some more careful reading what you said and what the documentation states, I guess you are right. Afaik, it does not work the way its stated in the documentation.

from transitions.

tyarkoni avatar tyarkoni commented on May 18, 2024

Good catch. I'll fix this right now. Thanks @TheMysteriousX!

from transitions.

aleneum avatar aleneum commented on May 18, 2024

in core.Machine.add_states there is a check if an on_enter_State method exists but it is not created otherwise.

#  Add enter/exit callbacks if there are existing bound methods
    enter_callback = 'on_enter_' + state_name
    if hasattr(self.model, enter_callback) and \
               inspect.ismethod(getattr(self.model, enter_callback)):
        state.add_callback('enter', enter_callback)
    exit_callback = 'on_exit_' + state_name
    if hasattr(self.model, exit_callback) and \
               inspect.ismethod(getattr(self.model, exit_callback)):
        state.add_callback('exit', exit_callback)

from transitions.

tyarkoni avatar tyarkoni commented on May 18, 2024

I assume we don't want to allow on_enter_* to work on the model itself, right? I mean, we definitely don't want to start patching an arbitrary model class's getattr method. So I'm just going to update the README to be clear about this.

from transitions.

aleneum avatar aleneum commented on May 18, 2024

👍 agree

from transitions.

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.