免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6288 | 回复: 3
打印 上一主题 下一主题

帮忙看下这个python有什么错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-03 09:48 |只看该作者 |倒序浏览
  1. #/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. """
  4. Fulltext Server 网络应用支持插件,用于构造 问答系统的增量索引。
  5. 版权属于(C) 2008 coreseek.com,保留所有权利
  6. 请仔细阅读代码中的注释,如果对Python语言不熟悉,请不要修改本文件
  7. """

  8. import MySQLdb
  9. import unicodedata
  10. import re
  11. import time
  12. import sys
  13. import signal
  14. import os
  15. from os.path import dirname, join, pardir
  16. # unicode convert helper function
  17. def u(s):
  18.         if isinstance(s, unicode):
  19.                 return s;
  20.         else:
  21.                 return unicode(s, g_datasource_encoding , errors='ignore')

  22. #初始化,用于检测当前文件是否被Python解释器直接执行               
  23. bStandAlone = False
  24. if not 'DataSource' in dir():
  25.         # make the define
  26.         class DataSource:
  27.                 pass
  28.         bStandAlone = True

  29. #包含db_conn.py所在的目录
  30. if '__file__' in globals():
  31.         #include parent dir
  32.         sys.path.insert(0, join(dirname(__file__), pardir))

  33. #检测系统参数,当存在该参数时,系统将读取该参数所指的文件作为 配置文件。
  34. if 'argv' in dir(sys) and len(sys.argv) > 1:
  35.         #0 self, 1 argv
  36.         try:
  37.                 execfile (sys.argv[1])
  38.         except:
  39.                 #normal route.
  40.                 from t_db_conn import *
  41. else:
  42.         from t_db_conn import *

  43. class t_Private:
  44.         def __init__(self):
  45.                 self.m_cursor = None
  46.                 self.m_dbconn = None
  47.                 self.m_docId = 0 # the 1st field if wanna to use autoid, return -1. means no id defined
  48.                 #range query.
  49.                 self.m_minid = 0
  50.                 self.m_maxid = 0
  51.                 self.m_start_id = 0;
  52.                 #base str
  53.                 self.m_basesql_str = ''
  54.        
  55.         def Connected(self):
  56.                 try:
  57.                         self.m_dbconn =  MySQLdb.connect (host = g_database_host,
  58.                                                                                           port = g_database_port,
  59.                                                                   user = g_database_username,
  60.                                                                   passwd = g_database_password,
  61.                                                                   db = g_database_dbname)
  62.                 except MySQLdb.Error, e:
  63.                         print "Error %d: %s" % (e.args[0], e.args[1])
  64.                         return False
  65.                 return True
  66.                
  67.         def BeforeIndex(self):
  68.                 #select max & min doc_id
  69.                 if self.m_dbconn == None:
  70.                         return False
  71.                
  72.                 sql = """SELECT MIN(id),MAX(id) FROM {$prefix}contentlist"""
  73.                 sql = sql.replace("{$prefix}",g_table_prefix)
  74.                 try:
  75.                         self.m_cursor = self.m_dbconn.cursor ()
  76.                         rowCount = self.m_cursor.execute (sql)
  77.                 except MySQLdb.Error, e:
  78.                         print "Error %d: %s" % (e.args[0], e.args[1])
  79.                         return False
  80.                 if rowCount == 0:
  81.                         return False
  82.                 tm_row = self.m_cursor.fetchone()
  83.                 #print tm_row[0],tm_row[1]
  84.                 if tm_row[0]:
  85.                         self.m_minid = tm_row[0]
  86.                 if tm_row[1]:
  87.                         self.m_maxid = tm_row[1]
  88.                 #self.m_minid = 4667671
  89.                 self.m_start_id = self.m_minid
  90.                
  91.                 self.m_cursor.close ()
  92.                 self.m_cursor = None
  93.                
  94.                 sql = """SELECT c.id AS id, c.CateID AS cid,
  95.                                         c.AuthorID AS AuthorID, c.Title, c.Description,
  96.                                         c.Author AS author, c.State AS State,
  97.                                         c.PublicTime AS dateline, c.Top as top,
  98.                                         c.pink as pink, cc.content as content,
  99.                                         ccc.HitNum as views,ccc.CommentNum as comments
  100.                                         FROM {$prefix}contentlist AS c
  101.                                         LEFT JOIN  {$prefix}content AS cc ON cc.ContentID = c.id
  102.                                         LEFT JOIN {$prefix}counter AS ccc ON c.id=ccc.ContentID"""
  103.                                        
  104.                 self.m_basesql_str = sql.replace("{$prefix}",g_table_prefix)
  105.                 return True
  106.        
  107.         def NextDocument(self, item, bDofetch):
  108.                 if self.m_cursor == None or bDofetch:
  109.                         #do fetch
  110.                         try:
  111.                                 self.m_cursor = self.m_dbconn.cursor ()
  112.                                 sql_condition = " AND c.id>="+str(self.m_start_id) + " AND c.id<="+str(self.m_start_id+g_each_max_record_count)
  113.                                 sql_condition = sql_condition.replace("{$prefix}",g_table_prefix)
  114.                                 rowCount = self.m_cursor.execute (self.m_basesql_str+sql_condition)
  115.                                 self.m_start_id = self.m_start_id + g_each_max_record_count+1; #append 1 to avoid doc_id duplicate.
  116.                                 #print 'select ', self.m_start_id, self.m_maxid
  117.                         except MySQLdb.Error, e:
  118.                                 print "Error %d: %s" % (e.args[0], e.args[1])
  119.                                 return False
  120.                         if rowCount == 0 and self.m_start_id > self.m_maxid:
  121.                                 return False;
  122.                         self.m_row = self.m_cursor.fetchone()
  123.                 else:
  124.                         self.m_row = self.m_cursor.fetchone()
  125.                 if self.m_row == None:
  126.                         return self.NextDocument(item,True)
  127.                 #do data assign
  128.                 item.post_id = self.m_row[0]
  129.                 item.cid = self.m_row[1]
  130.                 item.authorid = self.m_row[2]
  131.                 item.title = self.m_row[3]
  132.                 item.description = self.m_row[4]
  133.                 item.author = self.m_row[5]
  134.                 item.state = self.m_row[6]
  135.                 item.dateline = self.m_row[7]               
  136.                 item.top = self.m_row[8]               
  137.                 item.pink = self.m_row[9]
  138.                 item.content = self.m_row[10]
  139.                 item.views = self.m_row[11]
  140.                 item.comments = self.m_row[12]                       
  141.                 #item.post_id = 0
  142.                 return True
  143.         def OnIndexFinished(self):
  144.                 sql = """UPDATE {$prefix}settings SET value='{$id}' WHERE variable='idx_postid'
  145.                                 """
  146.                 sql = sql.replace("{$prefix}", g_table_prefix)
  147.                 sql = sql.replace("{$id}", str(self.m_maxid))
  148.                 print sql
  149.                 print self.m_maxid
  150.                 #self.Connected()
  151.                 #self.m_cursor = self.m_dbconn.cursor ()
  152.                 #Change DataBase Enocding here, is to support gbk or others.
  153.                 self.m_cursor.execute(sql)
  154.                 #self.m_dbconn.cursor().execute(sql)
  155.                 return True

  156. # your must declare a class drive from DataSource, if mutil-class defined in py file, only the 1st will be use.
  157. class t_DataSource(DataSource):
  158.         def __init__(self):
  159.                 """
  160.                 in this function ,you should define data schema, string2ord is not supported.
  161.                 supported types:
  162.                 string(use to launch full text search)
  163.                 number(int)
  164.                 timestamp(time)
  165.                 list(mva)
  166.                 if a attribue started with '__', it will not be used as a data attribute
  167.                 """
  168.                
  169.                 self.post_id = 0
  170.                 self.cid = 0
  171.                 self.author = 0
  172.                 self.title = ''
  173.                 self.description = ''
  174.                 self.author = ''
  175.                 self.state = 0
  176.                 self.views = 0
  177.                 self.state = 0
  178.                 self.top = 0
  179.                 self.content = ''
  180.                 self.pink = 0
  181.                 self.comments = 0
  182.                 self.dateline = 0
  183.                 #self.digest  = True
  184.                 #self.istop = True
  185.                 #inner attribute define
  186.                 self.__docid__ = 'post_id'; #use this to declare which attr is document unique id.
  187.                 self.__p__ = t_Private();
  188.                 pass
  189.                
  190.         def Dump_Object(self):
  191.                 ret = ''
  192.                 instancemethod = type(self.NextDocument)
  193.                 for s in dir(self):
  194.                         if s[0] == '_':
  195.                                 continue
  196.                         if type(getattr(self,s)) == instancemethod:
  197.                                 continue
  198.                         #print type(getattr(self,s)),s
  199.                         ret = ret + s+': '+ str(getattr(self,s)) + ', '
  200.                 return ret;
  201.                
  202.         def __del__(self):
  203.                 #print 'delete source';
  204.                 pass
  205.        
  206.         def Connected(self):
  207.                 return self.__p__.Connected();
  208.        
  209.         def OnBeforeIndex(self):
  210.                 #print 'OnBeforeIndex called';
  211.                 return self.__p__.BeforeIndex();
  212.        
  213.         def NextDocument(self):
  214.                 global NEEDEXIT
  215.                 if NEEDEXIT:
  216.                         return False
  217.                 bRet = self.__p__.NextDocument(self,False);
  218.                 #you can deal the context ,eg.
  219.                 """
  220.                 1 skip too small documents
  221.                 2 strip htmls
  222.                 3 strip emotions.
  223.                 """
  224.                 #do encoding convert also, sphinx can only accept utf-8 string.
  225.                 if bRet:
  226.                         pass
  227.                         #self.title = u(self.title).encode('gbk')
  228.                         #self.content = u(self.content).encode('gbk')
  229.                 else:
  230.                         self.post_id = 0
  231.                 return bRet
  232.                
  233.         def OnAfterIndex(self):
  234.                 #print 'OnAfterIndex';
  235.                 pass
  236.         def OnIndexFinished(self):
  237.                 print 'OnIndexFinished'
  238.                 return self.__p__.OnIndexFinished();
  239.                
  240. #deal ctrl+c in indexer
  241. NEEDEXIT=False
  242. def SignalHandler(sig, id):
  243.     global NEEDEXIT
  244.     NEEDEXIT = True
  245.     pass
  246.        
  247. signal.signal(signal.SIGINT,   SignalHandler)   
  248. # test bench
  249. if bStandAlone:
  250.         ds = Ask_DataSource();
  251.         ds.Connected();
  252.         if not ds.OnBeforeIndex():
  253.                 exit(0)
  254.         while(1):
  255.                 if not ds.NextDocument():
  256.                         break;
  257.                 if ds.post_id <= 0:
  258.                         break
  259.                 print ds.Dump_Object();
  260.         ds.OnAfterIndex();
  261.         ds.OnIndexFinished();
  262.         pass
  263. #data source define end
复制代码

这个程序在执行的时候会报错。
错误提示代码如下:
  1. Exception exceptions.AttributeError: "'Cursor' object has no attribute 'connecti
  2. on'" in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x00E2
  3. 5F50>> ignored
  4. Exception exceptions.AttributeError: "'Cursor' object has no attribute 'connecti
  5. on'" in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x00E2
  6. 5F50>> ignored
  7. Exception exceptions.AttributeError: "'Cursor' object has no attribute 'connecti
  8. on'" in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x00E2
  9. 5F50>> ignored
  10. Exception exceptions.AttributeError: "'Cursor' object has no attribute 'connecti
  11. on'" in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x00E2
  12. 5F50>> ignored
  13. Exception exceptions.AttributeError: "'Cursor' object has no attribute 'connecti
  14. on'" in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x00E2
  15. 5F50>> ignored
  16. Exception exceptions.AttributeError: "'Cursor' object has no attribute 'connecti
  17. on'" in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x00E2
  18. 5F50>> ignored
  19. Exception exceptions.AttributeError: "'Cursor' object has no attribute 'connecti
  20. on'" in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x00E2
  21. 5F50>> ignored
  22. Exception exceptions.AttributeError: "'Cursor' object has no attribute 'connecti
  23. on'" in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x00E2
  24. 5F50>> ignored
  25. Traceback (most recent call last):
  26.   File "<string>", line 1, in <module>
  27.   File "E:\searchd\share\hxkang\plugins\hxkang.main.py", line 246, in OnIndexFin
  28. ished
  29.     return self.__p__.OnIndexFinished();
  30.   File "E:\searchd\share\hxkang\plugins\hxkang.main.py", line 160, in OnIndexFin
  31. ished
  32.     self.m_cursor.execute(sql)
  33.   File "C:\Python25\Lib\site-packages\MySQLdb\cursors.py", line 143, in execute
  34.     from types import ListType, TupleType
  35. TypeError: an integer is required
  36. Exception exceptions.TypeError: 'an integer is required' in 'garbage collection'
  37. ignored
  38. Fatal Python error: unexpected exception during garbage collection
复制代码
前面的错误提示是出现非常多行,在强行ctrl+C退出后出现Traceback后面这段错误;

论坛徽章:
0
2 [报告]
发表于 2008-06-03 10:38 |只看该作者
'Cursor' object has no attribute 'connection'这个错误提示不是很明确么?

论坛徽章:
0
3 [报告]
发表于 2008-06-03 13:53 |只看该作者
这段代码貌似是高手写的。
有空阅读下,呵呵

论坛徽章:
0
4 [报告]
发表于 2008-06-03 15:35 |只看该作者
这个是sphinx的for gbk版本里的数据源代码,现在最主要的问题是他这个数据源貌似会运行两次,而报错就是第二次运行的时候报的,搞不明白,之前一个已经成功运行了,这个是修改过sql语句的版本,但是没办法运行。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP