随笔分类 - python
python相关文章
摘要:在网上找了一堆,也看了很多,发现暂时只有这个符合自己的期望,原地址:http://blog.csdn.net/kbkiss_1/article/details/5786718# !usr/bin/python # coding: utf-8# parsexml.py # 本例子参考自python联机文档,做了适当改动和添加 import xml.parsers.expat # 控制打印缩进 level = 0 # 获取某节点名称及属性值集合 def start_element(name, attrs): global level ...
阅读全文
摘要:学习python也有一段时间,当初学的时候就是因为找不到什么有效方法来实现一个通用的协议测试工具.实际场景如下:针对服务器与客户端通讯的私有协议(一般为二进制流),通常彼此的调试需要花费好些时间,且两方面都需要有专人参与,费时费力,当时就设想,能否做一个通用工具,通过xml配置协议结构,初始值,进行自动测试呢.原先有同事进行过这方面的探索,用C++实现了一个协议测试工具,感觉工作量比较大,且通用性比较差,将其用到另一个私有协议,需要改动太多的代码.当时就在考虑用C++编写是否合适,后来在网上搜索,发现python开发效率很高,且没有类型申明的麻烦,最重要的是可以边解释,边执行,当时就在想,是否
阅读全文
摘要:今天随意检查一下wing ide更新,发现又出新版本了4.1.7,下来后用4.1.6的破解文件abstract.pyo替换之,发现破解成功,高兴之余,看到source assistant居然还是乱码,想着白升级了,然后到网上找方法,居然找着了。网址如下:http://blog.csdn.net/hairui/article/details/5443356将“C:/Program Files/Wing IDE 4.1/bin/gtk-bin/etc/pango>” 目录中的 pango.aliases 文件改成courier = "yahei consolas hybrid&qu
阅读全文
摘要:正则表达式比较复杂,前段时间花了一些时间研究,但一直没能与实际工作结合起来,这几天修改配置文件,因牵涉的文件极其多,逐个修改工作量很大,便考虑写个工具,以后再改就方便了很多,结果就被正则表达式给难住了,原来[]符号在正则中是有特别用处的,一时疏忽,结果搞了好久。 解决的问题如下:程序搜索某个目录下的配置文件,找到后将配置文件的某个session下的列表替换掉。 打算分三步走 1、建立函数repla...
阅读全文
摘要:迭代测试边界条件Testing corner cases by iterationWhile developing code, new corner case inputs are often discovered. Being able to capturethese inputs in an iterable array makes it easy to add related test methods.1. Create a new file called recipe10.py in which to put all our code for this recipe.2. Pick a
阅读全文
摘要:测试边界条件Testing the edgesWhen we write automated tests, we pick the inputs and assert the expected outputs. It isimportant to test the limits of the inputs to make sure our code can handle good and badinputs. This is also known as testing corner cases.1. Create a new file named recipe9.py in which to
阅读全文
摘要:将晦涩难懂的测试分解成简单的测试本篇的目的是解说晦涩的测试不易一目了然,然后用一个例子将晦涩的测试用例化为简单的测试用例,使得测试结果一目了然。Breaking down obscure tests into simple ones.Unittest provides the means to test the code through a series of assertions. I have often feltthe temptation to exercise many aspects of a particular piece of code within a single tes
阅读全文
摘要:重组旧的测试代码,使用场景,之前有一段未使用unittest的测试代码,通过另一个包装文件使其运作起来,日常生活中极少用到。Retooling old test code to run inside unittest.Sometimes, we may have developed demo code to exercise our system. We don't have torewrite it to run it inside unittest. Instead, it is easy to hook it up to the test framework and runit
阅读全文
摘要:在测试模块中定义测试套件Defining test suites inside the test module.Each test module can provide one or more methods that define a different test suite. Onemethod can exercise all the tests in a given module; another method can define a particularsubset.1. Create a new file called recipe6.py in which to put our
阅读全文
摘要:执行多个测试套件(suites)Chaining together a suite of testsUnittest makes it easy to chain together test cases into a TestSuite. A TestSuite can be runjust like a TestCase, but it also provides additional functionality to add a single test, multipletests, and count them.1. Create a new file named recipe5.py
阅读全文
摘要:根据不同的命令行参数进行不同代码的单元测试。Running a subset of test case methodsSometimes it's convenient to run only a subset of test methods in a given test case. This recipewill show how to run either the whole test case, or pick a subset from the command line.1. Create a new file named recipe4.py in which to put
阅读全文
摘要:Running test cases from the command line with increased verbosity.It is easy to adjust the test runner to print out every test method as it is run.1. Create a new file called recipe3.py in which to store this recipe's code.2. Pick a class to test. In this case, we will use our Roman numeral conv
阅读全文
摘要:Setting up and tearing down a test harness.With the following steps, we will setup and teardown a test harness for each test method.1. Create a new file called recipe2.py in which to put all our code for this recipe.2. Pick a class to test. In this case, we will use a slightly altered version of our
阅读全文
摘要:一个非常简单的python的测试用例,取自电子书:《Python Testing Cookbook》第一章内容,里面说的足够简单明了,将之摘出来,对照代码,应该很容易理解。 The basic concept of an automated unittest test case is to instantiate part of our code, subject it to operations...
阅读全文
摘要:网上看到的计算器程式,再加上一些对于=号的处理,就完美了,例如输入:3+6*8敲回车,立刻就打印出51,如果输入3+6*8=则出错,这儿需处理一下. import os while True: dynamic = input('输入计算表达式:') if dynamic != 'cls': try: r...
阅读全文
摘要:python struct模块主要内容来自转自:http://blog.163.com/sywxf@126/blog/static/50671196201011319507710/简单读一个二进制文件:# coding: utf-8import structimport sysbinfile=open('test.bin','rb')buf = binfile.read()unpackIndex = 0a,b,c,d = struct.unpack_from('ifdh', buf, unpackIndex)print a, b, c, dunp
阅读全文
摘要:compile函数允许程序员在运行时刻迅速生成代码对象,然后就可以用exec语句或者内建函数eval 来执行这些对象或者对它们进行求值。一个很重要的观点是:exec和eval者可以执行字符串格式的Python代码。这也是与c++等静态语言最重要的区别。compile的三个参数都是必须的,第一参数代表了要编译的Python代码。第二个字符串,虽然是必须的,但通常被置为空串。最后的参数是个字符串,它用来表明代码对象的类型。有三种可能值:‘eval’ 可求值的表达式,和eval一起使用‘single’ 单一可执行语句,和exec一起使用‘exec’ 可执行语句组,和exec一起使用。1、可求值表达式
阅读全文
摘要:学习python终归要能用到,由于工作中经常牵涉到文件批量重命名,拷贝及指定条件搜索,特别是后者,非常有用。先将递归搜索功能实现之:#encoding=utf-8'''Created on 2012-4-30@author: luhx'''import osnewdir = "" #递归搜索函数 def search(rootdir): #dirlist = [] print rootdir dirlist = os.listdir(rootdir) for item in dirlist: item = rootdir +
阅读全文
摘要:这个还真的没怎么看懂,先上代码,此代码即为函数装饰器的一种用法。回头再来看一遍。#!/usr/bin/env pythonfrom time import ctime, sleepdef tsfunc(func): def wrappedFunc(): print '[%s] %s() called' % ( ctime(), func.__name__) return func() return wrappedFunc@tsfuncdef foo(): passfoo()sleep(4)for i in range(2): ...
阅读全文
摘要:这个函数据说叫做函数发生器,看文档也没看明白,后来在网上看了一个用法,发现实际上相当于return 语句,如下代码:def addlist(alist): for i in alist: yield i + 1取出alist的每一项,然后把i + 1塞进去。然后通过调用取出每一项:alist = [1, 2, 3, 4]for x in addlist(alist): print x,结果为:2 3 4 5这个调用非常清晰,每到yield i + 1就相当于return i + 1,即执行到这儿,此函数就结束了,有了前面基本的认识,对于后面较高级用法有很大的作用。较高...
阅读全文

浙公网安备 33010602011771号