eat python 002
ĿǰΪֹ£¬¾ÝÎÒËùÖª£¬ÔÚpythonÖжÔÏó³Ö¾Ã»¯ÓÐÒÔϼ¸ÖÖ·½·¨£º
1. ʹÓÃ(dbhash/bsddb, dbm, gdbm, dumbdbm µÈ£©ÒÔ¼°ËüÃǵÄ"¹ÜÀíÆ÷"( anydbm )¡£Ö»ÌṩÁË Python ×Ö
·û´®µÄÓÀ¾ÃÐÔ´¢´æ. Ìṩһ¸öÀàËÆ×ÖµäºÍÎļþµÄ¶ÔÏ󣬿ÉÒÔÍê³É×Ö·û´®µÄÓÀ¾ÃÐÔ´æ´¢¡£
2. ʹÓÃmarshalºÍpickleÀ´ÐòÁл¯python¶ÔÏ󣬲¢¾ß±¸´æ´¢µ½½éÖÊÉϵŦÄÜ¡£Á½ÕßµÄÇø±ðÔÚÓÚ£ºmarshalÖ»ÄÜ´¦Àí¼òµ¥µÄPython¶ÔÏ󣬰üÀ¨Êý×Ö¡¢ÐòÁС¢Ó³Éä¡¢ÒÔ¼°´úÂë¶ÔÏ󣻶øpickle»¹¿ÉÒÔ´¦ÀíµÝ¹é¶ÔÏ󣬱»²»Í¬µØ·½¶à´ÎÒýÓõĶÔÏó£¬ÒÔ¼°Óû§¶¨ÒåµÄÀàºÍʵÀý¡£ÆäÖУ¬pickleÓÐÒ»¸öCÓïÑÔʵÏֵİ汾——cPickle£¬¾ßÓиü¸ßµÄЧÂÊ£¬½¨ÒéʹÓÃcPickle¡£
3. ËäÈ»pickleÌṩ·Ç³£Ç¿´óµÄ¹¦ÄÜÁË£¬ÒѾ¿ÉÒÔÂú×ãÎÒÃǴ󲿷ֵÄÐèÇóÁË£¬µ«ÊÇ£¬ÈËÀàµÄÐèÇóÊÇÎÞÖ¹¾³µÄ£¬¹âÐòÁл¯²»Ðа¡£¬Ö»Óà pickle ²»Äܽâ¾öÃüÃûºÍ²éÕÒ pickle ÎļþÕâÑùµÄÎÊÌ⣬ҪÊÇ¿ÉÒÔ¶ÔÐòÁл¯µÄ¶ÔÏóÌṩ¹ÜÀí¹¦ÄÜ£¬Ö§³Ö²¢·¢·ÃÎʾͺÃÁË¡£Òò´Ë£¬ÈËÃÇ·¢Ã÷ÁËshelveÄ£¿é£¬ËüÊÇÇ°Á½ÕßµÄ×ۺϡ£shelveÄ£¿éʹÓà anydbmÄ£¿éÑ°ÕÒºÏÊʵÄDBMÄ£¿é£¬È»ºóʹÓÃcPickleÀ´Íê³É¶ÔÏó´æ´¢×ª»»¹ý³Ì¡£shelveÄ£¿éÔÊÐí¶ÔÊý¾Ý¿âÎļþ½øÐв¢·¢µÄ¶Á·ÃÎÊ£¬µ«²»ÔÊÐí¹²Ïí¶Á/д·ÃÎÊ¡£
4. »¹ÓÐÒ»ÖÖ·½°¸£¬ÊÇÔÚIBMµÄÍøÕ¾ÉÏ¿´µ½µÄ¡£ÄǾÍÊÇZODB[1]ÁË¡£ZODB±È¼òµ¥µÄ pickle Îļþ¸ü½¡×³ÒÔ¼°¸ü¾ßÓпÉÉìËõÐÔ¡£ZODBÊÇÒ»¸öÕë¶ÔPythonµÄZ¶ÔÏóÊý¾Ý¿â£¬ÊÇÒ»¸ö½¡×³µÄ¡¢¶àÓû§µÄºÍÃæÏò¶ÔÏóµÄÊý¾Ý¿âϵͳ£¬ËüÄܹ»´æ´¢ºÍ¹ÜÀíÈÎÒ⸴Ô Python ¶ÔÏ󣬲¢Ö§³ÖÊÂÎñ²Ù×÷ºÍ²¢·¢¿ØÖÆ¡£Æäʵ£¬ZODBÒ²ÒÀ¿¿Python µÄ±¾»úÐòÁл¯ÄÜÁ¦£¬¶øÇÒÒªÓÐЧµØʹÓà ZODB£¬±ØÐë³ä·ÖÁ˽â pickle¡£
5. ×îºóÒ»Ö»ÖªµÀµÄ½â¾ö³Ö¾ÃÐÔÎÊÌâµÄ·½·¨ÊÇ Prevayler£¬PyPerSyst ½«Õû¸ö¶ÔÏóϵͳ±£´æÔÚÄÚ´æÖУ¬²¢Í¨¹ý²»Ê±µØ½«ÏµÍ³¿ìÕÕ pickle µ½´ÅÅÌÒÔ¼°Î¬»¤Ò»¸öÃüÁîÈÕÖ¾£¨Í¨¹ý´ËÈÕÖ¾¿ÉÒÔÖØÐÂÓ¦ÓÃ×îеĿìÕÕ£©À´ÌṩÔÖÄѻָ´¡£ËùÒÔ£¬¾¡¹ÜʹÓà PyPerSyst µÄÓ¦ÓóÌÐòÊܵ½¿ÉÓÃÄÚ´æµÄÏÞÖÆ£¬µ«ºÃ´¦ÊDZ¾»ú¶ÔÏóϵͳ¿ÉÒÔÍêȫװÈëµ½ÄÚ´æÖУ¬Òò¶øËٶȼ«¿ì£¬¶øÇÒʵÏÖÆðÀ´Òª±ÈÈç ZODB ÕâÑùµÄÊý¾Ý¿â¼òµ¥£¬ZODB ÔÊÐí¶ÔÏóµÄÊýÄ¿±ÈͬʱÔÚÄÜÄÚ´æÖÐËù±£³ÖµÄ¶ÔÏóÒª¶à¡£
±³¾°ÖªÊ¶£ºËü×î³õÊÇÓà Java ʵÏֵġ£ºóÀ´£¬Ò»Èº Python ³ÌÐòÔ±½« Prevayler ÒÆÖ²µ½ÁË Python ÉÏ£¬ÁíÆðÃûΪ PyPerSyst£¬ÓÉ SourceForge Íйܡ£Prevayler/PyPerSyst ¸ÅÄîÒ²Êǽ¨Á¢ÔÚ Java ºÍ Python ÓïÑԵı¾»úÐòÁл¯ÄÜÁ¦Ö®ÉÏ¡£
[1]: http://www.zope.org/Documentation/Articles/ZODB1
Ïà¹ØÎĵµ£º
import os
import unittest # °üº¬µ¥Ôª²âÊÔÄ£¿é
import sqlite3 as sqlite # °üº¬sqlite3Ä£¿é
def get_db_path():
return "sqlite_testdb"
class TransactionTests(unittest.TestCase): # µ¥Ôª²âÊÔµÚÒ»²½:¡¡ÓÉTestCaseÅÉÉúÀà
def setUp(self): # µ¥Ôª²âÊÔ»·¾³ÅäÖÃ
......
ÔÚÏîÄ¿ÀïÃæÒ»¸ö½âÎöÎı¾µÄ¹¤¾ßÀïÃæÓõ½ÁËÕâ¸öÃüÁîÀ´¸³Öµ£¬¿ªÊ¼Ò»Ö±ÖªµÀÒâ˼£¬ºÇºÇ ²éÁËÏ£¬ÕÒµ½·½·¨ÈçÏ£º
exec
Óï¾äÓÃÀ´Ö´Ðд¢´æÔÚ×Ö·û´®»òÎļþÖеÄPythonÓï¾ä¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚÔËÐÐʱÉú³ÉÒ»¸ö°üº¬Python´úÂëµÄ×Ö·û´®£¬È»ºóʹÓÃ
exec
Óï¾äÖ´ÐÐÕâЩÓï¾ä¡£ÏÂÃæÊÇÒ»¸ö¼òµ¥µÄÀý×Ó¡£
> ......
ÔÚPythonÖпÉÒÔ°Ñproperty¶¯Ì¬µÄ°ó¶¨µÄobjectµÄ¼Ì³ÐÀàÖУ¬²¢ÇÒ¿ÉÒÔ¶¨Òå´øÓвÎÊýµÄgetºÍset·½·¨¡£
±ÈÈ磬ÎÒÃǶ¨ÒåÁËÈ«¾Ö±äÁ¿g£¬È»ºóͨ¹ýÁ½¸ö·½·¨À´´æÈ¡gµÄÄÚÈÝ
def get_g(self):
return g
def set_g(self, _g):
global g
g = _g
¶¨ÒåÒ»¸öobjectµÄ¼Ì³ ......
PythonÖк¯Êý²ÎÊýµÄ´«µÝÊÇͨ¹ý“¸³Öµ”À´´«µÝµÄ¡£µ«ÕâÌõ¹æÔòÖ»»Ø´ðÁ˺¯Êý²ÎÊý´«µÝµÄ“Õ½ÂÔÎÊÌ┣¬²¢Ã»Óлشð“Õ½ÊõÎÊÌ┣¬Ò²¾Í˵ûÓлشðÔõô¸³ÖµµÄÎÊÌâ¡£º¯Êý²ÎÊýµÄʹÓÿÉÒÔ·ÖΪÁ½¸ö·½Ã棬һÊǺ¯Êý²ÎÊýÈçºÎ¶¨Ò壬¶þÊǺ¯ÊýÔÚµ÷ÓÃʱµÄ²ÎÊýÈçºÎ½âÎöµÄ¡£¶øºóÕßÓÖÊÇÓÉÇ°Õß¾ö¶¨µÄ¡£º¯Êý²ÎÊýµÄ¶¨ ......
#!/Library/Frameworks/Python.framework/Versions/2.5/bin/python
# encoding: utf-8
import sys, time
import thread
SLEEP_TIME = 0.0001
def run_benchmark(n, m):
# print(">> Python 2.5.1, stackless 3.1b3 here (N=%d, M=%d)!\n" % (n, m))
lock ......