From a0d49ee85f83348ad694b716b18c7cca604ca3ca Mon Sep 17 00:00:00 2001 From: Paul McGuire Date: Tue, 16 Apr 2019 23:09:57 -0500 Subject: Added change note re: changes to statemachine example; some code reformat/cleanup/commenting in statemachine.py --- examples/statemachine/statemachine.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'examples/statemachine/statemachine.py') diff --git a/examples/statemachine/statemachine.py b/examples/statemachine/statemachine.py index c46e3d0..f4244a5 100644 --- a/examples/statemachine/statemachine.py +++ b/examples/statemachine/statemachine.py @@ -49,6 +49,9 @@ namedStateMachine = (pp.Keyword("statemachine") + ident("name") + ":" def expand_state_definition(source, loc, tokens): + """ + Parse action to convert statemachine to corresponding Python classes and methods + """ indent = " " * (pp.col(loc, source) - 1) statedef = [] @@ -66,9 +69,11 @@ def expand_state_definition(source, loc, tokens): "class %s(object):" % baseStateClass, " def __str__(self):", " return self.__class__.__name__", + " @classmethod", " def states(cls):", " return list(cls.__subclasses__())", + " def next_state(self):", " return self._next_state_class()", ]) @@ -106,6 +111,10 @@ stateMachine.setParseAction(expand_state_definition) def expand_named_state_definition(source, loc, tokens): + """ + Parse action to convert statemachine with named transitions to corresponding Python + classes and methods + """ indent = " " * (pp.col(loc, source) - 1) statedef = [] # build list of states and transitions @@ -152,15 +161,18 @@ def expand_named_state_definition(source, loc, tokens): "class %s(object):" % baseStateClass, " def __str__(self):", " return self.__class__.__name__", + " @classmethod", " def states(cls):", " return list(cls.__subclasses__())", + " @classmethod", " def next_state(cls, name):", " try:", " return cls.tnmap[name]()", " except KeyError:", " raise InvalidTransitionException('%s does not support transition %r'% (cls.__name__, name))", + " def __bad_tn(name):", " def _fn(cls):", " raise InvalidTransitionException('%s does not support transition %r'% (cls.__name__, name))", @@ -194,6 +206,7 @@ def expand_named_state_definition(source, loc, tokens): ) ]) + # define Mixin class for application classes that delegate to the state statedef.extend([ "class {baseStateClass}Mixin:".format(baseStateClass=baseStateClass), " def __init__(self):", @@ -217,7 +230,6 @@ def expand_named_state_definition(source, loc, tokens): " return '{0}: {1}'.format(self.__class__.__name__, self._state)" ]) - return indent + ("\n" + indent).join(statedef) + "\n" namedStateMachine.setParseAction(expand_named_state_definition) -- cgit v1.2.1