##// END OF EJS Templates
use getattr in import_item, shim_module...
Min RK -
Show More
@@ -1,22 +1,11 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 A simple utility to import something by its string name.
3 A simple utility to import something by its string name.
4
5 Authors:
6
7 * Brian Granger
8 """
4 """
9
5
10 #-----------------------------------------------------------------------------
6 # Copyright (c) IPython Development Team.
11 # Copyright (C) 2008-2011 The IPython Development Team
7 # Distributed under the terms of the Modified BSD License.
12 #
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
16
8
17 #-----------------------------------------------------------------------------
18 # Functions and classes
19 #-----------------------------------------------------------------------------
20
9
21 def import_item(name):
10 def import_item(name):
22 """Import and return ``bar`` given the string ``foo.bar``.
11 """Import and return ``bar`` given the string ``foo.bar``.
@@ -41,8 +30,8 b' def import_item(name):'
41 package, obj = parts
30 package, obj = parts
42 module = __import__(package, fromlist=[obj])
31 module = __import__(package, fromlist=[obj])
43 try:
32 try:
44 pak = module.__dict__[obj]
33 pak = getattr(module, obj)
45 except KeyError:
34 except AttributeError:
46 raise ImportError('No module named %s' % obj)
35 raise ImportError('No module named %s' % obj)
47 return pak
36 return pak
48 else:
37 else:
@@ -15,7 +15,7 b' class ShimModule(types.ModuleType):'
15 # Use the equivalent of import_item(name), see below
15 # Use the equivalent of import_item(name), see below
16 name = "%s.%s" % (self._mirror, key)
16 name = "%s.%s" % (self._mirror, key)
17
17
18 # NOTE: the code below is copied *verbatim* from
18 # NOTE: the code below was copied *verbatim* from
19 # importstring.import_item. For some very strange reason that makes no
19 # importstring.import_item. For some very strange reason that makes no
20 # sense to me, if we call it *as a function*, it doesn't work. This
20 # sense to me, if we call it *as a function*, it doesn't work. This
21 # has something to do with the deep bowels of the import machinery and
21 # has something to do with the deep bowels of the import machinery and
@@ -33,11 +33,7 b' class ShimModule(types.ModuleType):'
33 # called with 'foo.bar....'
33 # called with 'foo.bar....'
34 package, obj = parts
34 package, obj = parts
35 module = __import__(package, fromlist=[obj])
35 module = __import__(package, fromlist=[obj])
36 try:
36 return getattr(module, obj)
37 pak = module.__dict__[obj]
38 except KeyError:
39 raise AttributeError(obj)
40 return pak
41 else:
37 else:
42 # called with un-dotted string
38 # called with un-dotted string
43 return __import__(parts[0])
39 return __import__(parts[0])
General Comments 0
You need to be logged in to leave comments. Login now