Python sqlite3和单元测试
import os
import unittest # 包含单元测试模块
import sqlite3 as sqlite # 包含sqlite3模块
def get_db_path():
return "sqlite_testdb"
class TransactionTests(unittest.TestCase): # 单元测试第一步: 由TestCase派生类
def setUp(self): # 单元测试环境配置
try:
os.remove(get_db_path())
except:
pass
self.con1 = sqlite.connect(get_db_path(), timeout=0.1) # 连接数据库
self.cur1 = self.con1.cursor() # 获取游标
self.con2 = sqlite.connect(get_db_path(), timeout=0.1)
self.cur2 = self.con2.cursor()
def tearDown(self): # 单元测试环境清除
self.cur1.close() # 关闭游标
self.con1.close() # 关闭连接
self.cur2.close()
self.con2.close()
os.unlink(get_db_path())
def CheckDMLdoesAutoCommitBefore(self):
self.cur1.execute("create table test(i)") # 执行SQL查询
self.cur1.execute("insert into test(i) values (5)")
self.cur1.execute("create table test2(j)")
self.cur2.execute("select i from test")
res = self.cur2.fetchall()
self.failUnlessEqual(len(res), 1) # 测试
def CheckInsertStartsTransaction(self):
self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)")
self.cur2.execute("select i from test")
res = self.cur2.fetchall()
self.failUnlessEqual(len(res), 0)
def CheckUpdateStartsTransaction(self):
self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)")
self.con1.commit()
self.cur1.execute("update test set i=6")
self.cur2.execute("select i from test")
res = self.cur2.fetchone()[0]
self.failUnlessEqual(res, 5)
def CheckDeleteStartsTransaction(self):
self.cur1.exec
相关文档:
from cmd import *
class MyShell(Cmd):
def preloop(self):
print "print this line before entering the loop"
def postloop(self):
&nb ......
原文:http://www.klipdas.com/blog/?p=python-decorator
python装饰器介绍
Python 2.2中引入的 classmethod() 和 staticmethod() 内置函数,你可以这样调用classmethod():
class A:
def foo(self, y):
print y
foo = classmethod(foo)
也可以这样:
class A:
@classmethod
def foo(sel ......
python中国论坛 http://www.okpython.com/bbs/index.php
开源社区 http://sourceforge.net/
python官网 http://www.python.org/
pythonIDE BOAhttp://boa-constructor.sourceforge.net/
pythonIDE 大全http://www.oschina.net/project/tag/120
python组件http://py.dw ......
SQLite 作为一个轻量级嵌入式数据库,还是非常好用的。雨痕极力推荐~~~~~~
今天有个朋友测试 SQLite,然后得出的结论是:SQLite 效率太低,批量插入1000条记录,居然耗时 2 分钟!
下面是他发给我的测试代码。我晕~~~~~~
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
......
You are here: Home ‣ Dive Into Python 3 ‣
Difficulty level: ♦♢♢♢♢
Installing Python 安装Python
❝ Tempora mutantur nos et mutamur in illis. (Times change, and we change with them.) ❞
— ancient Roman proverb
D ......