Mercurial > pylearn
changeset 597:c4579524baa6
revised newstuff.resolve to do more importing
author | james@X40 |
---|---|
date | Tue, 13 Jan 2009 16:00:24 -0500 |
parents | 3f9ec536f2c1 |
children | e763711472a5 |
files | pylearn/dbdict/newstuff.py |
diffstat | 1 files changed, 13 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/pylearn/dbdict/newstuff.py Fri Dec 19 16:46:51 2008 -0500 +++ b/pylearn/dbdict/newstuff.py Tue Jan 13 16:00:24 2009 -0500 @@ -26,11 +26,22 @@ ### resolve ################################################################################ -def resolve(name): +def resolve(name, try_import=True): + """ + Resolve a string of the form X.Y...Z to a python object by repeatedly using getattr, and + __import__ to introspect objects (in this case X, then Y, etc. until finally Z is loaded). + """ symbols = name.split('.') builder = __import__(symbols[0]) for sym in symbols[1:]: - builder = getattr(builder, sym) + try: + builder = getattr(builder, sym) + except AttributeError, e: + if try_import: + __import__(builder.__name__, fromlist=[sym]) + builder = getattr(builder, sym) + else: + raise e return builder ################################################################################