Python magic that allows you to reuse attributes in other classes is the "MyDiskMonitor (DiskMonitor)" statement. You only need to put the name of the previous class in brackets when defining the name of the new class. Once this step is completed, you can immediately access other class attributes to do what you want. But it's not just fun. Add another method to send a tag message via email.
It may be named disk_alert (self), so that you can further customize the new class. This is the beauty of object-oriented design; it allows experienced developers to constantly reuse the code they have written, thus saving a lot of time. Unfortunately, object-oriented programming also has its disadvantages. All these abstractions are at the cost of complexity. If the abstraction is excessive, it may be completely self-defeating.
Because Python supports multiple inheritance, abstraction can lead to harmful complexity. Can you imagine that you only want to view multiple files for writing a method? Believe it or not, this situation does happen and represents the unfortunate reality of object-oriented programming.
An alternative to object-oriented programming is functional programming, and Python provides resources for functional and object-oriented and procedural programming. In the last example, we will look at how to write disk monitoring code that has become boring in a functional way.
-
- from subprocess import Popen, PIPE
- import re
-
- def disk_space(pattern="2[0-9]%", message="CAPACITY WARNING:"):
- #Generator Pipeline To Search For Critical Items
- ps = Popen("df -h", shell=True,stdout=PIPE, stderr=PIPE)
- outline = (line.split() for line in ps.stdout)
- flag = (" ".join(row) for row in outline if re.search(pattern, row[-2]))
- for line in flag:
- print "%s %s" % (message,line)
-
- disk_space()
View the last example, which is very different from all other code you have seen in this article. If you browse the code line by line, you can start with the "ps" variable with the content you have never seen before. The following two lines of code use a generator expression to process the file object ps. stdout.
Analyze the file and search for the row you are searching. If you cut the code lines and paste them into the interactive Python Shell. If printed, you will see that the summary and flag are both generator objects. The generator object comes with the next method, allowing you to connect operations through the pipeline.
The summary line removes new line characters from a line and passes the line to the next generator expression. The latter searches for a regular expression match item in each line at a time, then, the output is passed to the tag. This compact workflow can replace the object-oriented programming style and is quite interesting. However, this style also has shortcomings, because the simplicity of the Code will lead to errors that are difficult to debug.
Unless each line of code is executed independently. Functional Programming is also a headache, because it allows you to consider solving the problem by linking the solution together. This is quite different in terms of process style and object-oriented style.
This article is a bit experimental, because it talked about process and object-oriented from Bash and PHP, and finally talked about functional Python using the same basic code. I hope this article shows that Python is a very flexible and powerful language.
Developers of other programming languages can also learn and appreciate it. As Python becomes more and more popular, other developers will learn Python in addition to the preferred language. The two largest development areas of Python recently are Web development and system management. For Web development, PHP developers may soon have to make weekly choices, that is, which project uses Python is more meaningful.
And which project is more meaningful when PHP is used. For system administrators, Bash, and Perl script programmers, they are often asked to use Python to complete certain projects. This is partly because there is no choice, and partly because many vendors are providing Python APIs for their products. Preparing a bit of Python in your toolbox will never hurt anyone.
- How to embed Python into C ++ applications?
- In-depth discussion of Ruby and Python syntax comparison
- Introduction to Python
- Python Learning Experience: version, IDE selection and encoding solution
- Analysis of Python GIL and thread security