##// END OF EJS Templates
Restructure code to avoid unnecessary list slicing by using rsplit.
Fernando Perez -
Show More
@@ -34,29 +34,17 b' def import_item(name):'
34 mod : module object
34 mod : module object
35 The module that was imported.
35 The module that was imported.
36 """
36 """
37
38 package = '.'.join(name.split('.')[0:-1])
39 obj = name.split('.')[-1]
40
37
41 # Note: the original code for this was the following. We've left it
38 parts = name.rsplit('.', 1)
42 # visible for now in case the new implementation shows any problems down
39 if len(parts) == 2:
43 # the road, to make it easier on anyone looking for a problem. This code
40 # called with 'foo.bar....'
44 # should be removed once we're comfortable we didn't break anything.
41 package, obj = parts
45
42 module = __import__(package, fromlist=[obj])
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])
56 try:
43 try:
57 pak = module.__dict__[obj]
44 pak = module.__dict__[obj]
58 except KeyError:
45 except KeyError:
59 raise ImportError('No module named %s' % obj)
46 raise ImportError('No module named %s' % obj)
60 return pak
47 return pak
61 else:
48 else:
62 return __import__(obj)
49 # called with un-dotted string
50 return __import__(parts[0])
General Comments 0
You need to be logged in to leave comments. Login now