]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a utility function to import an object by name.
authorBen Darnell <ben@bendarnell.com>
Wed, 23 Feb 2011 22:22:23 +0000 (14:22 -0800)
committerBen Darnell <ben@bendarnell.com>
Wed, 23 Feb 2011 22:22:23 +0000 (14:22 -0800)
tornado/test/runtests.py
tornado/util.py [new file with mode: 0644]

index 64ad78c1de3ae8c4d995a3d1c1223048b5fbb8a1..78e5d5e7cbd806ffca2a93a3301bb227f5980f08 100755 (executable)
@@ -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 (file)
index 0000000..3706b16
--- /dev/null
@@ -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()