模块文档字符串的快速参考。 [英] Quick Reference from module doc strings.

查看:48
本文介绍了模块文档字符串的快速参考。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有人建议如何进一步改善这个?

干杯,

Ron_Adam


def getobjs(object,dlist = [],lvl = 0,maxlevel = 1):

"""从对象中检索子对象列表。 """

如果对象不在dlist中:

dlist.append(object)

如果lvl< maxlevel:

dobj = dir(eval(object))

for dobj中的项目:

尝试:

dlist = getobjs(object + ''。'''+ item,dlist,lvl + 1)

除外:

pass

return dlist

def printdoc(objectlist):

"""从对象列表中返回一个已排序的可打印快速参考

指南。 ""

outtext = []

objectlist.sort(lambda x,y:cmp(x.lower(),y.lower()))

对象列表中的obj:

object = eval(obj)

object_type = type(object)

outtext .append('' - ''* 40 +''\ n'')

outtext.append(str(obj)+''\ n'')

if hasattr(object,''__ modle ___'):

outtext.append(" Module:" + str(object .__ module __)+''\ n'')

if hasattr(object,''__ class__''):

outtext.append(" Class:" + str(object .__ class __)+''\ nn \\ n'')

else:

outtext.append(" Type:" + str(object_type)+''\ n \''')

if isinstance(object,str):

if len(object)> 200:

s = object [0:200] +" ; ......"

else:

s = object

outtext.append(obj +''='')

如果s中的''\ n'':quotes =''"& ;"''

else:quotes =''"''

if len(s)> 60:print

outtext.append(引号+ s +引号+''\ n \''')

elif(isinstance(object,str)

或isinstance(object,int )

或isinstance(对象,布尔)

或isinstance(对象,元组)

或isinstance(对象,列表)

或isinstance(object,dict)):

s = str(object)

if len(s)< 200:

outtext.append(obj +''=''+ s +''\ n \''')

else:

outtext.append(obj +''= ''+ s [0:200] +''...... \ n \ n'')

if hasattr(object,''__ doc __''):

if object .__ doc _!= type(object).__ doc __:

outtext.append(str(object .__ doc __)+''\ n \ n'')

返回''''。join(outtext)


def quick_ref(姓名):

"""

quick_ref(module_name) - >可打印的字符串


从对象生成一个排序的快速参考指南

doc strings。 module_name是一个名称为

的字符串,用于获取文档字符串的模块或类。


示例:

import os

print quick_ref(''os'')

"""

objlist = getobjs(name)

返回printdoc(objlist)

if __name__ ==" __ main __":

#import module在大多数情况下调用之前。

print quick_ref(''__ builtins__'')

解决方案

Ron Adam写道:

有没有人建议如何进一步改善这一点?


不起作用(来自我)。但是如果你能够承担风格

评论,请继续阅读:-)

elif(isinstance(object,str)
或isinstance(object,int)
或isinstance(object,bool)
或isinstance(object,tuple)
或isinstance(object,list)
或isinstance(object,dict)):




从Python 2.2版开始,isinstance的第二个arg可能是一个元组。你可以预先定义这个有意义的名字:


TYPES_WHICH_whatever =(str,int,bool等等)


干杯,

John


John Machin写道:

Ron Adam写道:

有没有人有关于如何进一步改进这一点的建议?



不起作用(来自我)。但是如果您能够承担风格的评论,请继续阅读:-)

elif(isinstance(object,str)
或isinstance( object,int)
或isinstance(object,bool)
或isinstance(object,tuple)
或isinstance(object,list)
或isinstance(object,dict)):



从Python 2.2版开始,isinstance的第二个arg可能是一个元组。你可以预先定义一个有意义的名字:

TYPES_WHICH_whatever =(str,int,bool等等)

干杯,
约翰




其实我是在征求意见,这是一个项目的开始,而不是最终的结果。那谢谢啦! ;-)


我改为:


如果类型(对象)== str:

。 ...

elif type(object)in(str,int,bool,tuple,list,dict):

....

谢谢,我不需要isinstance(),类型也可以在这里工作。


它需要什么才能使它有用?我正在考虑在数据库中使用

,您可以按主题和

关键字快速获取信息,而不仅仅是模块。我也在考虑用它来生成

的网页。


就每个项目的doc字符串的实际内容而言,我们可以提交

要求在需要的地方进行改进。


干杯,

_Ron


Ron Adam写道:

John Machin写道:

Ron Adam写道:

有没有人建议如何进一步改善这个?
不是功能性的(来自我)。但是如果您能够承担风格的评论,请继续阅读:-)

elif(isinstance(object,str)
或isinstance(object,int)
或isinstance(object,bool)
或isinstance(object,tuple)
或isinstance(object,list)
或isinstance(object,dict)):



从Python 2.2版开始,isinstance的第二个arg可能是一个元组。你可以预先定义一个有意义的名字:

TYPES_WHICH_whatever =(str,int,bool等等)



其实我乞求评论,这是一个项目的开始,而不是结束。那谢谢啦! ;-)

我改为:

如果类型(对象)== str:
....
elif类型(对象) in(str,int,bool,tuple,list,dict):
....




Althought对象是你自己价值的可怕名字(你可能想要一个用于定义新式类的内置

对象):


if isinstance(object,(str,int, bool,tuple,list,dict)):

...

或(正如John Machin试图建议的那样):


if isinstance(object,TYPES_WHICH_whatever):

...


这允许你使用那些内置类型的实例和任何

用户定义的子类型。

谢谢,我不需要isinstance(),类型也可以在这里工作。




但isinstance版本比......版本中的类型(...)更好。


--Scott David Daniels
Sc *********** @ Acm.Org



Does anyone have suggestions on how to improve this further?
Cheers,
Ron_Adam

def getobjs(object, dlist=[], lvl=0, maxlevel=1):
""" Retrieve a list of sub objects from an object. """
if object not in dlist:
dlist.append(object)
if lvl<maxlevel:
dobj = dir(eval(object))
for item in dobj:
try:
dlist = getobjs(object+''.''+item, dlist, lvl+1)
except:
pass
return dlist

def printdoc(objectlist):
""" Return a sorted printable quick reference
guide from a list of objects. """
outtext = []
objectlist.sort(lambda x, y: cmp(x.lower(), y.lower()))
for obj in objectlist:
object = eval(obj)
object_type = type(object)
outtext.append(''-''*40+''\n'')
outtext.append(str(obj)+''\n'')
if hasattr(object, ''__module__''):
outtext.append("Module:"+str(object.__module__)+''\ n'')
if hasattr( object,''__class__''):
outtext.append("Class:"+str(object.__class__)+''\n\ n'')
else:
outtext.append("Type:"+str(object_type)+''\n\n'')
if isinstance(object,str):
if len(object)>200:
s = object[0:200]+"......"
else:
s = object
outtext.append(obj+''='')
if ''\n'' in s: quotes=''"""''
else: quotes =''"''
if len(s)>60: print
outtext.append(quotes+s+quotes+''\n\n'')
elif (isinstance(object,str)
or isinstance(object,int)
or isinstance(object,bool)
or isinstance(object,tuple)
or isinstance(object,list)
or isinstance(object,dict)):
s = str(object)
if len(s)<200:
outtext.append(obj+''=''+s+''\n\n'')
else:
outtext.append(obj+''=''+s[0:200]+''......\n\n'')
if hasattr(object,''__doc__''):
if object.__doc__ != type(object).__doc__:
outtext.append(str(object.__doc__)+''\n\n'')
return ''''.join(outtext)

def quick_ref(name):
"""
quick_ref(module_name) -> printable string

Generate a sorted quick reference guide from an objects
doc strings. The module_name is a string with the name of
the module or class to get documents string from.

Example:
import os
print quick_ref(''os'')
"""
objlist = getobjs(name)
return printdoc(objlist)

if __name__ == "__main__":
#import module before calling in most cases.
print quick_ref(''__builtins__'')

解决方案

Ron Adam wrote:

Does anyone have suggestions on how to improve this further?
Not functionally (from me, yet). However if you can bear a stylistic
comment, do read on :-)
elif (isinstance(object,str)
or isinstance(object,int)
or isinstance(object,bool)
or isinstance(object,tuple)
or isinstance(object,list)
or isinstance(object,dict)):



Since Python version 2.2, the 2nd arg of isinstance may be a tuple. You
could define this up front, with a meaningful name:

TYPES_WHICH_whatever = (str, int, bool, etc etc)

Cheers,
John


John Machin wrote:

Ron Adam wrote:

Does anyone have suggestions on how to improve this further?


Not functionally (from me, yet). However if you can bear a stylistic
comment, do read on :-)

elif (isinstance(object,str)
or isinstance(object,int)
or isinstance(object,bool)
or isinstance(object,tuple)
or isinstance(object,list)
or isinstance(object,dict)):


Since Python version 2.2, the 2nd arg of isinstance may be a tuple. You
could define this up front, with a meaningful name:

TYPES_WHICH_whatever = (str, int, bool, etc etc)

Cheers,
John



Actually I''m begging for comments, it''s the beginning of a project not
the end. So thanks! ;-)

I changed it to:

if type(object)==str:
....
elif type(object) in (str,int,bool,tuple,list,dict):
....

Thanks, I don''t need the isinstance(), type works here just as well.

What would it take to make it useful? I''m thinking of putting it to use
in a database where you can get quickly get info by subject and
keywords, not just by module. I''m also thinking of using it to generate
web pages.

As far as the actual content of doc strings for each item, we can submit
requests for improvements where it''s needed.

Cheers,
_Ron


Ron Adam wrote:

John Machin wrote:

Ron Adam wrote:

Does anyone have suggestions on how to improve this further?
Not functionally (from me, yet). However if you can bear a stylistic
comment, do read on :-)

elif (isinstance(object,str)
or isinstance(object,int)
or isinstance(object,bool)
or isinstance(object,tuple)
or isinstance(object,list)
or isinstance(object,dict)):



Since Python version 2.2, the 2nd arg of isinstance may be a tuple. You
could define this up front, with a meaningful name:

TYPES_WHICH_whatever = (str, int, bool, etc etc)



Actually I''m begging for comments, it''s the beginning of a project not
the end. So thanks! ;-)

I changed it to:

if type(object)==str:
....
elif type(object) in (str,int,bool,tuple,list,dict):
....



Althought object is a horrible name for your own value (there is a builtin
object which you use for defining new-style classes), you probably want:

if isinstance(object, (str,int,bool,tuple,list,dict)):
...
or (as John Machin was trying to suggest):

if isinstance(object, TYPES_WHICH_whatever):
...

This allows you to use instances of those builtin types and any
user-defined subtypes.
Thanks, I don''t need the isinstance(), type works here just as well.



But the isinstance version is better than the type(...) in ... version.

--Scott David Daniels
Sc***********@Acm.Org


这篇关于模块文档字符串的快速参考。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆