openstack cinder python 小结

本文介绍了OpenStack Cinder中使用Python的一些关键点,包括inspect.stack用于获取调用信息,str.rpartition进行字符串分割,hashlib.md5生成MD5摘要,os.path模块处理文件路径,@property装饰器创建只读属性,以及Python进制转换、异常处理和类型判断等知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值