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 11 1400 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
[Bearophile] 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?
[Bearophile] Thank you, __getattr__ does what I need :-) A smart help can be designed easely...
The idea is a winner. When you're done, consider posting the result as an ASPN
cookbook recipe.
Raymond Hettinger
Raymond Hettinger>When you're done, consider posting the result as an
ASPN cookbook recipe.<
I still cannot write in the cookbook... I think I have problems with
the registration. So you can put it there...
I've found that difflib is good enough for the string matching. This
idea isn't fully mine, it's modified from the Mathematica textual
interface. Here is the code with long lines:
| def __getattr__(self, name):
| "If a wrong method is called, suggest methods with similar
names."
| def match(str1, str2):
| "Return approximate string comparator measure (between 0.0
and 1.0) using difflib."
| if str1 == str2:
| return 1.0
| m1 = SequenceMatcher(None, str1, str2)
| m2 = SequenceMatcher(None, str2, str1)
| return (m1.ratio()+m2.ratio()) / 2.0 # average
|
| toRemove = """__delattr__ __dict__ __getattribute__ __module__
__new__ __reduce__ __copy__
| __reduce_ex__ __setattr__ __slot__ __weakref__ __str__
__class__ __doc__""".split()
| methods = set(dir(self.__class__)).difference(toRemove)
| name = name.lower()
| matchList = [ (match(name, m.lower()),m) for m in methods ]
| suggestions = sorted(matchList, reverse=True)[:5] # A heap isn't
necessary here
| suggestions = ", ".join( pair[1] for pair in suggestions )
| raise AttributeError, ("method '%s' not found. \nMost similar
named ones: %s" % (name, suggestions))
Note: the general idea of a smart help can be improved a *lot*, this is
the basic version :-]
Bear hugs,
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?
Have you heard of rlcompleter2? It gives you tab-completion on the repl.
While your idea looks nice at first glance, what I don't like about it that
it will make an object return always _something_ - and thus you don't catch
misspellings in non-interactive, or at least not where they actually happen
but when you try to work with the results.
--
Regards,
Diez B. Roggisch
Diez B. Roggisch>it will make an object return always _something_ - and
thus you don't catch misspellings in non-interactive<
Uhm, I'm sorry, I don't understand you.
If you look at the code I've just posted, you can see that it still
raises AttributeError, the difference is just the error message...
Bearophile
On Wed, 30 Mar 2005 17:55:32 GMT, "Raymond Hettinger" <vz******@verizon.net> wrote: [Bearophile] 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?
[Bearophile] Thank you, __getattr__ does what I need :-) A smart help can be designed easely...
The idea is a winner. When you're done, consider posting the result as an ASPN cookbook recipe.
Interactively, I often use dir(whatever) to find methods, but I sure wish dir had
some keyword arguments to limit the returned info various ways, e.g., methods
of the immediate class, not the whole mro, and optionally without the __xxx__ methods.
Ditto with help().
BTW, when are we going to be able to write
@classdeco
class Foo(object):
...
so we can implement smart help as a decorator?
I.e., the above decorator syntax would be a non-intrusive way of spelling
class Foo(object):
__metaclass__ = classdeco
...
(haven't thought about cascaded decorators in this context ;-)
Regards,
Bengt Richter
"Bengt Richter" <bo**@oz.net> wrote in message
news:42****************@news.oz.net... BTW, when are we going to be able to write
@classdeco class Foo(object):
That possibility is currently being discussed for 2.5 on the pydev list.
There are mixed opinions, none yet from Guido that I noticed.
Terry J. Reedy
Suggesting method names based on a wrong method name can be useful, but
I think the "smart help" can be improved: it can also be useful to have
a suggestion for method names on the basis on a short description (or
keywords) about what I want to do to/with the object. Maybe some people
here can give me some suggestions on how to do this.
I think I can add a
Keywords: xxxx, xxx, xxxx.
final part to each docstring of the methods, so a kind of little search
engine can search for the few most fitting methods based on the user
text search query, and on the methods keywords+doc texts.
User query example:
"How to rotate the object"
Result (a short ranked list of method names that can perform that
operation):
rotate, flip, pivot, mirror
(Note: the query is inserted calling a "help" method, or something
similar, etc. It doesn't require any magic.)
Do you know any little search engine that can be used for this purpose?
Thank you,
Bearophile
Is that last idea so stupid? Still, I'd like to know if you know some
little Python search engines for such purpose.
Thank you,
Bearophile This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jaunty Edward |
last post by:
Hi,
I have to show some of the posible usernames available when a user can
not get a perticular username in a very normal member registration
form.
I will be thankful if anyone can show me a...
|
by: Rim |
last post by:
Hi,
I have been thinking about how to overload the assign operation '='.
In many cases, I wanted to provide users of my packages a natural
interface to the extended built-in types I created for...
|
by: David MacQuigg |
last post by:
I'm not getting any feedback on the most important benefit in my
proposed "Ideas for Python 3" thread - the unification of methods and
functions. Perhaps it was buried among too many other less...
|
by: Mayer |
last post by:
Hello:
Is there a way to see at the python prompt the names of all the public
methods of a class or the names exported by a module? I know that
GUI-based IDEs have a nifty way of displaying...
|
by: Generic Usenet Account |
last post by:
A lot has been said in this newsgroup regarding the "evil" set/get
accessor methods. Arthur Riel, (of Vanguard Training), in his class,
"Heuristis for O-O Analysis & Design", says that there is...
| |
by: Martin Herbert Dietze |
last post by:
Hello,
I would like to implement a callback mechanism in which a
child class registers some methods with particular signatures
which would then be called in a parent class method.
In...
|
by: Just D |
last post by:
All,
What are advantages and disadvantages of these two different approaches?
1 . I create a base class with a few virtual methods, then I derive my
classes from this class, override these...
|
by: Aaron Watters |
last post by:
A C# question about constructors/static methods and inheritance:
Please help me make my code simpler!
For fun and as an exercise I wrote somewhat classical B-tree
implementation
in C# which I...
|
by: Elephant |
last post by:
Hello, question,
I want to make a COM-compatible .NET DLL (Visual Basic). For this I need an
interface. The DLL has a class that must contain methods that can be used
without making an instance...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |