Skip to content

Commit

Permalink
Add a utility function to import an object by name.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdarnell committed Feb 23, 2011
1 parent bfb171f commit a785a12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions tornado/test/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
TEST_MODULES = [
'tornado.httputil.doctests',
'tornado.iostream.doctests',
'tornado.util.doctests',
'tornado.test.escape_test',
'tornado.test.httpserver_test',
'tornado.test.ioloop_test',
Expand Down
20 changes: 20 additions & 0 deletions tornado/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Miscellaneous utility functions."""

def import_object(name):
"""Imports an object by name.
import_object('x.y.z') is equivalent to 'from x.y import z'.
>>> import tornado.escape
>>> import_object('tornado.escape') is tornado.escape
True
>>> import_object('tornado.escape.utf8') is tornado.escape.utf8
True
"""
parts = name.split('.')
obj = __import__('.'.join(parts[:-1]), None, None, [parts[-1]], 0)
return getattr(obj, parts[-1])

def doctests():
import doctest
return doctest.DocTestSuite()

0 comments on commit a785a12

Please sign in to comment.