diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-17 18:08:35 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-17 18:08:35 -0500 |
commit | f0264bd8d1a548a50b3e5f7d99cfefd577942d14 (patch) | |
tree | c37f35dcd201afedc03b3f38f0191d7d2005aade /examples | |
parent | a0d49ee85f83348ad694b716b18c7cca604ca3ca (diff) | |
download | pyparsing-git-f0264bd8d1a548a50b3e5f7d99cfefd577942d14.tar.gz |
Fix generated stateMixin class to properly implement overridable transition methods instead of messing with getattr; allows use of `super().transition_name()` in classes that subclass from the Mixin
Diffstat (limited to 'examples')
-rw-r--r-- | examples/statemachine/libraryBookDemo.py | 4 | ||||
-rw-r--r-- | examples/statemachine/statemachine.py | 12 |
2 files changed, 7 insertions, 9 deletions
diff --git a/examples/statemachine/libraryBookDemo.py b/examples/statemachine/libraryBookDemo.py index 95e5996..5e73f07 100644 --- a/examples/statemachine/libraryBookDemo.py +++ b/examples/statemachine/libraryBookDemo.py @@ -24,9 +24,9 @@ class RestrictedBook(Book): # specialized checkout to check permission of user first def checkout(self, user=None): if user in self._authorized_users: - self._state = self._state.checkout() + super().checkout() else: - raise Exception("{0} could not check out restricted book".format((user, "anonymous")[user is None])) + raise Exception("{0} could not check out restricted book".format(user if user is not None else "anonymous")) def run_demo(): diff --git a/examples/statemachine/statemachine.py b/examples/statemachine/statemachine.py index f4244a5..7e4de86 100644 --- a/examples/statemachine/statemachine.py +++ b/examples/statemachine/statemachine.py @@ -219,17 +219,15 @@ def expand_named_state_definition(source, loc, tokens): " def state(self):", " return self._state", - " # get behavior/properties from current state", - " def __getattr__(self, attrname):", - " attr = getattr(self._state, attrname)", - " if attrname in {baseStateClass}.transition_names:".format(baseStateClass=baseStateClass), - " return lambda x=self: setattr(x, '_state', attr())", - " return attr", - " def __str__(self):", " return '{0}: {1}'.format(self.__class__.__name__, self._state)" ]) + # define transition methods to be delegated to the _state instance variable + statedef.extend( + " def {tn_name}(self): self._state = self._state.{tn_name}()".format(tn_name=tn) + for tn in transitions + ) return indent + ("\n" + indent).join(statedef) + "\n" namedStateMachine.setParseAction(expand_named_state_definition) |