免费注册 查看新帖 |

Chinaunix

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

自己写的一个“打印父类”的一个小程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-25 11:40 |只看该作者 |倒序浏览
当学习一些大型的、复杂的程序时这个应该会很有用
列出来,大家共享

若想经常使用里面的两个函数,可以在 PYTHONSTARTUP 环境变量文件里
添加一句:from prt_deep_bases import *
以后就可以直接应用了

prt_deep_bases.py

#!/usr/bin/env python

_prestr = ''        # for function tree()
_level = 0        # for function info()

def tree(cls):
    """ print the base_classes of the given class by tree diagram """
    global _prestr    # the pre_string of every printed line
    bases = cls.__bases__

    if bases:
        if not _prestr :            # print the first line
            print  cls.__name__
        _prestr += ' ' * 4
        n = length = len(bases)        # n flags the number of unprinted base_class
        for i in range(length):
            print _prestr + '+--->' + bases[i].__name__
            if bases[i].__bases__:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_prestr += ('|   ' if n > 1 else ' ' * 4)&nbsp;&nbsp;&nbsp;&nbsp;# n <= 1 indicates the last base of bases
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree(bases[i])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# recursion
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_prestr = _prestr[:-4]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n -= 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# decrease the number of unprinted base_class

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if i == length - 1:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_prestr = _prestr[:-4]

def info(cls):
&nbsp;&nbsp;&nbsp;&nbsp;""" print the base_classes of the given class indentively one by one """
&nbsp;&nbsp;&nbsp;&nbsp;global _level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the recursion level
&nbsp;&nbsp;&nbsp;&nbsp;bases = cls.__bases__

&nbsp;&nbsp;&nbsp;&nbsp;if bases:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print ' ' * 4 * _level + cls.__name__
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for each_base in bases:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print ' ' * 4 * (_level+1) + '+--->' + each_base.__name__
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_level += 1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for each_base in bases:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;info(each_base)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# recursion
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_level -= 1

def _app():
&nbsp;&nbsp;&nbsp;&nbsp;""" test the function tree() and info() """
&nbsp;&nbsp;&nbsp;&nbsp;class a:pass
&nbsp;&nbsp;&nbsp;&nbsp;class b:pass
&nbsp;&nbsp;&nbsp;&nbsp;class c(a, b):pass
&nbsp;&nbsp;&nbsp;&nbsp;class d:pass
&nbsp;&nbsp;&nbsp;&nbsp;class e(c, d):pass
&nbsp;&nbsp;&nbsp;&nbsp;class f:pass
&nbsp;&nbsp;&nbsp;&nbsp;class g(e, f):pass

&nbsp;&nbsp;&nbsp;&nbsp;title = lambda str: '='*10 + str + '='*10

&nbsp;&nbsp;&nbsp;&nbsp;print title('tree')
&nbsp;&nbsp;&nbsp;&nbsp;tree(g)
&nbsp;&nbsp;&nbsp;&nbsp;print title('info')
&nbsp;&nbsp;&nbsp;&nbsp;info(g)
&nbsp;&nbsp;&nbsp;&nbsp;print title('='*4)

if __name__ == '__main__':
&nbsp;&nbsp;&nbsp;&nbsp;_app()


[ 本帖最后由 爱知 于 2009-2-26 17:48 编辑 ]

评分

参与人数 1可用积分 +5 收起 理由
xiaoyu9805119 + 5 精品文章

查看全部评分

论坛徽章:
0
2 [报告]
发表于 2009-02-25 11:41 |只看该作者
运行结果:
==========tree==========
g
    +--->e
    |       +--->c
    |       |       +--->a
    |       |       +--->b
    |       +--->d
    +--->f
==========info==========
g
    +--->e
    +--->f

    e
        +--->c
        +--->d

        c
            +--->a
            +--->b

========================

论坛徽章:
0
3 [报告]
发表于 2009-02-25 11:44 |只看该作者
以上程序是在学习 wxpython 时用的
>>> import wx
>>> tree(wx.Frame)
Frame
    +--->TopLevelWindow
            +--->Window
                    +--->EvtHandler
                            +--->Object
                                    +--->object
>>>

论坛徽章:
0
4 [报告]
发表于 2009-02-25 11:49 |只看该作者

回复 #3 爱知 的帖子

恩。。感谢您为人类的python史发展推动了不可动摇坚定不移关键性的一步
那个。。虽然我一点都没看懂。。
不过还是从字里行间中感受到了一股,强大的,扑面而来的,迅雷不及掩耳的,质朴的,无法用言语表达的感觉。
再此对您的无私的,伟大的,博爱的,宽正宏和的开源气节予以精神上的表彰
楼下的继续夸,夸到楼主脸红,把所有好东东都共享出来为止

论坛徽章:
0
5 [报告]
发表于 2009-02-25 11:52 |只看该作者
不错,感谢分享

论坛徽章:
0
6 [报告]
发表于 2009-02-25 20:25 |只看该作者
cool...

论坛徽章:
0
7 [报告]
发表于 2009-02-25 22:49 |只看该作者
很帅很有用

论坛徽章:
0
8 [报告]
发表于 2009-02-26 17:54 |只看该作者
又加了一些注释

论坛徽章:
0
9 [报告]
发表于 2009-03-01 16:33 |只看该作者
很好的东东
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP