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
相关文档:
计划这个星期学习用python实现决策树算法。今晚就碰到了好多问题,好久没有用python了,并且3.0和书本上的有些东西不太一致,这里记录下几个地方。
1) import module 如果不是在标准目录下(系统的path,python的目录,当前目录),那么需要先import sys,然后sys.path.append('');
2) 改变源文件后,再次import ......
1. SQLite Database Browser 是一个SQLite数据库管理工具。是开源的、免费的。
Home Page
http://sqlitebrowser.sourceforge.net/
Download
http://sourceforge.net/project/showfiles.php?group_id=87946
Wiki
http://en.wikipedia.org/wiki/SQLite_Database_Browser
2.  ......
原文: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 ......
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 ......