Go to the documentation of this file.00001 """Miscellaneous utility functions."""
00002
00003 from __future__ import absolute_import, division, with_statement
00004
00005
00006 class ObjectDict(dict):
00007 """Makes a dictionary behave like an object."""
00008 def __getattr__(self, name):
00009 try:
00010 return self[name]
00011 except KeyError:
00012 raise AttributeError(name)
00013
00014 def __setattr__(self, name, value):
00015 self[name] = value
00016
00017
00018 def import_object(name):
00019 """Imports an object by name.
00020
00021 import_object('x.y.z') is equivalent to 'from x.y import z'.
00022
00023 >>> import tornado.escape
00024 >>> import_object('tornado.escape') is tornado.escape
00025 True
00026 >>> import_object('tornado.escape.utf8') is tornado.escape.utf8
00027 True
00028 """
00029 parts = name.split('.')
00030 obj = __import__('.'.join(parts[:-1]), None, None, [parts[-1]], 0)
00031 return getattr(obj, parts[-1])
00032
00033
00034
00035
00036
00037
00038
00039 if str is unicode:
00040 def b(s):
00041 return s.encode('latin1')
00042 bytes_type = bytes
00043 else:
00044 def b(s):
00045 return s
00046 bytes_type = str
00047
00048
00049 def raise_exc_info(exc_info):
00050 """Re-raise an exception (with original traceback) from an exc_info tuple.
00051
00052 The argument is a ``(type, value, traceback)`` tuple as returned by
00053 `sys.exc_info`.
00054 """
00055
00056
00057 if isinstance(exc_info[1], exc_info[0]):
00058 raise exc_info[1], None, exc_info[2]
00059
00060 else:
00061
00062
00063 raise exc_info[0], exc_info[1], exc_info[2]
00064
00065
00066
00067 def doctests():
00068 import doctest
00069 return doctest.DocTestSuite()