From: Ben Darnell Date: Wed, 23 Feb 2011 22:22:23 +0000 (-0800) Subject: Add a utility function to import an object by name. X-Git-Tag: v2.0.0~124 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a785a128d5cf79480234c970e06f68f8de97580b;p=thirdparty%2Ftornado.git Add a utility function to import an object by name. --- diff --git a/tornado/test/runtests.py b/tornado/test/runtests.py index 64ad78c1d..78e5d5e7c 100755 --- a/tornado/test/runtests.py +++ b/tornado/test/runtests.py @@ -4,6 +4,7 @@ import unittest TEST_MODULES = [ 'tornado.httputil.doctests', 'tornado.iostream.doctests', + 'tornado.util.doctests', 'tornado.test.escape_test', 'tornado.test.httpserver_test', 'tornado.test.ioloop_test', diff --git a/tornado/util.py b/tornado/util.py new file mode 100644 index 000000000..3706b1610 --- /dev/null +++ b/tornado/util.py @@ -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()