Python 7th Day

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 7th Day相关的知识,希望对你有一定的参考价值。

subprocess

The subprocess module allows us to:

  1. spawn new processes
  2. connect to their input/output/error pipes
  3. obtain their return codes

To run UNIX commands we need to create a subprocess that runs the command. The recommended approach to invoking subprocesses is to use the convenience functions for all use cases they can handle. Or we can use the underlying Popen interface can be used directly.

subprocess.call()

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
>>> import os
>>> os.chdir(/)
>>> import subprocess
>>> subprocess.call([ls,-l])
total 181
drwxr-xr-x    2 root root  4096 Mar  3  2012 bin
drwxr-xr-x    4 root root  1024 Oct 26  2012 boot

Setting the shell argument to a true value causes subprocess to spawn an intermediate shell process, and tell it to run the command. In other words, using an intermediate shell means that variables, glob patterns, and other special shell features in the command string are processed before the command is run.

Here, in the example, $HOME was processed before the echo command. Actually, this is the case of command with shell expansion while the command ls -l considered as a simple command.

subprocess.check_call()

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

The check_call() function works like call() except that the exit code is checked, and if it indicates an error happened then a CalledProcessError exception is raised.

subprocess.check_output()

subprocess.check_output(args, *, stdin=None, stderr=None, 
                                 shell=False, universal_newlines=False)

The standard input and output channels for the process started by call() are bound to the parent‘s input and output. That means the calling program cannot capture the output of the command. To capture the output, we can use check_output() for later processing.

>>> import subprocess
>>> output = subprocess.check_output([ls,-l])
>>> print output
total 181
drwxr-xr-x    2 root root  4096 Mar  3  2012 bin
drwxr-xr-x    4 root root  1024 Oct 26  2012 boot
...
>>> output = subprocess.check_output([echo,$HOME], shell=True)
>>> print output
/user/khong


subprocess.call() & subprocess.check_call() & subprocess.check_output() are convenience functions, the underlying process creation and management in this module is handled by the Popen class.

面向对象

面向对象编程(Object Oriented Programming),简称OOP,是一种程序设计思想。OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数。 

我们以一个例子来说明面向过程和面向对象在程序流程上的不同之处。

假设我们要处理学生的成绩表,为了表示一个学生的成绩,面向过程的程序可以用一个dict表示:

stu1 = { name: Michael, score: 98 }
stu2 = { name: Bob, score: 81 }

而处理学生成绩可以通过函数实现,比如打印学生的成绩:

def print_score(stu):
    print(%s: %s % (std[name], std[score]))

如果采用面向对象的程序设计思想,我们首选思考的不是程序的执行流程,而是Student这种数据类型应该被视为一个对象,这个对象拥有name和score这两个属性(Property)。如果要打印一个学生的成绩,首先必须创建出这个学生对应的对象,然后,给对象发一个print_score消息,让对象自己把自己的数据打印出来。

class Student(object):

    def __init__(self, name, score):
        self.name = name
        self.score = score

    def print_score(self):
        print(%s: %s % (self.name, self.score))

面向对象的设计思想是抽象出Class,根据Class创建Instance。面向对象的抽象程度又比函数要高,因为一个Class既包含数据,又包含操作数据的方法。

仍以Student类为例,在Python中,定义类是通过class关键字:

class Student(object):
    pass

class 后面紧接着是类名,即 Student类名通常是大写开头的单词,紧接着是 (object),表示该类是从哪个类继承下来的,object 为父类,通常,如果没有合适的继承类,就使用 object 类,这是所有类最终都会继承的类

由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去。通过定义一个特殊的 __init__ 方法,在创建实例的时候,就把 name,score 等属性绑上去:

class Student(object):

    def __init__(self, name, score):
        self.name = name
        self.score = score

和普通的函数相比,在类中定义的函数只有一点不同,就是第一个参数永远是实例变量 self,并且,调用时,不用传递该参数。除此之外,类的方法和普通函数没有什么区别,所以,你仍然可以用默认参数、可变参数、关键字参数和命名关键字参数。

 

以上是关于Python 7th Day的主要内容,如果未能解决你的问题,请参考以下文章

团队作业4——第一次项目冲刺(Alpha版本)7th day

Python-Basis-7th-Ubuntu

day06-jsp

常用python日期日志获取内容循环的代码片段

Homework (7th,Mar.)

python 有用的Python代码片段