Cute python: Using a state machine

Source: Internet
Author: User
Tags final

State machines are, in theory, the basis for almost everything that is related to computer and programming. From a practical standpoint, state machines can also help solve many common problems (especially for Python programmers). In this article, David Mertz discusses the actual examples of when and how to use the Python encoding state machine.

What is Python?

Please briefly recall the first article in this column that Python is a free advanced interpretive language developed by Guido van Rossum. Its syntax is simple and understandable, and its object-oriented semantics are powerful (but flexible). Python can be widely used and highly portable.

What is a state machine?

An extremely precise description of a state machine is that it is a direction graph, consisting of a set of nodes and a set of corresponding transfer functions. The state machine "runs" by responding to a series of events. Each event is within the control of a transfer function that belongs to the current node, where the scope of the function is a subset of the nodes. The function returns the "next" (perhaps the same) node. At least one of these nodes must be a final state. The state machine stops when it reaches the final state.

But an abstract mathematical description (as I have just given) does not really explain the circumstances in which a state machine can be used to solve a real programming problem. Another strategy is to define a state machine as a mandatory programming language in which nodes are also source lines. From a practical point of view, this definition, despite its accuracy, is as theoretical and impractical as the first description. (for descriptive, functional, or constrained languages, such as Haskell, Scheme, or Prolog, this does not necessarily occur.) )

Let's try to use examples that are more appropriate to the actual tasks around us. Logically, each rule expression is equivalent to a state machine, and the parser for each rule expression implements the state machine. In fact, most programmers don't really take this into account when they write state machines.

In the following example, we will look at the real exploratory definition of the state machine. In general, we have a number of different ways to respond to a limited set of events. In some cases, the response depends only on the event itself. In other cases, however, the appropriate action depends on the previous event.

The state machines discussed in this article are advanced machines designed to demonstrate a programming solution for a class of problems. If it is necessary to discuss programming issues in response to categories of event behavior, your solution is likely to be an explicit state machine.

Text Processing state machine

One programming problem that most likely invokes an explicit state machine involves working with text files. Working with a text file typically involves reading a unit of information, usually called a character or line, and then performing the appropriate action on the cell that you just read. In some cases, this process is "stateless" (that is, each of these cells contains enough information to correctly determine what to do). In other cases, even if the text file is not completely stateless, the data has only a limited context (for example, the operation depends on more information than the line number). However, in other common text processing problems, the input file is extremely "state". The meaning of each piece of data depends on the string (perhaps the string behind it) that precedes it. Reports, mainframe data entry, readable text, programming source files, and other kinds of text files are stateful. A simple example is a line of code that may appear in the Python source file:

MyObject = SomeClass (this, which, other)

This line indicates that if there are just the following lines around the line, some of the content is different:

"""How to use SomeClass:
myObject = SomeClass(this, that, other)
"""

We should know that we are in a "block reference" state to determine that this line of code is part of the comment rather than the Python operation.

When not to use the state machine

When you start the task of writing a processor for any stateful text file, ask yourself what type of entry you want to find in the file. Each type of entry is a candidate for a state. There are several types of these. If the numbers are large or uncertain, the state machine may not be the correct solution. (In this case, some database solutions might be more appropriate.) )

Also, consider whether you need to use a state machine. In many cases, it's better to start with a simpler approach. It may be found that even if a text file is stateful, there is a simple way to read it in chunks (where each piece is a type of input value). In fact, in a single state block, a state machine is necessary only if the transfer between text types requires content-based computing.

The following simple example illustrates the need to use a state machine. Consider the two rules used to divide a column of numbers into pieces. In the first rule, the 0 in the list represents the discontinuity between blocks. In the second rule, gaps between blocks occur when the sum of elements in a block exceeds 100. Because it uses an accumulator variable to determine whether a threshold is reached, you cannot "immediately" see the bounds of the child list. Therefore, the second rule might be more appropriate for a mechanism similar to a state machine.

An example of a slightly state, but a text file that is not suitable for working with a state machine is a Windows-style. ini file. This file includes a section header, a comment, and many assignments. For example:

; set the colorscheme and userlevel
[colorscheme]
background=red
foreground=blue
title=green
[userlevel]
login=2
title=1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.