怎么把下面的Python代码封装成函数

Python037

怎么把下面的Python代码封装成函数,第1张

Python:常用函数封装:

def is_chinese(uchar):

"""判断一个unicode是否是汉字"""

if uchar >= u'\u4e00' and uchar<=u'\u9fa5':

return True

else:

return False

def is_number(uchar):

"""判断一个unicode是否是数字"""

if uchar >= u'\u0030' and uchar<=u'\u0039':

return True

else:

return False

def is_alphabet(uchar):

"""判断一个unicode是否是英文字母"""

if (uchar >= u'\u0041' and uchar<=u'\u005a') or (uchar >= u'\u0061' and uchar<=u'\u007a'):

return True

else:

return False

def is_other(uchar):

"""判断是否非汉字,数字和英文字符"""

if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):

return True

else:

return False

def B2Q(uchar):

"""半角全角"""

inside_code=ord(uchar)

if inside_code<0x0020 or inside_code>0x7e: #不是半角字符就返回原来的字符

return uchar

if inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0

inside_code=0x3000

else:

inside_code+=0xfee0

return unichr(inside_code)

def Q2B(uchar):

"""全角转半角"""

inside_code=ord(uchar)

if inside_code==0x3000:

inside_code=0x0020

else:

inside_code-=0xfee0

if inside_code<0x0020 or inside_code>0x7e: #转完之后不是半角字符返回原来的字符

return uchar

return unichr(inside_code)

def stringQ2B(ustring):

"""把字符串全角转半角"""

return "".join([Q2B(uchar) for uchar in ustring])

def uniform(ustring):

"""格式化字符串,完成全角转半角,大写转小写的工作"""

return stringQ2B(ustring).lower()

def string2List(ustring):

"""将ustring按照中文,字母,数字分开"""

retList=[]

utmp=[]

for uchar in ustring:

if is_other(uchar):

if len(utmp)==0:

continue

else:

retList.append("".join(utmp))

utmp=[]

else:

utmp.append(uchar)

if len(utmp)!=0:

retList.append("".join(utmp))

return retList

如何使用Python和Nose实现自动化测试

本文我将详细介绍使用Appium下的Python编写的测试的例子代码对一个iOS的样例应用进行测试所涉及的各个步骤,而对Android应用进行测试所需的步骤与此非常类似。

然后按照安装指南,在你的机器上安装好Appium。

我还需要安装Appium的所有依赖并对样例apps进行编译。在Appium的工作目录下运行下列命令即可完成此任务:

$ ./reset.sh --ios

编译完成后,就可以运行下面的命令启动Appium了:

$ grunt appium

现在,Appium已经运行起来了,然后就切换当前目录到sample-code/examples/python。接着使用pip命令安装所有依赖库(如果不是在虚拟环境virtualenv之下,你就需要使用sudo命令):

$ pip install -r requirements.txt

接下来运行样例测试:

$ nosetests simple.py

既然安装完所需软件并运行了测试代码,大致了解了Appium的工作过程,现在让我们进一步详细看看刚才运行的样例测试代码。该测试先是启动了样例应用,然后在几个输入框中填写了一些内容,最后对运行结果和所期望的结果进行了比对。首先,我们创建了测试类及其setUp方法:

classTestSequenceFunctions(unittest.TestCase):

defsetUp(self):

app=os.path.join(os.path.dirname(__file__),

'../../apps/TestApp/build/Release-iphonesimulator',

'TestApp.app')

app=os.path.abspath(app)

self.driver=webdriver.Remote(

command_executor='127.0.0.1:4723/wd/hub',

desired_capabilities={

'browserName':'iOS',

'platform':'Mac',

'version':'6.0',

'app': app

})

self._values=[]

“desired_capabilities”参数用来指定运行平台(iOS 6.0)以及我们想测试的应用。接下来我们还添加了一个tearDown方法,在每个测试完成后发送了退出命令:

deftearDown(self):

self.driver.quit()

最后,我们定义了用于填写form的辅助方法和主测试方法:

def_populate(self):

# populate text fields with two random number

elems=self.driver.find_elements_by_tag_name('textField')

foreleminelems:

rndNum=randint(0,10)

elem.send_keys(rndNum)

self._values.append(rndNum)

deftest_ui_computation(self):

# populate text fields with values

self._populate()

# trigger computation by using the button

buttons=self.driver.find_elements_by_tag_name("button")

buttons[0].click()

# is sum equal ?

texts=self.driver.find_elements_by_tag_name("staticText")

self.assertEqual(int(texts[0].text),self._values[0]+self._values[1])

本文介绍到此,相信很多朋友都已经明白了。但是如果你对Nose和Python来运行Appium测试有任何问题或看法,可以给我留言,我们可以继续交流。