changeset 598:e763711472a5

merged
author james@X40
date Tue, 13 Jan 2009 16:00:36 -0500
parents c4579524baa6 (diff) 605ab704abc3 (current diff)
children bd777e960c7c
files pylearn/dbdict/newstuff.py
diffstat 1 files changed, 13 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/dbdict/newstuff.py	Mon Jan 12 18:47:10 2009 -0500
+++ b/pylearn/dbdict/newstuff.py	Tue Jan 13 16:00:36 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
 
 ################################################################################