Process-oriented programming of Python functions

Source: Internet
Author: User

First, explain

Process-oriented: The core is the process of two words, the process is to solve the problem, based on the process to design the program is like in the design, pipeline programming ideas, in the design process, the need to design the entire process, an industrial pipeline, is a mechanical way of thinking

Ii. Advantages and Disadvantages

Advantages: The program architecture is clear, can simplify the complex problems, process

Cons: Poor scalability, a streamline is only used to solve a problem, so that process-oriented applications are often not required to change the software

Application scenario: Linux kernel, Git,httpd,shell script

Iii. examples (process-oriented programming using the co-function)

Filter the contents of sub-files and Word folders under a file, the command in Linux is Grep-rl ' python '/etc

Using Python's Walk () inside the package OS, the ability to open a folder in the path under the parameters and return a tuple

Import os>>> os.walk ('D:\test')        0x0000000002adb3b8>> > Os.walk ('d:\\test')        #  after using this path mode, win under >>> Os.walk (R'D:\test')       #  using R is to let the symbols in the string have no special meaning, for escaping

An error occurred because of the path, but it is possible to have it on a different machine

>>> g=os.walk ('D:\test')>>> Next (g) Traceback (most Recent call last):  '<stdin>' in <module>  Stopiteration

Recommended later is to use \

>>> g=os.walk ('d:\\test')>>> Next (g) (' d:\\test ', ['a','b'], ['test.txt  '])

Returns a tuple , the first element is the path to the file, the second is a folder, and the third is the file under that path

1.2.1 Program Flow
    1. Find File path--os.walk
    2. Then open the file--open
    3. Reads the contents of each line of the file-for lines in F
    4. Filter for Python if ' Python ' in line
    5. Print file paths that contain Python

The program is executed from the top down, 1 of the generated path as a parameter to 2, 2 generated to 3 ...

The resulting result is the following input parameter

1 Find the path to the file

G is a generator that can be executed with next (), and each time next is run, the result is the path of the file opened in turn

>>> G=os.walk ('d:\\test')>>>Next (g) ('d:\\test', ['a','b'], ['Test.txt'])>>>Next (g) ('d:\\test\\a', ['A1'], ['a.txt'])>>>Next (g) ('D:\\TEST\\A\\A1', [], ['A1.txt'])>>>Next (g) ('d:\\test\\b', ['B1'], ['B.txt'])>>>Next (g) ('D:\\TEST\\B\\B1', [], ['B1.txt'])

When we open the file, we need to find the absolute path of the file, and now we can stitch the first and third parts with the method of string concatenation.

To open with a loop:

ImportOS#def serach ():g = Os.walk ('d:\\test') forIinchg:Print(i) Results: ('D:\test', ['a','b'], ['Test.txt'])('D:\test\a', ['A1'], ['a.txt'])('D:\TEST\A\A1', [], ['A1.txt'])('D:\test\b', ['B1'], ['B.txt'])('D:\TEST\B\B1', [], ['B1.txt'])

Stitching the queried files and paths into absolute paths

ImportOS#def serach ():g = Os.walk ('d:\\test') forIinchg:#print (i)     forJinchI[-1]:#The last element is traversed, these are the filesFile_path='%s\\%s'%(I[0],J)Print(File_path) results: D:\test\test.txtd:\test\a\a.txtd:\test\a\a1\a1.txtd:\test\b\b.txtd:\test\b\b1\b1.txt

So we can find out all the absolute paths to the file.

Implemented with functions:

defsearch (): whileTrue:file_name=yield   #loops through the white loop to receiveg = Os.walk (file_name)#here's the parameter.         forIinchg: forJinchI[-1]:#The last element is traversed, these are the filesFile_path='%s\\%s'%(I[0],J)Print(File_path) G=search ()#g is the generator functionNext (g)#InitializeG.send ('d:\\test')#The path is passed through send
2 then open the file

In the writing program, the problem encountered here is with open (File_path) as F:attributeerror: enter, do not understand why, and then think open may be the system has been used, So after modifying the name, the execution succeeds.

@init   #  Initialize generator def  opener (target):    " Open file, action handle " while      True:        file_path=yield  #  receives the path of the search pass with         Open (File_path) as F:            #  send multiple tuples in order to pass the file path down
3 reading the contents of each line of a file
@init def Cat (target):      while True:        file_path,f=yield         for inch F:             # Pass both the file path and the contents of each row
4 filter Whether there is a single line of content
@init def grep (pattern,target):  #  patter is the filtered parameter while      True:        file_ Path,line=yield        if in line :            target.send (File_path)    #  pass a file path with the appropriate content
5 Printing file paths that contain Python
@init def printer ():      while True:        file_path=yield        Print (File_path)

The above is the definition phase of the function, the following is the execution phase of the function:

G=search (Opener (Cat (grep ('python', Printer ()))) G.send (' d:\\test ')

Target This generator:
Opener (Cat (grep (' Python ', Printer ())))

Process-oriented programming of Python functions

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.