Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 550383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:16:58+00:00 2026-05-13T11:16:58+00:00

Related to this Stack Overflow question (C state-machine design) , could you Stack Overflow folks share

  • 0

Related to this Stack Overflow question (C state-machine design), could you Stack Overflow folks share your Python state-machine design techniques with me (and the community)?

At the moment, I am going for an engine based on the following:

class TrackInfoHandler(object):
    def __init__(self):
        self._state="begin"
        self._acc=""

    ## ================================== Event callbacks

    def startElement(self, name, attrs):
        self._dispatch(("startElement", name, attrs))

    def characters(self, ch):
        self._acc+=ch

    def endElement(self, name):
        self._dispatch(("endElement", self._acc))
        self._acc=""

    ## ===================================
    def _missingState(self, _event):
        raise HandlerException("missing state(%s)" % self._state)

    def _dispatch(self, event):
        methodName="st_"+self._state
        getattr(self, methodName, self._missingState)(event)

    ## =================================== State related callbacks

But I am sure there are tons of ways of going at it while leveraging Python’s dynamic nature (e.g. dynamic dispatching).

I am after design techniques for the “engine” that receives the “events” and “dispatches” against those based on the “state” of the machine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:16:58+00:00Added an answer on May 13, 2026 at 11:16 am

    I don’t really get the question. The State Design pattern is pretty clear. See the Design Patterns book.

    class SuperState( object ):
        def someStatefulMethod( self ):
            raise NotImplementedError()
        def transitionRule( self, input ):
            raise NotImplementedError()
    
    class SomeState( SuperState ):
        def someStatefulMethod( self ):
            actually do something()
        def transitionRule( self, input ):
            return NextState()
    

    That’s pretty common boilerplate, used in Java, C++, Python (and I’m sure other languages, also).

    If your state transition rules happen to be trivial, there are some optimizations to push the transition rule itself into the superclass.

    Note that we need to have forward references, so we refer to classes by name, and use eval to translate a class name to an actual class. The alternative is to make the transition rules instance variables instead of class variables and then create the instances after all the classes are defined.

    class State( object ):
        def transitionRule( self, input ):
            return eval(self.map[input])()
    
    class S1( State ): 
        map = { "input": "S2", "other": "S3" }
        pass # Overrides to state-specific methods
    
    class S2( State ):
        map = { "foo": "S1", "bar": "S2" }
    
    class S3( State ):
        map = { "quux": "S1" }
    

    In some cases, your event isn’t as simple as testing objects for equality, so a more general transition rule is to use a proper list of function-object pairs.

    class State( object ):
        def transitionRule( self, input ):
            next_states = [ s for f,s in self.map if f(input)  ]
            assert len(next_states) >= 1, "faulty transition rule"
            return eval(next_states[0])()
    
    class S1( State ):
        map = [ (lambda x: x == "input", "S2"), (lambda x: x == "other", "S3" ) ]
    
    class S2( State ):
        map = [ (lambda x: "bar" <= x <= "foo", "S3"), (lambda x: True, "S1") ]
    

    Since the rules are evaluated sequentially, this allows a “default” rule.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Related to this question , I decided to check the UDFs in my data
Related to this question: URL characters replacement in JSP with UrlRewrite I want to
Related to this question , what is the best practice for naming a mutex?
This question is related to this one , though I think I was a
Much related to this question , we have a scenario on my team where
[This question is somewhat related to this question , but the answers are not...]
This is related to this question . I'm writing a Flex app (a WindowedApplication)
This question is related to this other question I recently asked... cf.net exception and
This is related to this question . I'm not an expert on Linux device
This query is related to this one I asked yesterday. I have a radio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.