This is found in the python standard library, as unittest.TestLoader.loadTestsFromName. Unfortunately, the method goes on to do additional test-related activities, but this first ha looks re-usable. We have edited it to remove the test-related functionality:
def get_object(name):
"""Retrieve a python object, given its dotted.name."""
parts = name.split('.')
parts_copy = parts[:]
while parts_copy:
try:
module = __import__('.'.join(parts_copy))
break
except ImportError:
del parts_copy[-1]
if not parts_copy: raise
parts = parts[1:]
obj = module
for part in parts:
parent, obj = obj, getattr(obj, part)
return obj