#data row
class Row(pb.Copyable):
def __init__(self,entries):
self.__dict__.update(entries)
#tran objects
########################################################################
class ResultObj(pb.Copyable):
#字符串太长pb无法 传输,转成列表,原理跟FilePager一样
def __init__(self,rs,PageNo=1,pageSize=300,filedDict=None):
self.pageSize=pageSize
if rs.RecordCount:
rs.AbsolutePage=PageNo
self.pageNo=PageNo
rs.PageSize=self.pageSize
self.pageCount=rs.PageCount
if rs.RecordCount:
self.rs=rs.GetRows(self.pageSize)
self.field_num=rs.Fields.Count
self.next=None
self.count=self.pageSize
if self.pageNo<self.pageCount:
self.next=ResultObj(rs,PageNo+1,self.pageSize,filedDict)
else:
self.count=rs.RecordCount-(self.pageNo-1)*self.pageSize
#查询结果的每一列 生成字典
if filedDict is None:
self.filedDict={}
for n in xrange(self.field_num):
self.filedDict[n]=rs.Fields.Item(n).Name.lower()
#没有列明转成 c0 c1.....
if not self.filedDict[n]:
self.filedDict[n]='c'+str(n)
else:
self.filedDict=filedDict
def __len__(self):
return self.count
def __getitem__(self,key):
row={}
for cur_col in xrange(self.field_num):
row[self.filedDict[cur_col]]=self.rs[cur_col][key]
return Row(row)
def __iter__(self):
return iter(self.__next__())
#遍历列表
def __next__(self):
fiedlcount=xrange(self.field_num)
while self:
for cur_row in xrange(self.count):
row={}
for cur_col in fiedlcount:
row[self.filedDict[cur_col]]=self.rs[cur_col][cur_row]
yield Row(row)
self=self.next
########################################################################
class ResultDict(ResultObj,pb.Copyable):
def __init__(self,rs,PageNo=1,pageSize=300,filedDict=None):
ResultObj.__init__(self,rs,PageNo=1,pageSize=300,filedDict=None)
def __getitem__(self,key):
row={}
for cur_col in xrange(self.field_num):
row[self.filedDict[cur_col]]=self.rs[cur_col][key]
return row
#遍历列表
def __next__(self):
fiedlcount=xrange(self.field_num)
while self:
for cur_row in xrange(self.count):
row={}
for cur_col in fiedlcount:
row[self.filedDict[cur_col]]=self.rs[cur_col][cur_row]
yield row
self=self.next
########################################################################
class ResultArray(ResultObj,pb.Copyable):
def __init__(self,rs,PageNo=1,pageSize=300,filedDict=None):
ResultObj.__init__(self,rs,PageNo=1,pageSize=300,filedDict=None)
def __getitem__(self,key):
row=[]
for cur_col in xrange(self.field_num):
row.append(self.rs[cur_col][key])
return row
#遍历列表
def __next__(self):
fiedlcount=xrange(self.field_num)
while self:
for cur_row in xrange(self.count):
row=[]
for cur_col in fiedlcount:
row.append(self.rs[cur_col][cur_row])
yield tuple(row)
self=self.next
python将rs转换成可迭代的对象
最新推荐文章于 2020-11-05 09:04:16 发布