1.inspect.statck
Return a list of frame records for the caller’s stack. The first entry in thereturned list represents the caller; the last entry represents the outermostcall on the stack.
最后一个表示当前的最外层调用,包含当前调用的(对象,所在文件路径,具体行数,函数名)
cinder通过获取最外层调用信息,得到其binary ,即cinder-volume
2.str.rpartition(sep)
字符窜最后出现sep处进行分割,返回三元组(before_sep,sep,after_sep)
>>> s = "hihi/yoyo/zeze"
>>> s.rpartition('/')
('hihi/yoyo', '/', 'zeze')
3.%
参考http://developer.51cto.com/art/201003/189039.htm
%s 转成string
整型数:%d 无符号整型数:%u 八进制:%o 十六进制 %x %X 浮点数:%f 科学计数法:%e %E
针对dict类型,可用%(key)type %dict
>>> 'arg is %(arg)s ' %{'arg':20,'name':'whl'}
'arg is 20 '
4.string
string.strip(arg)
string 头尾无掉arg,当arg为None 或者没有给出,则去掉头尾的空格
string.startswith('#') check string start with '#' or not return Bool
string.splitlines() split string by line
5.hashlib
hashlib.md5("/home/cloud/test").hexdigest()
返回32长的字符串
6.path
os.path.dirname() 父目录
os.path.basename() 全路经的文件名称
os.chdir() 改变当前目录
os.path.exists() 是否存在
os.chmod(path,mode) 更改文件权限
7.@property
@property 将函数当作read-only属性访问
>>>class test(object):
def __init__(self):
self.name = "hihi"
@property
def default(self):
return self.name
>>>m = test()
>>>m.default
'hih'
8.进制之间的转换
int表示的是十进制值,而2、8、16进制的表示都是通过字符窜来表述
2进制0b开头,8进制0开头,16进制0x开头
十进制转2进制
(Pdb) bin(509)
'0b111111101'
十进制转8进制
(Pdb) oct(509)
'0775'
十进制转16进制
(Pdb) hex(509)
'0x1fd'
2、8、16进制转回10进制
(Pdb) int('0x1fd',16)
509
(Pdb) int('0775',8)
509
(Pdb) int('0b111111101',2)
509
2进制转16进制,通过十进制转
(Pdb) hex(int('0b111111101',2))
'0x1fd'
9.获取文件权限 参考(http://stackoverflow.com/questions/5337070/how-can-i-get-a-files-permission-mask)
(Pdb) os.stat('/home/cloud/win7').st_mode
#换成8进制
(Pdb) oct(os.stat('/home/cloud/win7').st_mode)
'0100775'
字符窜后3位表示权限775,而前面的部分表示文件类型
S_IFMT 0170000 bitmask for the file type bitfields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file(普通文件)
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
判断文件权限是否为644
(Pdb) os.stat('/home/cloud/win7').st_mode & 0777 == int('644',8)
False
10.sys.excepthook函数进行全局异常的获取。
首先定义异常处理函数,并使用该函数接收系统异常信息。
import sys
def excepthook(ntype,value,trace):
'''write the unhandle exception to log.'''
print 'type is :',ntype
print 'value is :',value
print 'trace is :',trace
if __name__ == "__main__":
sys.excepthook = excepthook
m = 'hello'
print m.newattr
结果
type is : <type 'exceptions.AttributeError'>
value is : 'str' object has no attribute 'newattr'
trace is : <traceback object at 0x7ff410ca46c8>
11.vars
vars获取对象/类/模块的属性 vars() =locals(), vars(object),获取对象属性
12.模块导入
from ** import **
import **
都会执行模块所在的py文件,这样当模块里有print时,则导入模块时显示打印信息
模块第二次被导入,则不会再次执行其所在的py文件
13.判断类型
>>> isinstance(1,(list,tuple,int))
True
>>> isinstance('sdfsdf',(int,list))
False
14.无穷大及无穷小
float('inf') 无穷大
-float('inf') 无穷小
15.类的继承关系
class people(object):
def __init__(self):
self.sex ='man'
def sound(self):
print 'i am ',self.sex
class student(people):
def __init__(self):
self.grade = '3'
def rest(self):
print "do rest"
student 继承people,继承了父类sound函数,但是没有继承sex属性
(Pdb) s1 = student()
(Pdb) dir(s1)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'grade', 'rest', 'sound']
(Pdb) s1.sound()
i am *** AttributeError: 'student' object has no attribute 'sex'
student继承people,继承父类sound函数以及sex属性
class student(people):
def __init__(self):
self.grade = '3'
super(student,self).__init__()
def rest(self):
print "do rest"
(Pdb) s1 = student()
(Pdb) dir(s1)['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__','__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__' ,'__str__', '__subclasshook__'
, '__weakref__', 'grade', 'rest', 'sex', 'sound']
(Pdb) s1.sound()
i am man