Object-Oriented Programming-Python applications

Source: Internet
Author: User

Python applications support process-oriented programming and object-oriented programming. In the "process-oriented" language, A program is built by a process or a function that can only reuse code. I hope this article will help you.

The SimPy Library only provides three abstract/parent classes, which correspond to the three basic concepts of simulation. Many other common functions and constants are used to control the running of simulation, but important concepts are combined with these classes. The core concept in the simulation is process. A process is just an object. It completes some tasks and then prepares to complete the next task.

Sometimes wait for a while before. In SimPy, you can also "deactivate" the process, which means that after a process completes a task, it will only do when other processes require the process to complete other tasks. It is often useful to consider a process as an attempt to accomplish a goal. When writing a process, it is usually written as a loop in which multiple operations can be executed.

You can insert a Python application statement between each operation, which allows the simulation scheduler to execute each waiting process operation before the return control. Many operations performed by a process depend on the use of resources. Resources are restricted only in terms of availability.

In the biological model, resources may be food supply; in the network model, resources can be routers or limited bandwidth channels; in our market simulation, resources are payment channels. The only task executed by a resource is to restrict its use to a specific process at any given time.

In the SimPy programming model, a process independently determines how long it will take to retain resources, and the resources itself are passive. In the actual system, the SimPy model may be suitable for conceptual solutions or not; it is easy to imagine that resources will limit their utilization rate in essence, for example, if the server computer does not obtain a satisfactory response within the required time frame, the connection will be interrupted ).

But as a programming problem, it is not particularly important for a process or resource to be "active", as long as you understand your intention ). The last SimPy class is the monitoring program. In fact, the monitoring program is not very important, but it is very convenient. All tasks done by the monitoring program are to record the events reported to it, and save the average, count, and variance statistics about these events ).

The Python application provided by this library is a useful tool for recording simulation measures, but you can also record events using any other technology you want. In fact, my example subclass the Monitor to provide some slightly) enhanced capabilities.
In most of my articles, I will give sample applications right away, but in this example, I think it is more useful to take you through every step of the grocery store application. If you want to, you can cut and paste each part together. The SimPy creators will include my examples in future releases.

Some examples attached to SimPy use the import * style. But I prefer to make the namespace I filled clearer. For the minimum version required by the Python application SimPy), import the generator feature as stated. This is not required for Python Versions later than Python 2.3.
For my applications, I have defined several runtime durations, which describe several schemes that I am interested in during a specific simulated runtime. When I change the scheme, I must edit these constants in the main script. If the content of this application is more enriched.

Then I may use command line options, environment variables, or configuration files to configure these parameters. But for now, this style is enough:

The main task we need to complete is to define one or more processes. For a simulated grocery store, we are interested in the Process of paying customers at the Channel.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.