Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4970317
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2013-06-24 16:58:06


  1. #data row
  2. class Row(pb.Copyable):
  3.     def __init__(self,entries):
  4.         self.__dict__.update(entries)
  5. #tran objects
  6. ########################################################################
  7. class ResultObj(pb.Copyable):
  8.     #字符串太长pb无法 传输,转成列表,原理跟FilePager一样
  9.     def __init__(self,rs,PageNo=1,pageSize=300,filedDict=None):
  10.         self.pageSize=pageSize    
  11.         if rs.RecordCount:
  12.             rs.AbsolutePage=PageNo
  13.         self.pageNo=PageNo
  14.         rs.PageSize=self.pageSize
  15.         self.pageCount=rs.PageCount
  16.         if rs.RecordCount:
  17.             self.rs=rs.GetRows(self.pageSize)
  18.         self.field_num=rs.Fields.Count
  19.         self.next=None
  20.         self.count=self.pageSize

  21.         if self.pageNo<self.pageCount:
  22.             self.next=ResultObj(rs,PageNo+1,self.pageSize,filedDict)
  23.         else:
  24.             self.count=rs.RecordCount-(self.pageNo-1)*self.pageSize
  25.         #查询结果的每一列 生成字典
  26.         if filedDict is None:
  27.             self.filedDict={}
  28.             for n in xrange(self.field_num):
  29.                 self.filedDict[n]=rs.Fields.Item(n).Name.lower()
  30.                 #没有列明转成 c0 c1.....
  31.                 if not self.filedDict[n]:
  32.                     self.filedDict[n]='c'+str(n)
  33.         else:
  34.             self.filedDict=filedDict

  35.     def __len__(self):
  36.         return self.count

  37.     def __getitem__(self,key):     
  38.         row={}
  39.         for cur_col in xrange(self.field_num):
  40.             row[self.filedDict[cur_col]]=self.rs[cur_col][key]
  41.         return Row(row)                    
  42.         
  43.     def __iter__(self):
  44.         return iter(self.__next__())
  45.     
  46.     #遍历列表
  47.     def __next__(self):
  48.         fiedlcount=xrange(self.field_num)
  49.         while self:
  50.             for cur_row in xrange(self.count):
  51.                 row={}
  52.                 for cur_col in fiedlcount:
  53.                     row[self.filedDict[cur_col]]=self.rs[cur_col][cur_row]
  54.                 yield Row(row)                    
  55.             self=self.next
  56. ########################################################################
  57. class ResultDict(ResultObj,pb.Copyable):
  58.     def __init__(self,rs,PageNo=1,pageSize=300,filedDict=None):
  59.         ResultObj.__init__(self,rs,PageNo=1,pageSize=300,filedDict=None)    
  60.     def __getitem__(self,key):     
  61.         row={}
  62.         for cur_col in xrange(self.field_num):
  63.             row[self.filedDict[cur_col]]=self.rs[cur_col][key]
  64.         return row        
  65.     #遍历列表
  66.     def __next__(self):
  67.         fiedlcount=xrange(self.field_num)
  68.         while self:
  69.             for cur_row in xrange(self.count):
  70.                 row={}
  71.                 for cur_col in fiedlcount:
  72.                     row[self.filedDict[cur_col]]=self.rs[cur_col][cur_row]
  73.                 yield row                    
  74.             self=self.next
  75. ########################################################################
  76. class ResultArray(ResultObj,pb.Copyable):
  77.     def __init__(self,rs,PageNo=1,pageSize=300,filedDict=None):
  78.         ResultObj.__init__(self,rs,PageNo=1,pageSize=300,filedDict=None)
  79.     
  80.     def __getitem__(self,key):     
  81.         row=[]
  82.         for cur_col in xrange(self.field_num):
  83.             row.append(self.rs[cur_col][key])
  84.         return row        
  85.     
  86.     #遍历列表
  87.     def __next__(self):
  88.         fiedlcount=xrange(self.field_num)
  89.         while self:
  90.             for cur_row in xrange(self.count):
  91.                 row=[]
  92.                 for cur_col in fiedlcount:
  93.                     row.append(self.rs[cur_col][cur_row])
  94.                 yield tuple(row)                    
  95.             self=self.next


文章来自:http://blog.csdn.net/ld490832353/article/details/8072142
阅读(1715) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~