Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ddfcf4

Browse files
fperezminrk
authored andcommitted
Restructure code to avoid unnecessary list slicing by using rsplit.
1 parent 8ce76a8 commit 8ddfcf4

1 file changed

Lines changed: 7 additions & 19 deletions

File tree

IPython/utils/importstring.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,17 @@ def import_item(name):
3434
mod : module object
3535
The module that was imported.
3636
"""
37-
38-
package = '.'.join(name.split('.')[0:-1])
39-
obj = name.split('.')[-1]
4037

41-
# Note: the original code for this was the following. We've left it
42-
# visible for now in case the new implementation shows any problems down
43-
# the road, to make it easier on anyone looking for a problem. This code
44-
# should be removed once we're comfortable we didn't break anything.
45-
46-
## execString = 'from %s import %s' % (package, obj)
47-
## try:
48-
## exec execString
49-
## except SyntaxError:
50-
## raise ImportError("Invalid class specification: %s" % name)
51-
## exec 'temp = %s' % obj
52-
## return temp
53-
54-
if package:
55-
module = __import__(package,fromlist=[obj])
38+
parts = name.rsplit('.', 1)
39+
if len(parts) == 2:
40+
# called with 'foo.bar....'
41+
package, obj = parts
42+
module = __import__(package, fromlist=[obj])
5643
try:
5744
pak = module.__dict__[obj]
5845
except KeyError:
5946
raise ImportError('No module named %s' % obj)
6047
return pak
6148
else:
62-
return __import__(obj)
49+
# called with un-dotted string
50+
return __import__(parts[0])

0 commit comments

Comments
 (0)