www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Fully dynamic d by opDotExp overloading

reply bearophile <bearophileHUGS lycos.com> writes:
Andrei Alexandrescu:
 Pascalize!S s;
 s.foo(); // works
 s.Foo(); // works too
 s.fOo(); // yup, works again

I can show something even more extreme :-) What we are discussing in this thread is named the __getattr__ method in Python: http://docs.python.org/reference/datamodel.html#object.__getattr__ Note that in Python there's something even more powerful, __getattribute__: http://docs.python.org/reference/datamodel.html#object.__getattribute__ So I have created this, that I actually use in a large Graph class of mine that has many methods: http://code.activestate.com/recipes/409000/ Such class can be used from the interactive shell too, to play with graphs in an interactive way, modify them, plot them, etc. Often you may not remember the name of a method you need, or you may mistype it. In such situation __getattr__ is being called. It collects the class method names, removed the useless ones, and performs a similarity search between the given wrong method name and the strings in that list. Then returns the 4-5 most similar ones, each one followed by their docstring, that shows the function signature and the purpose of the method. So you can usually find the method you were looking for, and use it. This is useful both when using such Graph class from the command line and when you are writing code without an IDE that helps you finding method names. That Python code of mine becomes doable in D too (using for example my very fast approximate string distance. This is a case where you don't need the list of changes, but just the distance number. So superdan can rest in peace). Bye, bearophile
Apr 18 2009
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
bearophile wrote:
 Andrei Alexandrescu:
 Pascalize!S s;
 s.foo(); // works
 s.Foo(); // works too
 s.fOo(); // yup, works again

I can show something even more extreme :-) What we are discussing in this thread is named the __getattr__ method in Python: http://docs.python.org/reference/datamodel.html#object.__getattr__ Note that in Python there's something even more powerful, __getattribute__: http://docs.python.org/reference/datamodel.html#object.__getattribute__ So I have created this, that I actually use in a large Graph class of mine that has many methods: http://code.activestate.com/recipes/409000/ Such class can be used from the interactive shell too, to play with graphs in an interactive way, modify them, plot them, etc. Often you may not remember the name of a method you need, or you may mistype it. In such situation __getattr__ is being called. It collects the class method names, removed the useless ones, and performs a similarity search between the given wrong method name and the strings in that list. Then returns the 4-5 most similar ones, each one followed by their docstring, that shows the function signature and the purpose of the method. So you can usually find the method you were looking for, and use it. This is useful both when using such Graph class from the command line and when you are writing code without an IDE that helps you finding method names. That Python code of mine becomes doable in D too (using for example my very fast approximate string distance. This is a case where you don't need the list of changes, but just the distance number. So superdan can rest in peace). Bye, bearophile

There's an interesting idea... Instead of "No member 'foo'", you could have "No member 'foo'; did you mean 'far' or 'fur'?" -- Daniel
Apr 18 2009
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Daniel Keep wrote:
 
 bearophile wrote:
 Andrei Alexandrescu:
 Pascalize!S s;
 s.foo(); // works
 s.Foo(); // works too
 s.fOo(); // yup, works again

What we are discussing in this thread is named the __getattr__ method in Python: http://docs.python.org/reference/datamodel.html#object.__getattr__ Note that in Python there's something even more powerful, __getattribute__: http://docs.python.org/reference/datamodel.html#object.__getattribute__ So I have created this, that I actually use in a large Graph class of mine that has many methods: http://code.activestate.com/recipes/409000/ Such class can be used from the interactive shell too, to play with graphs in an interactive way, modify them, plot them, etc. Often you may not remember the name of a method you need, or you may mistype it. In such situation __getattr__ is being called. It collects the class method names, removed the useless ones, and performs a similarity search between the given wrong method name and the strings in that list. Then returns the 4-5 most similar ones, each one followed by their docstring, that shows the function signature and the purpose of the method. So you can usually find the method you were looking for, and use it. This is useful both when using such Graph class from the command line and when you are writing code without an IDE that helps you finding method names. That Python code of mine becomes doable in D too (using for example my very fast approximate string distance. This is a case where you don't need the list of changes, but just the distance number. So superdan can rest in peace). Bye, bearophile

There's an interesting idea... Instead of "No member 'foo'", you could have "No member 'foo'; did you mean 'far' or 'fur'?" -- Daniel

Heh. The string kernels in std.numeric (http://erdani.dreamhosters.com/d/web/phobos/std_numeric.html) are to help with exactly that. Andrei
Apr 18 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Andrei Alexandrescu wrote:
 Daniel Keep wrote:
 There's an interesting idea...

 Instead of "No member 'foo'", you could have "No member 'foo'; did you
 mean 'far' or 'fur'?"

Heh. The string kernels in std.numeric (http://erdani.dreamhosters.com/d/web/phobos/std_numeric.html) are to help with exactly that.

I remember, but can't find, some famous programmer posted to the net a simple spell checking algorithm that worked by counting letter additions, deletions, and transpositions to determine best matches.
Apr 18 2009
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Walter Bright wrote:
 Andrei Alexandrescu wrote:
 Daniel Keep wrote:
 There's an interesting idea...

 Instead of "No member 'foo'", you could have "No member 'foo'; did you
 mean 'far' or 'fur'?"

Heh. The string kernels in std.numeric (http://erdani.dreamhosters.com/d/web/phobos/std_numeric.html) are to help with exactly that.

I remember, but can't find, some famous programmer posted to the net a simple spell checking algorithm that worked by counting letter additions, deletions, and transpositions to determine best matches.

I don't know who the famous programmer was but it sounds like he was referring to one of the edit distances. std.algorithm has an implementation of the popular Levenshtein distance. http://erdani.dreamhosters.com/d/web/phobos/std_algorithm.html#levenshteinDistance String kernels go way beyond that because they count the weighted length of all gapped substrings common to two strings. http://erdani.dreamhosters.com/d/web/phobos/std_numeric.html#gapWeightedSimilarity With such a similarity, more subtle errors can be detected. Andrei
Apr 18 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
A simple command line spell checker would be a cool demonstration of this!
Apr 18 2009
parent Georg Wrede <georg.wrede iki.fi> writes:
Walter Bright wrote:
 A simple command line spell checker would be a cool demonstration of this!

$ man bash /cdspell If set, minor errors in the spelling of a directory com- ponent in a cd command will be corrected. The errors checked for are transposed characters, a missing charac- ter, and one character too many. If a correction is found, the corrected file name is printed, and the com- mand proceeds. This option is only used by interactive shells.
Apr 18 2009
prev sibling parent Leandro Lucarella <llucax gmail.com> writes:
Daniel Keep, el 18 de abril a las 19:08 me escribiste:
 So I have created this, that I actually use in a large Graph class of mine
that has many methods:
 http://code.activestate.com/recipes/409000/
 Such class can be used from the interactive shell too, to play with graphs in
an interactive way, modify them, plot them, etc.
 Often you may not remember the name of a method you need, or you may
 mistype it. In such situation __getattr__ is being called. It collects
 the class method names, removed the useless ones, and performs
 a similarity search between the given wrong method name and the
 strings in that list. Then returns the 4-5 most similar ones, each one
 followed by their docstring, that shows the function signature and the
 purpose of the method. So you can usually find the method you were
 looking for, and use it. This is useful both when using such Graph
 class from the command line and when you are writing code without an
 IDE that helps you finding method names.  That Python code of mine
 becomes doable in D too (using for example my very fast approximate
 string distance. This is a case where you don't need the list of
 changes, but just the distance number. So superdan can rest in peace).
 
 Bye,
 bearophile

There's an interesting idea... Instead of "No member 'foo'", you could have "No member 'foo'; did you mean 'far' or 'fur'?"

Git[1] added that feature for Git commands recently and even when it's not really *that* useful, is a nice feature =) Maybe something like "candidates are: ..." following by each method and file location (to empower IDEs) can be done. I mean, the DMD frontend could do that, not opDot(Exp), of course ;) -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- Software is like sex: it's better when it's free. -- Linus Torvalds
Apr 18 2009