1. Python popen: open a command or return a value from the pipeline.
Starting from python2.4, you can use the subprocess module to generate sub-processes and connect them to the standard input, output, and error of sub-processes. You can also obtain the returned values of sub-processes, subprocess is intended to replace several other old modules or functions. For example, OS. System, OS. Spawn *, OS. popen *, popen2. *, commands .*
The following is an example of subprocess.
import win32com.clientfrom subprocess import Popenimport timeshell = win32com.client.Dispatch("WScript.Shell")shell.Run("notepad")time.sleep(5)Popen("taskkill /f /im notepad.exe")Popen("calc")time.sleep(5)Popen("taskkill /f /im calc.exe")
2. python creates a process using OS. the fork function is only available on the POSIX system. In python of windows, the OS module does not define the OS. fork functions, on the contrary, Windows programmers use multi-thread programming technology (multiprocessing) to complete concurrent tasks.
About Python fork:
1) Create an MPS queue 2) create a sub-process
Sub-process:
1) The pipeline read end needs to be closed. 2) Execution starts. 3) write the result to the write end. 4) the process is killed.
Parent process:
1) Close the pipeline write end 2) read data from the read end until the sub-process dies or closes 3) call the waitpid method to ensure that the sub-process has been revoked (if this is not done in FreeBSD, the sub-process will never die) 4) process output
3. Example of map, reduce, and filter.
def map_func(lis): return lis + 1def reduce_func(li, lis): return li + lisdef filter_func(lis): if lis % 2 == 0: return True else: return Falselis = [1, 2, 3, 4, 5]map_I = map(map_func, lis)reduce_I = reduce(reduce_func, lis)filter_I = filter(filter_func, lis)print map_Iprint reduce_Iprint filter_I
Output result:
>>> [2, 3, 4, 5, 6]15[2, 4]>>>
4. Python Bubble sorting methods.
Method 1:
array = [1, 2, 5, 3, 6, 8, 4]for i in range(len(array) - 1, 1, -1): #print i for j in range(0, i): #print j if array[j] > array[j + 1]: array[j], array[j + 1] = array[j + 1], array[j]print array
Method 2:
array1 = [1, 2, 5, 3, 6, 8, 4]array1.sort(cmp = None, key = None, reverse = False)print array1
The result is as follows:
>>> [1, 2, 3, 4, 5, 6, 8][1, 2, 3, 4, 5, 6, 8]>>>
5. Use python to output data.
array = [1, 2, 5, 3, 6, 8, 4]# (0, 1, 2, 3, 4, 5, 6)# (-7,-6,-5,-4,-3,-2,-1)print array[::2]print array[2::]print array[::-1]print array[0:]print array[:-1]print array[3:-3]
The result is as follows:
>>> [1, 5, 6, 4][5, 3, 6, 8, 4][4, 8, 6, 3, 5, 2, 1][1, 2, 5, 3, 6, 8, 4][1, 2, 5, 3, 6, 8][3]>>>
6. the python function can return multiple values, as shown in the following example.
def test(num = 0): return(num, num + 1)def hello(num = 0): if num == 0: pass else: return num + 2num1, num2 = test(1)num4 = hello(1)num3 = hello(0)print num1, num2print num3, num4
The result is as follows:
>>> 1 2None 3>>>
7. when Python traverses dictionary elements, the sequence of dictionary elements is usually not defined. In other words, keys and values in the dictionary are always processed during iteration, but the processing order is uncertain. If the order is important, key values can be saved in a separate list, for example, sorted before iteration.
8. the cmd command "netsh WLAN export profile" allows you to export a Wireless Configuration File Created in the Windows graphical user interface to an XML file, it can be imported to another computer or backed up.
Appendix: several books to learn Python are recommended for future reference.
1. Learning python, O' Reilly, Introduction
2. Pratical python, apress, and some utilities
3. Python standard library, Fredrik lundh, module instance
4. Python cookbook, (2nd) Chinese version, o'reilly, human mail, advanced