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