Ondrej Certik wrote: > On Mon, Mar 16, 2009 at 8:13 AM, Fabian Seoane <fab...@fseoane.net> wrote: > >> Function get_class returns a class from a given string. It makes use >> of get_mod_func >> --- >> sympy/utilities/source.py | 26 ++++++++++++++++++++++++++ >> 1 files changed, 26 insertions(+), 0 deletions(-) >> >> diff --git a/sympy/utilities/source.py b/sympy/utilities/source.py >> index 56ca7f7..1470759 100644 >> --- a/sympy/utilities/source.py >> +++ b/sympy/utilities/source.py >> @@ -10,3 +10,29 @@ def source(object): >> """ >> print 'In file: %s' % inspect.getsourcefile(object) >> print inspect.getsource(object) >> + >> +def get_class(lookup_view): >> + """ >> + Convert a string version of a class name to the object. >> + >> + This is used by the query method >> + """ >> + if isinstance(lookup_view, str): >> + # Bail early for non-ASCII strings (they can't be functions). >> + lookup_view = lookup_view.encode('ascii') >> + mod_name, func_name = get_mod_func(lookup_view) >> + if func_name != '': >> + lookup_view = getattr(__import__(mod_name, {}, {}, ['']), >> func_name) >> + if not callable(lookup_view): >> + raise AttributeError("'%s.%s' is not a callable." % >> (mod_name, func_name)) >> + return lookup_view >> + >> + >> +def get_mod_func(callback): >> + # Converts 'sympy.core.query.QueryIntegerHandler' to >> + # ['sympy.core.query', 'QueryIntegerHandler'] >> + try: >> + dot = callback.rindex('.') >> + except ValueError: >> + return callback, '' >> + return callback[:dot], callback[dot+1:] >> > > Could there be tests for both of the functions above? It's not exactly > clear what they are doing from the docstrings. Also code like this is > really hackish: > > getattr(__import__(mod_name, {}, {}, ['']), func_name) >
yeah, I know it looks hackish, but it is copied from a similar utility in the django codebase, so i'm quite sure that there isn't a better way of doing it It sure lacks docstrings and tests, so I'll send a reworked patch or this and the other modules soon. Thanks for the review, > If this is needed, I think it deserves a thorough docstrings and > comments why this is needed and what it does. > > Ondrej > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy-patches" group. To post to this group, send email to sympy-patches@googlegroups.com To unsubscribe from this group, send email to sympy-patches+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sympy-patches?hl=en -~----------~----~----~----~------~----~------~--~---