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
classand its configure
method, which becomes a part of theinterface of its subclasses, including AsyncHTTPClient
, IOLoop
,and Resolver
.
- class
tornado.util.
ObjectDict
[源代码]¶
Makes a dictionary behave like an object, with attribute-style access.
- class
tornado.util.
GzipDecompressor
[源代码]¶
Streaming gzip decompressor.
The interface is like that ofzlib.decompressobj
(without some of theoptional arguments, but it understands gzip headers and checksums.decompress
(value, max_length=None)[源代码]¶
Decompress a chunk, returning newly-available data.
Some data may be buffered for later processing;flush
mustbe called when there is no more input data to ensure thatall data was processed.
Ifmaxlength
is given, some input data may be left overinunconsumed_tail
; you must retrieve this value and passit back to a future call todecompress
if it is not empty.
unconsumed_tail
¶
Returns the unconsumed portion left over
tornado.util.
import_object
(_name)[源代码]¶
Imports an object by name.
importobject(‘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
- >>> import tornado.escape
tornado.util.
errno_from_exception
(_e)[源代码]¶
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.
reunescape
(_s)[源代码]¶
Unescape a string escaped byre.escape
.
May raiseValueError
for regular expressions which could nothave been produced byre.escape
(for example, strings containing\d
cannot be unescaped).
4.4 新版功能.
- class
tornado.util.
Configurable
[源代码]¶
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 withconfigure
.
By using the constructor as the factory method, the interfacelooks like a normal class,isinstance
works as usual, etc. Thispattern is most useful when the choice of implementation is likelyto be a global decision (e.g. whenepoll
is available,always use it instead ofselect
), or when apreviously-monolithic class has been split into specializedsubclasses.
Configurable subclasses must define the class methodsconfigurable_base
andconfigurable_default
, and use the instancemethodinitialize
instead ofinit
.- classmethod
configurablebase
()[源代码]¶
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
configurabledefault
()[源代码]¶
Returns the implementation class to be used if none is configured.
initialize
()[源代码]¶
Initialize aConfigurable
subclass instance.
Configurable classes should useinitialize
instead ofinit
.
在 4.2 版更改: Now accepts positional arguments in addition to keyword arguments.
- classmethod
- _class
tornado.util.
ArgReplacer
(func, name)[源代码]¶
Replaces one value in anargs, 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.getold_value
(_args, kwargs, default=None)[源代码]¶
Returns the old value of the named argument without replacing it.
Returnsdefault
if the argument is not present.
replace
(new_value, args, kwargs)[源代码]¶
Replace the named argument inargs, kwargs
withnewvalue
.
Returns(old_value, args, kwargs)
. The returnedargs
andkwargs
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 addedtokwargs
and None will be returned asold_value
.
tornado.util.
timedelta_to_seconds
(_td)[源代码]¶
Equivalent to td.total_seconds() (introduced in python 2.7).
原文: