WEB开发网
开发学院软件开发Python 从脚本编写到面向对象的 Python 编程 阅读

从脚本编写到面向对象的 Python 编程

 2008-09-23 12:46:56 来源:WEB开发网   
核心提示: 使用继承的面向对象 Python 磁盘监视脚本#!/usr/bin/env pythonfrom subprocess import Popen, PIPEimport reclass DiskMonitor():"""Disk Monitoring Clas

使用继承的面向对象 Python 磁盘监视脚本

#!/usr/bin/env python
from subprocess import Popen, PIPE
import re
class DiskMonitor():
  """Disk Monitoring Class"""
  def __init__(self,
        pattern="2[0-9]%",
        message="CAPACITY WARNING",
        cmd = "df -h"):
    self.pattern = pattern
    self.message = message
    self.cmd = cmd
  def disk_space(self):
    """Disk space capacity flag method"""
    ps = Popen(self.cmd, shell=True,stdout=PIPE,stderr=PIPE)
    output_lines = ps.stdout.readlines()
    for line in output_lines:
      line = line.strip()
      if re.search(self.pattern,line):
        print "%s %s" % (self.message,line)
class MyDiskMonitor(DiskMonitor):
  """Customized Disk Monitoring Class"""
  def disk_space(self):
    ps = Popen(self.cmd, shell=True,stdout=PIPE,stderr=PIPE)
    print "RAW DISK REPORT:"
    print ps.stdout.read()
if __name__ == "__main__":
  d = MyDiskMonitor()
  d.disk_space()          

如果运行这个使用继承的脚本版本,您将获得以下输出:RAW DISK REPORT:
Filesystem      Size Used Avail Use% Mounted on
/dev/sda1       3.8G 694M 2.9G 20% /
varrun        252M  48K 252M  1% /var/run
varlock        252M   0 252M  0% /var/lock
udev         252M  52K 252M  1% /dev
devshm        252M   0 252M  0% /dev/shm

上一页  1 2 3 4 5 6 7  下一页

Tags:脚本 编写 面向

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接