Archive for the 'Python' Category

Page 2 of 4

glade+pygtk学习 (2)

按照计划,今天参照这篇文章 Building an Application with PyGTK and Glade 学习。第一次使用menu和toolbar,进一步学习了tree/list view的使用,昨天还不太会用的dialog也学会了。

下载源代码: pygtk-learning-pywine.tgz

昨天遗留的几个问题也解决了:
1. 首先完成了那个dialog。虽然没什么功能,就算是复习一下dialog的实现方法吧。
2. 把ServerInfo程序的treeview内容改成显示http的header了。

  1. #get http headers, then insert into tree model
  2. data = page.read()
  3. headers = page.info()
  4. for name in headers.keys():
  5.     insert_row(self.treemodel, None, name, headers[name])

3. 把页面内容转换成unicode字符。用到了re模块去取页面meta标记里的字符集设置。

  1. import re, string
  2.        
  3. r = re.compile(r""".*content=['|"]?text/html;\s?charset=([a-zA-Z0-9\-]+)['|"]?.*""", re.S | re.I | re.M)
  4. m = r.match(data)
  5. if m:
  6.     charset = string.lower(m.group(1))
  7.     if charset != 'utf-8':
  8.         data = data.decode(charset, 'ignore').encode('utf-8', 'ignore')
  9.     else:
  10.         data = data.encode('utf-8', 'ignore')
  11. self.logwindow.set_text(data)

4. 用TexBuffer的set_text方法替代了insert_at_cursor。

新ServerInfo程序的源代码: pygtk-learning-serverinfo.tgz

glade+pygtk学习

今天上午参照 A Beginner’s Guide to Using pyGTK and Glade 这篇文章学习,感觉确实很好用。文章开始部分对比几种GUI开发库写得挺好,虽然是2003年写的。还有后面提到如何结合socket编程,正好要用到。

照着Demo实做了一遍,发现一些用法已经deprecated了:

  • gtk.TRUE – 用True就行了
  • gtk.mainquit – 换成gtk.main_quit
  • gtk.mainloop() – 换成gtk.main()

期间犯了个小错误,忘了在glade里为主窗口添加destroy信号,结果没法关闭窗口后没法退出程序。

显示含有中文的页面内容时会在下面这行代码报错,像是编码问题,先不深究了。

self.logwindow.insert_at_cursor(data, len(data))

自己用为Entry控件加了activate信号,响应回车事件。也准备试试如何显示对话框,还没有完成。

可以在这里下载Demo的源代码: guoyong.org/?dl=pygtk-demo.tgz

接下来计划按照这篇文章 [ www.learningpython.com/2006/05/30/building-an-appl... ]学习,然后准备自己动手写了。

一个把Rss Feed的编码方式由GB2312转成UTF-8的CGI脚本

今天设置自己的guoyong.org” href=”http://planet.guoyong.org”>Planet站点,用CU的feed做试验,发现会乱码,原因是CU的feed是GB2312编码的。

google到一些资料,但好像并不能完全解决问题:

小林的blog,按照这个方法做没成功。

Web4C,我写完脚本才注意到这个搜索结果,但没找到planet源码里的planetlib.py文件。

我想到的土方法,写一个cgi脚本做proxy,取回feed后做转换编码方式再输出。

转码是这样完成的: str.decode('GBK').encode('UTF-8')

下载

Python for S60 1.3.8

论坛里有人提醒,Python for S60 1.3.8出来了,这个版本是专门用于S60 3rd Edition的device。下载地址:http://sourceforge.net/projec…id=171153

Google Scholar Bibtex Parser – gsbibtex.py

与bibtex.py类似,区别:

  • 只针对从Google Scholar中导出的BibTex
  • 不依赖第三方软件(比如bibtex2html) ,完全使用Python。需要pyparsing模块。
  • 效果与bibtex.py类似,但目前不支持style参数
  • 支持对附加文件的链接,包括pdf, ps, dvi, rtf, chm, nh等格式

下载: gsbibtex.py

我只在我使用的desktop edition 1.5.3-1下测试过,OS是Windows XP SP2。
示例:

{{{#!gsbibtex
@article{iannella2003drm,
title={{Digital Rights Management (DRM) Architecture}},
author={Iannella, R.},
journal={Internet: http://www. dlib. org/dlib/june01/iannella/06iannella. html, Stand},
volume={7},
year={2003},
abstract={this is a abstract},
Keywords={drm watermarking},
pdf={attachment:demo.pdf},
chm={attachment:测试.chm},
url={http://www.dlib.org/dlib/june01/iannella/06iannella.html},
nh={attachment:数字版权管理(DRM)系统的研究、设计和实现.nh.zip}
}
@article{俞银燕2005数字版,
title={{数字版权保护技术研究综述}},
author={俞银燕 and 汤帜},
journal={计算机学报},
volume={28},
number={12},
pages={1957-1968},
year={2005},
publisher={万方数据资源系统},
comment={write your comment here}
}
}}}
{{{#!gsbibtex usekeys=off
@article{iannella2003drm,
title={{Digital Rights Management (DRM) Architecture}},
author={Iannella, R.},
journal={Internet: http://www. dlib. org/dlib/june01/iannella/06iannella. html, Stand},
volume={7},
year={2003}
}
@article{pitkanen2000tdr,
title={{Towards A Digital Rights Management Framework}},
author={Pitk{\"a}nen, O. and V{\"a}lim{\"a}ki, M.},
journal={IeC2000 Proceedings, UMIST, Manchester, UK},
year={2000}
}
}}}

效果:

gsbibtex output




This work by wolfg is licensed under a Attribution-Noncommercial-Share Alike 2.5 China Mainland.