Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for enums #357

Merged
merged 1 commit into from
Aug 20, 2019
Merged

Add support for enums #357

merged 1 commit into from
Aug 20, 2019

Conversation

artofhuman
Copy link
Contributor

Hi, it's a simple draft attempt to add support for enums (#334).
The main idea is to pass states to machine as we can do it with strings to State class and work with it.

I added some tests to illustrate the api

Example:

class States(enum.Enum):
  RED = 1
  YELLOW = 2
  GREEN = 3

m = Machine(states=States, initial=States.YELLOW)

m.states
=> OrderedDict([('RED', <State('RED')@4370833592>), ('YELLOW', <State('YELLOW')@4370472240>), ('GREEN', <State('GREEN')@4371112008>)])

m.to_RED()

m.is_RED() # => True
m.state => <States.RED: 1>

m.add_transition('switch_to_yellow', self.States.RED, self.States.YELLOW)

But now in the code base, we have many conditions isinstance(state, string_types, ) or isinstance(state, State). For support enums i added check isinstance(state, enum.Enum) and it not looking good and we should drop support for python < 3.4.

Any suggestions how enums should work are welcome :)

@aleneum what do you think about this implementation?

@artofhuman artofhuman changed the title Add enums support Add support for enums Jul 25, 2019
@aleneum aleneum merged commit 7555537 into pytransitions:master Aug 20, 2019
@aleneum aleneum mentioned this pull request Aug 20, 2019
@andrewpwade andrewpwade mentioned this pull request Aug 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants