Python 自定义模块 意见征集稿

tim_spac 2008-01-28 12:04:29
请诸位帮我看看,提出改进意见或使用中需要增加的功能
#!phthon
#-*- coding:GBK -*-
# filename:HtmlFunctions.py

def _attr(attr = {}):
return ''.join([' %s="%s"' % (k,v) for k,v in attr.items()])

def _th(content, attr={}):
return "<TH%s>%s</TH>" % (_attr(attr),content)

def _td(content, attr={}):
return "<TD%s>%s</TD>" % (_attr(attr),content)

def _td_c(content, attr={}):
attr['align']='center'
return _td(content, attr)

def _td_l(content, attr={}):
attr['align']='left'
return _td(content, attr)

def _td_r(content, attr={}):
attr['align']='right'
return _td(content, attr)

def _td_money(mny, attr={}):
attr['align']='right'
return _td("%.2f" % mny, attr)

def _tr(cells=[], attr={}):
return "<TR%s>%s</TR>" % (
_attr(attr),
''.join(cells)
)

def _tb(rows=[]):
return "<TBODY>%s</TBODY>" % '\n'.join(rows)

def _table(tableInfos={}):
if tableInfos.has_key('attrib') and tableInfos['attrib'] > '':
attrib = tableInfos['attrib']
else:
attrib = ''
if tableInfos.has_key('caption') and tableInfos['caption'] > '':
caption = "<CAPTION>%s</CAPTION>" %s (tableInfos['caption'])
else:
caption = ''
if tableInfos.has_key('rows') and tableInfos['rows'] > '':
rows = tableInfos['rows']
else:
rows = ''
return "<TABLE%s>%s%s</TABLE>" % (attrib, caption, rows)

class Table:
def __init__(self, attrib={}):
self.headColumns = []
self.caption = ''
self.attrib = attrib
pass

def bind(self, data=[]):
self.data = data
pass

def toString(self):
caption = self.caption == '' and '' or '<CAPTION>%s</CAPTION>' % self.caption
if len(self.headColumns) == 0:
headers = ''
else:
headers = "<THEAD>%s</THEAD>" % _tr(
''.join([_th(cell) for cell in self.headColumns]))
tbody = _tb([
( _tr(''.join([_td(cell) for cell in row])) )
for row in self.data
])
return "<TABLE%s>%s\n%s</TABLE>" % (_attr(self.attrib),headers,tbody)

def setHeader(self, headColumns=[]):
self.headColumns = headColumns

def setCaption(self, caption):
self.caption = caption

def setColumnAttr(self, columnAttribs=[]):
self.columnAttribs = columnAttribs
pass


使用示例:

import sys
sys.path.append("D:\\Workspace\\Python\\Lib")
import HtmlFunctions as Html
tb = Html.Table({'class':'sheetStyle'})
tb.bind(collectionsFee)
tb.setHeader([
'部门'
,'区域'
,'业务类型'
,'客户'
,'收费组'
,'联系人 | 电话'
,'Email'
,'地址'
,'合计金额'
,'备注' ])
tb.setCaption('-- 收费安排表 -- ')

print tb.toString()
...全文
181 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tim_spac 2008-02-02
  • 打赏
  • 举报
回复
首先谢谢hanker的意见。
说明一下:
1) 刚刚开始学习Python(i like it);
2) Django会去学的,但还要等一段时间;
3) 开发背景: 单位中常要临时生成一些简单数据报表,本人不是很喜欢拼接html标记的工作,想弄个小东西将数据库里查出了的内容往里面一放就完事.
hanker9608 2008-02-02
  • 打赏
  • 举报
回复
个人感觉没什么用,要实现界面和数据分离,推荐django,模板功能做的很好很强大
而且楼主的类还不支持单元格合并,实际开发中用处不是很大
tim_spac 2008-01-28
  • 打赏
  • 举报
回复
本帖在30日内征集意见,代码大家可以自由使用。所提建议一经采纳将加分。
tim_spac 2008-01-28
  • 打赏
  • 举报
回复
jq:您建议attrib封装一下,如何封装好呢?您希望使用它?
按您提的建议目前代码改造如下:
#!phthon
#-*- coding:GBK -*-
# filename:HtmlFunctions.py

def _attr(attr = {}):
return ''.join([' %s="%s"' % (k,v) for k,v in attr.items()])

def _th(content, attr={}):
return "<TH%s>%s</TH>" % (_attr(attr),content)

def _td(content, attr={}):
return "<TD%s>%s</TD>" % (_attr(attr),content)

def _td_c(content, attr={}):
attr['align']='center'
return _td(content, attr)

def _td_l(content, attr={}):
attr['align']='left'
return _td(content, attr)

def _td_r(content, attr={}):
attr['align']='right'
return _td(content, attr)

def _td_money(mny, attr={}):
attr['align']='right'
return _td("%.2f" % mny, attr)

def _tr(cells=[], attr={}):
return "<TR%s>%s</TR>" % (
_attr(attr),
''.join(cells)
)

def _tb(rows=[]):
return "<TBODY>%s</TBODY>" % '\n'.join(rows)

def _table(tableInfos={}):
if tableInfos.has_key('attrib') and tableInfos['attrib'] > '':
attrib = tableInfos['attrib']
else:
attrib = ''
if tableInfos.has_key('caption') and tableInfos['caption'] > '':
caption = "<CAPTION>%s</CAPTION>" %s (tableInfos['caption'])
else:
caption = ''
if tableInfos.has_key('rows') and tableInfos['rows'] > '':
rows = tableInfos['rows']
else:
rows = ''
return "<TABLE%s>%s%s</TABLE>" % (attrib, caption, rows)

class Table:
def __init__(self, attrib={}):
self.headColumns = []
self.caption = ''
self.attrib = {}
self.data = []
pass

def toString(self):
caption = (self.caption == '') \
and '' \
or ('<CAPTION>%s</CAPTION>' % self.caption)
if len(self.headColumns) == 0:
headers = ''
else:
headers = "<THEAD>%s</THEAD>" % _tr(
''.join([_th(cell) for cell in self.headColumns]))
tbody = _tb([
( _tr(''.join([_td(cell) for cell in row])) )
for row in self.data
])
return "<TABLE%s>%s\n%s\n%s</TABLE>" % (
_attr(self.attrib),
caption,
headers,
tbody)

使用示例:
import sys
sys.path.append("D:\\Workspace\\Python\\Lib")
import HtmlFunctions as Html
tb = Html.Table()
tb.attrib = {'class':'sheetStyle'}
tb.caption = "-- 收费安排表 (Summit)--"
tb.headColumns = [
'部门'
,'区域'
,'业务类型'
,'客户'
,'收费组'
,'联系人 | 电话'
,'Email'
,'地址'
,'合计金额'
,'备注' ]
tb.data = collectionsFee
print tb.toString()

请继续..
tim_spac 2008-01-28
  • 打赏
  • 举报
回复
thanks.
金庆 2008-01-28
  • 打赏
  • 举报
回复
命名HtmlFunctions不合适,直接命名html就可以了。
attrib好象应该封装一下。
Table.__init__()为什么只有一个默认参数,attrib为什么要特殊对待?建议同样使用setAttrib()方式。
setXXX()不需要默认参数,没必要。
或者根本不需要setXXX,因为属性已经暴露在外。

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧