### Navigation
- [index](# "General Index")
- [modules](# "Python Module Index") |
- [next](# "Frequently Asked Questions") |
- [previous](# "tornado.testing — Unit testing support for asynchronous code") |
- [Tornado 4.4.dev1 documentation](#) »
- [Utilities](#) »
# `tornado.util` — General-purpose utilities
Miscellaneous utility functions and classes.
This module is used internally by Tornado. It is not necessarily expectedthat the functions and classes defined here will be useful to otherapplications, but they are documented here in case they are.
The one public-facing part of this module is the [`Configurable`](# "tornado.util.Configurable") classand its [`configure`](# "tornado.util.Configurable.configure") method, which becomes a part of theinterface of its subclasses, including [`AsyncHTTPClient`](# "tornado.httpclient.AsyncHTTPClient"), [`IOLoop`](# "tornado.ioloop.IOLoop"),and [`Resolver`](# "tornado.netutil.Resolver").
*class *`tornado.util.``ObjectDict`[[source]](#)
Makes a dictionary behave like an object, with attribute-style access.
*class *`tornado.util.``GzipDecompressor`[[source]](#)
Streaming gzip decompressor.
The interface is like that of [`zlib.decompressobj`](https://docs.python.org/3.4/library/zlib.html#zlib.decompressobj "(in Python v3.4)") [https://docs.python.org/3.4/library/zlib.html#zlib.decompressobj] (without some of theoptional arguments, but it understands gzip headers and checksums.
`decompress`(*value*, *max_length=None*)[[source]](#)
Decompress a chunk, returning newly-available data.
Some data may be buffered for later processing; [`flush`](# "tornado.util.GzipDecompressor.flush") mustbe called when there is no more input data to ensure thatall data was processed.
If `max_length` is given, some input data may be left overin `unconsumed_tail`; you must retrieve this value and passit back to a future call to [`decompress`](# "tornado.util.GzipDecompressor.decompress") if it is not empty.
`unconsumed_tail`
Returns the unconsumed portion left over
`flush`()[[source]](#)
Return any remaining buffered data not yet returned by decompress.
Also checks for errors such as truncated input.No other methods may be called on this object after [`flush`](# "tornado.util.GzipDecompressor.flush").
`tornado.util.``import_object`(*name*)[[source]](#)
Imports an object by name.
import_object(‘x') is equivalent to ‘import x'.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
>>> import_object('tornado') is tornado
True
>>> import_object('tornado.missing_module')
Traceback (most recent call last):
...
ImportError: No module named missing_module
~~~
`tornado.util.``errno_from_exception`(*e*)[[source]](#)
Provides the errno from an Exception object.
There are cases that the errno attribute was not set so we pullthe errno out of the args but if someone instantiates an Exceptionwithout any args you will get a tuple error. So this functionabstracts all that behavior to give you a safe way to get theerrno.
`tornado.util.``re_unescape`(*s*)[[source]](#)
Unescape a string escaped by [`re.escape`](https://docs.python.org/3.4/library/re.html#re.escape "(in Python v3.4)") [https://docs.python.org/3.4/library/re.html#re.escape].
May raise `ValueError` for regular expressions which could nothave been produced by [`re.escape`](https://docs.python.org/3.4/library/re.html#re.escape "(in Python v3.4)") [https://docs.python.org/3.4/library/re.html#re.escape] (for example, strings containing`\d` cannot be unescaped).
New in version 4.4.
*class *`tornado.util.``Configurable`[[source]](#)
Base class for configurable interfaces.
A configurable interface is an (abstract) class whose constructoracts as a factory function for one of its implementation subclasses.The implementation subclass as well as optional keyword arguments toits initializer can be set globally at runtime with [`configure`](# "tornado.util.Configurable.configure").
By using the constructor as the factory method, the interfacelooks like a normal class, [`isinstance`](https://docs.python.org/3.4/library/functions.html#isinstance "(in Python v3.4)") [https://docs.python.org/3.4/library/functions.html#isinstance] works as usual, etc. Thispattern is most useful when the choice of implementation is likelyto be a global decision (e.g. when [`epoll`](https://docs.python.org/3.4/library/select.html#select.epoll "(in Python v3.4)") [https://docs.python.org/3.4/library/select.html#select.epoll] is available,always use it instead of [`select`](https://docs.python.org/3.4/library/select.html#select.select "(in Python v3.4)") [https://docs.python.org/3.4/library/select.html#select.select]), or when apreviously-monolithic class has been split into specializedsubclasses.
Configurable subclasses must define the class methods[`configurable_base`](# "tornado.util.Configurable.configurable_base") and [`configurable_default`](# "tornado.util.Configurable.configurable_default"), and use the instancemethod [`initialize`](# "tornado.util.Configurable.initialize") instead of `__init__`.
*classmethod *`configurable_base`()[[source]](#)
Returns the base class of a configurable hierarchy.
This will normally return the class in which it is defined.(which is *not* necessarily the same as the cls classmethod parameter).
*classmethod *`configurable_default`()[[source]](#)
Returns the implementation class to be used if none is configured.
`initialize`()[[source]](#)
Initialize a [`Configurable`](# "tornado.util.Configurable") subclass instance.
Configurable classes should use [`initialize`](# "tornado.util.Configurable.initialize") instead of `__init__`.
Changed in version 4.2: Now accepts positional arguments in addition to keyword arguments.
*classmethod *`configure`(*impl*, ***kwargs*)[[source]](#)
Sets the class to use when the base class is instantiated.
Keyword arguments will be saved and added to the arguments passedto the constructor. This can be used to set global defaults forsome parameters.
*classmethod *`configured_class`()[[source]](#)
Returns the currently configured class.
*class *`tornado.util.``ArgReplacer`(*func*, *name*)[[source]](#)
Replaces one value in an `args, kwargs` pair.
Inspects the function signature to find an argument by namewhether it is passed by position or keyword. For use in decoratorsand similar wrappers.
`get_old_value`(*args*, *kwargs*, *default=None*)[[source]](#)
Returns the old value of the named argument without replacing it.
Returns `default` if the argument is not present.
`replace`(*new_value*, *args*, *kwargs*)[[source]](#)
Replace the named argument in `args, kwargs` with `new_value`.
Returns `(old_value, args, kwargs)`. The returned `args` and`kwargs` objects may not be the same as the input objects, orthe input objects may be mutated.
If the named argument was not found, `new_value` will be addedto `kwargs` and None will be returned as `old_value`.
`tornado.util.``timedelta_to_seconds`(*td*)[[source]](#)
Equivalent to td.total_seconds() (introduced in python 2.7).
© Copyright 2009-2016, The Tornado Authors. Created using [Sphinx](http://sphinx-doc.org/) 1.3.5.
- User's guide
- Introduction
- Asynchronous and non-Blocking I/O
- Coroutines
- Queue example - a concurrent web spider
- Structure of a Tornado web application
- Templates and UI
- Authentication and security
- Running and deploying
- Web framework
- tornado.web — RequestHandler and Application classes
- tornado.template — Flexible output generation
- tornado.escape — Escaping and string manipulation
- tornado.locale — Internationalization support
- tornado.websocket — Bidirectional communication to the browser
- HTTP servers and clients
- tornado.httpserver — Non-blocking HTTP server
- tornado.httpclient — Asynchronous HTTP client
- tornado.httputil — Manipulate HTTP headers and URLs
- tornado.http1connection – HTTP/1.x client/server implementation
- Asynchronous networking
- tornado.ioloop — Main event loop
- tornado.iostream — Convenient wrappers for non-blocking sockets
- tornado.netutil — Miscellaneous network utilities
- tornado.tcpclient — IOStream connection factory
- tornado.tcpserver — Basic IOStream-based TCP server
- Coroutines and concurrency
- tornado.gen — Simplify asynchronous code
- tornado.concurrent — Work with threads and futures
- tornado.locks – Synchronization primitives
- tornado.queues – Queues for coroutines
- tornado.process — Utilities for multiple processes
- Integration with other services
- tornado.auth — Third-party login with OpenID and OAuth
- tornado.wsgi — Interoperability with other Python frameworks and servers
- tornado.platform.asyncio — Bridge between asyncio and Tornado
- tornado.platform.caresresolver — Asynchronous DNS Resolver using C-Ares
- tornado.platform.twisted — Bridges between Twisted and Tornado
- Utilities
- tornado.autoreload — Automatically detect code changes in development
- tornado.log — Logging support
- tornado.options — Command-line parsing
- tornado.stack_context — Exception handling across asynchronous callbacks
- tornado.testing — Unit testing support for asynchronous code
- tornado.util — General-purpose utilities
- Frequently Asked Questions
- Release notes
- What's new in Tornado 4.3
- What's new in Tornado 4.2.1
- What's new in Tornado 4.2
- What's new in Tornado 4.1
- What's new in Tornado 4.0.2
- What's new in Tornado 4.0.1
- What's new in Tornado 4.0
- What's new in Tornado 3.2.2
- What's new in Tornado 3.2.1
- What's new in Tornado 3.2
- What's new in Tornado 3.1.1
- What's new in Tornado 3.1
- What's new in Tornado 3.0.2
- What's new in Tornado 3.0.1
- What's new in Tornado 3.0
- What's new in Tornado 2.4.1
- What's new in Tornado 2.4
- What's new in Tornado 2.3
- What's new in Tornado 2.2.1
- What's new in Tornado 2.2
- What's new in Tornado 2.1.1
- What's new in Tornado 2.1
- What's new in Tornado 2.0
- What's new in Tornado 1.2.1
- What's new in Tornado 1.2
- What's new in Tornado 1.1.1
- What's new in Tornado 1.1
- What's new in Tornado 1.0.1
- What's new in Tornado 1.0