Other features of the Python Language (chapter fourth: infrequently used features)

Source: Internet
Author: User

How to use the lambda and filter functions

How to use map to avoid loops

String substitution

Getopt Module

4.1 Lambda and filter: simple anonymous functions

4.2Map Short Circuit cycle

4.3 Making decisions in a list--list parsing

4.4 Generating iterators for loops

4.5 replacing a special string with a dictionary

4.6 Important Modules

4.6.1getopt getting options from the command line

The most common way to specify how a program runs in a UNIX system is to add parameters to the program's command line. For example, you might have seen the ability to run many programs so that they can provide basic information about how they run. Implemented using-h in Python

These options were previously specified differently on different UNIX platforms, which largely led to the adoption of two options for most of the projects: simple form and long form.

The user two options are very meaningful, ideally, we would like to provide a simple and long command form, and allow the arbitrary designation of one of them, so if you want to write a user can develop a configuration file program, you may want to only experienced users to provide a simple option of-C, There are also long options such as-config-file, which in either case want them to have the same functionality in the program saved time, while allowing the user to freely use these options.

Getopt module in order to be the Convention of this standard easy-to-use provides two functions: Getopt.getopt and getopt.gnugetopt, they are basically the same, basic getopt has been running until the first non-option encountered, the rest is not checked.

To make getopt more useful, you must know what options you want the user to use, and typically write a city that provides users with information about how to run the program, just like the python-H print message.

It is often useful to have a configuration file that takes these ideas as a starting point, and you can start writing new programs so that both-H and--help produce the least amount of information about how the program is used.

When you use the long option and require a parameter (like the--config in its example), you must use the Equals Sign connection option and the value of the parameter. However, when you use the simple option, you can split the option and its corresponding value with one or more spaces or tabs.

The preceding code fragment, if run in a program that uses the parameter-C test-h-config=secondtest, produces the following output:

Note the second instance of the configuration file is accepted by default, and when he appears, re-accesses the same code that sets the config file so that the second instance can be enabled.

The second list is an empty list, because all the options available to the program on the command line are valid options, or if the options have valid parameters, if you insert other strings in the middle of the option, the standard getopt will behave differently if the parameters are replaced

4.6.2 use more than one process

In Unix and Unix-like operating systems, the primary way to perform some type of subtask is to run a new program that has established a new process. In Unix systems, this is done using a system call, in Python, The os.fork can be used for system calls, and the computer will copy the current information into the newly created process, except that the return value of Os.fork in the newly created process (subprocess) is 0, and this return value is the process ID (PID) of the newly created process in the original process (parent process).

The parent process can wait for an event while the child process is running, and vice versa, the code is simple and practical, but only works on Nuix and Unix-like systems.

One of the most common things after calling Os.fork is to call Os.execl immediately to run other programs. OS.EXECL is a new program that replaces the instruction that is running the program, so it terminates the calling program, and a new program appears in his place

The Os.wait function notifies PYT to let the parent process do nothing.

4.6.3 threads-Accomplish multiple tasks in the same process

Using fork and spawn to create a new process can sometimes take a lot of cost and not enough benefits, and the fork is that when sometimes it takes a lot of work at the same time, some work may have to wait for other work to finish, and when that happens, You might want all the different components to pass them to other parts of the program.

Because child processes are originally created with data from the parent process, they share a lot of information, but they are completely different entities, so it takes a lot of skill to make two processes work together.

Threading concepts are used in order to cope with the complexities of not applying sub-processes.

When executing a program, many threads are more convenient than using a separate process, and note that the following example uses subclasses.

In many cases, such as a Web server or a graphical user interface, threads have a more significant meaning because they only require programmers to do very little work and require little system resources.

Other features of the Python Language (chapter fourth: infrequently used features)

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.