用Python做测试——测试人员需要掌握的Python基础(二)

来源:互联网 发布:网络平台运行 编辑:程序博客网 时间:2024/05/17 23:59

f、函数

定义函数

def sayHello():

    print 'Hello World!' # block belonging to the function

 

sayHello() # call the function

 

使用函数形参

def printMax(a, b):

    if a > b:

        print a, 'is maximum'

    else:

        print b, 'is maximum'

 

printMax(3, 4) # directly give literal values

x = 5

y = 7

printMax(x, y) # give variables as arguments

 

默认参数值

def say(message, times = 1):
    print message * times

say('Hello')
say('World', 5)

默认参数只能在后面,不能在形参前面

def func(a, b=5, c=10):
    print 'a is', a, 'and b is', b, 'and c is', c

func(3, 7)
func(25, c=24)


Return

def maximum(x, y):

    if x > y:

        return x

    else:

        return y

print maximum(2, 3)

g、模块

利用import语句 输入 sys模

import sys

 

print 'The command line arguments are:'

for i in sys.argv:

    print i

print '\n\nThe PYTHONPATH is', sys.path, '\n'

 

如何导入自定义模块?

def sayhi():

    print 'Hi, this is mymodule speaking.'

 

version = '0.1'

---------------------------------------------

import mymodule

 

mymodule.sayhi()

print 'Version', mymodule.version

h、数据结构

List(序列)

list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个 序列 的项目

一旦你创建了一个列表,你可以添加、删除或是搜索列表中的项目。

 

由于你可以增加或删除项目,列表是 可变的数据类型,即这种类型是可以被改变的。

shoplist = ['apple', 'mango', 'carrot', 'banana']

 

print 'I have', len(shoplist),'items to purchase.'

 

print 'These items are:', # Notice the comma at end of the line

for item in shoplist:

    print item,

 

print '\nI also have to buy rice.'

shoplist.append('rice')

print 'My shopping list is now', shoplist

 

print 'I will sort my list now'

shoplist.sort()

print 'Sorted shopping list is', shoplist

 

print 'The first item I will buy is', shoplist[0]

olditem = shoplist[0]

del shoplist[0]

print 'I bought the', olditem

print 'My shopping list is now', shoplist

 

tuple(元组)

元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组

zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)

new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]

 

元组最通常的用法是用在打印语句中

age = 22

name = 'Swaroop'

 

print '%s is %d years old' % (name, age)

print 'Why is %s playing with that python?' % name

 

dict(字典)

键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。

 

记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。

ab = {       'Swaroop'   : 'swaroopch@byteofpython.info',

             'Larry'     : 'larry@wall.org',

             'Matsumoto' : 'matz@ruby-lang.org',

             'Spammer'   : 'spammer@hotmail.com'

     }

 

print "Swaroop's address is %s" % ab['Swaroop']

 

# Adding a key/value pair

ab['Guido'] = 'guido@python.org'

 

# Deleting a key/value pair

del ab['Spammer']

 

print '\nThere are %d contacts in the address-book\n' % len(ab)

for name, address in ab.items():

    print 'Contact %s at %s' % (name, address)

 

if 'Guido' in ab: # OR ab.has_key('Guido')

print "\nGuido's address is %s" % ab['Guido']

 

序列切片

shoplist = ['apple', 'mango', 'carrot', 'banana']

# Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2]

# Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]

# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]

 

组合使用

 

接口测试,主要返回的数据类型为以下两种:

字典中+list

List+字典

 

i、使用文件

poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
        use Python!
'''

f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file

f = file('poem.txt')
# if no mode is specified, 'r'ead mode is assumed by default
while True:
    line = f.readline()
    if len(line) == 0: # Zero length indicates EOF
        break
    print line,
    # Notice comma to avoid automatic newline added by Python
f.close() # close the file

 

j、try..except

import sys

 

try:

    s = raw_input('Enter something --> ')

except EOFError:

    print '\nWhy did you do an EOF on me?'

    sys.exit() # exit the program

except:

    print '\nSome error/exception occurred.'

    # here, we are not exiting the program

finally:

print 'Done'

0 0