Skip to content

Way to use dot notation to refer to states in a state machine #15694

Discussion options

You must be logged in to vote

You can use special methods of dict in Python to implement "dot notation". This simple module shows you how:

# dotdict.py
_B='No such attribute: '
class Dot(dict):
    def __getattr__(B,N):
            if N in B:return B[N]
            else:raise AttributeError(_B+N)
    def __setattr__(B,N,V):
            B[N]=V
    def __delattr__(B,N):
            if N in B:del B[N]
            else:raise AttributeError(_B+N)

You can then use the class as shown below:

Use Ctrl-D to exit, Ctrl-E for paste mode
>>> from dotdict import Dot
>>> state=Dot()
>>> state.idle=1
>>> state.triggered=2
>>> state.lockout=3
>>> current_state=state.idle
>>> current_state
1
>>> state
{'triggered': 2, 'idle': 1, 'lockout'

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@bgoolsby
Comment options

Answer selected by bgoolsby
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants