alysivji / finite-state-machine

Lightweight, decorator-based Python implementation of a Finite State Machine
MIT License
111 stars 12 forks source link

Add support for enums #7

Closed alysivji closed 3 years ago

sammydowds commented 3 years ago

Hey Aly! I think I want to take this on. I have looked over these docs: https://docs.python.org/3/library/enum.html, and would love to connect more on details of implementation and what you are looking for!

alysivji commented 3 years ago

Awesome! Would love your help in getting this feature implemented.

TBH, I'm not quite sure what the implementation of this will be. This library was created to mimic the functionality django-fsm without requiring an ORM. It might make sense to work through their quickstart and try to implement a field using Enums.

Next, I take a look at the examples folder to get a sense of how the library is supposed to work.

Then I would try to implement the examples using Enums (both string and ints). Take a look at the tests folder to see how we can test State Machine examples. This might result in some errors which you can start to investigate / trace to find out exactly what to change.

There might not be too much actual dev work to implement this as the underlying types are strings and ints, most of the work is about making sure this feature behaves similarly to how django-fsm does things.

sammydowds commented 3 years ago

Ok cool. I will dig into the code more tomorrow! I was able to build out an example similar to how they did it within their examples, however, it seems too simple. I might be missing something. I will attach an example tomorrow!

sammydowds commented 3 years ago

So, from what I have seen in the examples - it looks like they just wrapped state with a class. Then they store the enum value to an int field through Django. They do not use the Enum built-in Python class.

I think since the transition function allows for integers, that it already supports enums? I rebuilt the turnstile example with how they presented enums in the django-fsm example. I think the only downside here is when you return state of the turnstile it is an integer with no description.

from finite_state_machine import StateMachine, transition

class TurnstileStateEnum(object):
    CLOSE = 1
    OPEN = 2

class Turnstile(StateMachine):

    def __init__(self):
        self.state = TurnstileStateEnum.CLOSE
        super().__init__()

    @transition(source=[TurnstileStateEnum.CLOSE, TurnstileStateEnum.OPEN], target=TurnstileStateEnum.OPEN)
    def insert_coin(self):
        pass

    @transition(source=TurnstileStateEnum.OPEN, target=TurnstileStateEnum.CLOSE)
    def pass_thru(self):
        pass
sammydowds commented 3 years ago

Apologies for the format above... I couldn't quite get the code insert to work properly.

alysivji commented 3 years ago

No worries on the format, you can mark code blocks with three backticks.

Let me think about this API a bit. I think it might make sense to deviate from django-fsm to support Enums properly.

sammydowds commented 3 years ago

I think I agree with the sentiment of deviating. I think the way they are building it out is mutable as well? Let me know what you think, and I can start working on it.

Also, I am new to the Github collaboration aspect of things - thanks for the responses so far!

alysivji commented 3 years ago

Closed via #24 and #25