建议使用类似名称的方法 [英] Suggesting methods with similar names

查看:58
本文介绍了建议使用类似名称的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有许多方法的类Surface。在交互式

窗口中工作时,当我写错了方法名称时,我收到这样的错误:

table.addGlas()



回溯(最近一次调用最后一次):

文件"< ; stdin>",第1行,在?

AttributeError:''Surface''对象没有属性''addGlas''


是否有可能让对象给出一个更好的答案:一个简短的列表

的几个方法名称类似于我拼错的那个?


我已经发现了一些带有语音算法的模块,如soundex,
http://sourceforge.net/projects/advas/

我可以用以下方法生成方法名列表:

toRemove =""" __ delattr__ __dict__ __getattribute__ __module__ __new__

__reduce__ __copy__ __reduce_ex__ __setattr__ __slot__

__weakref__ __str__ __class__ __doc __""" .split()

methods = sorted( set(dir(Surface))。差异(toRemove))


问题是调用语音算法来显示排名列表

2-4方法名称最类似于错误的方法。我不知道

如果这个问题需要在python shell中更改,或者在Surface类的

元类中等等。

最后也许这个功能(灵感来自类似的

Mathematica)已经在IPython中: - ]


再见,

Bearophile

解决方案

是************ @ lycos.com 写道:

我有一个类Surface有很多方法。在交互式
窗口中工作时,当我写错了方法名称时,我收到类似的错误:

table.addGlas()


Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
AttributeError:''Surface''对象有没有属性''addGlas''

是否有可能让对象给出更好的答案:一个简短的列表
几个类似于我拼写错误的方法名称?b​​r />
我找到了一些带语音算法的模块,如soundex,
metaphone等,例如:
http://sourceforge.net/projects/advas/
我可以用这样的方法生成方法名列表: br /> toRemove =""" __ delattr__ __dict__ __getattribute__ __module__ __new__
__reduce__ __copy__ __reduce_ex__ __setattr__ __slot__
__ werefref__ __str__ __class__ __doc __""" .split()
methods = sorted(set(dir(Surface))。difference(toRemove))

问题正在调用语音算法来显示2-4个方法名称的排序列表,这些名称与被称为错误的方法名称最相似。我不知道
如果这个问题需要在python shell中更改,或者在Surface类的
元类中等等。
最后也许这个功能(灵感来自类似的Mathematica已经在IPython中了: - ]



你可以通过覆盖__getattribute__(未经测试)来实现这个目标:


def __getattribute __(self,name):

试试:

object .__ getattribute __(self,name)#或者你的超类是什么

#或者使用super(),但我必须查找语法和

#早餐正在等待;)

除了AttributeError:

#find类似属性

个建议= ....

引发AttributeError('''''''''''''''''''''''''''''''''''' >
表示%s?"%(名称,建议))


我将它留给专家将其包装成通用元类,装饰器

等;)


-

Benjamin Niemann

电子邮件:粉红色at odahoda dot de

WWW: http://www.odahoda.de/


>你可以通过覆盖__getattribute__(未经测试)来实现这个目标:


我相信OP会更好地使用__getattr__,只有在

属性不是时才调用通过正常查找找到,这是他唯一需要/ b $ b需要/想要定制控制的时间。


TJR


谢谢,__ getattr__做我需要的:-)

聪明的帮助可以轻松设计......


熊拥抱,

Bearophile


I have a class Surface with many methods. Working in the interactive
window I receive an error like this when I write the wrong method name:

table.addGlas()


Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: ''Surface'' object has no attribute ''addGlas''

Is it possibile to make the object give a better answer: a short list
of few method names similar to the one I''ve misspelled?

I''ve found some modules with phonetic algorithms like soundex,
metaphone, etc, for example here:
http://sourceforge.net/projects/advas/

I can produce the list of method names with this:
toRemove = """__delattr__ __dict__ __getattribute__ __module__ __new__
__reduce__ __copy__ __reduce_ex__ __setattr__ __slot__
__weakref__ __str__ __class__ __doc__""".split()
methods = sorted( set(dir(Surface)).difference(toRemove) )

The problem is calling the phonetic algorithm to show a ranked list of
the 2-4 method names most similar to the wrong one called. I don''t know
if this problem requires a change in the python shell, or in the
metaclass of that Surface class, etc.
And in the end maybe this functionality (inspired by a similar
Mathematica one) is already inside IPython :-]

Bye,
Bearophile

解决方案

be************@lycos.com wrote:

I have a class Surface with many methods. Working in the interactive
window I receive an error like this when I write the wrong method name:

table.addGlas()


Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: ''Surface'' object has no attribute ''addGlas''

Is it possibile to make the object give a better answer: a short list
of few method names similar to the one I''ve misspelled?

I''ve found some modules with phonetic algorithms like soundex,
metaphone, etc, for example here:
http://sourceforge.net/projects/advas/

I can produce the list of method names with this:
toRemove = """__delattr__ __dict__ __getattribute__ __module__ __new__
__reduce__ __copy__ __reduce_ex__ __setattr__ __slot__
__weakref__ __str__ __class__ __doc__""".split()
methods = sorted( set(dir(Surface)).difference(toRemove) )

The problem is calling the phonetic algorithm to show a ranked list of
the 2-4 method names most similar to the wrong one called. I don''t know
if this problem requires a change in the python shell, or in the
metaclass of that Surface class, etc.
And in the end maybe this functionality (inspired by a similar
Mathematica one) is already inside IPython :-]


You could achieve this by overriding __getattribute__ (untested):

def __getattribute__(self, name):
try:
object.__getattribute__(self, name) # or whatever is your superclass
# or use super(), but I would have to lookup the syntax and
# breakfast is waiting ;)
except AttributeError:
# find similar attributes
suggestions = ....
raise AttributeError("''Surface'' object has no attribute ''%s''. Did you
mean %s?" % (name, suggestions))

I leave it to the experts to wrap this into a generic metaclass, decorator
etc. ;)

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/


> You could achieve this by overriding __getattribute__ (untested):

I believe OP would do better to use __getattr__, which is only called when
the attribute is not found by the normal lookup, which is the only time he
needs/wants customized control.

TJR


Thank you, __getattr__ does what I need :-)
A smart help can be designed easely...

Bear hugs,
Bearophile


这篇关于建议使用类似名称的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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