tornado.util — General-purpose utilities¶
Miscellaneous utility functions and classes.
This module is used internally by Tornado. It is not necessarily expected that the functions and classes defined here will be useful to other applications, but they are documented here in case they are.
Exception raised by
Changed in version 5.0:: Unified
tornado.util.TimeoutError. Both former names remain as aliases.
Makes a dictionary behave like an object, with attribute-style access.
Streaming gzip decompressor.
The interface is like that of
zlib.decompressobj(without some of the optional arguments, but it understands gzip headers and checksums.
decompress(value: bytes, max_length: int = 0) → bytes¶
Decompress a chunk, returning newly-available data.
Some data may be buffered for later processing;
flushmust be called when there is no more input data to ensure that all data was processed.
max_lengthis given, some input data may be left over in
unconsumed_tail; you must retrieve this value and pass it back to a future call to
decompressif it is not empty.
Returns the unconsumed portion left over
import_object(name: str) → Any¶
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
errno_from_exception(e: BaseException) → Optional[int]¶
Provides the errno from an Exception object.
There are cases that the errno attribute was not set so we pull the errno out of the args but if someone instantiates an Exception without any args you will get a tuple error. So this function abstracts all that behavior to give you a safe way to get the errno.
re_unescape(s: str) → str¶
Unescape a string escaped by
ValueErrorfor regular expressions which could not have been produced by
re.escape(for example, strings containing
\dcannot be unescaped).
New in version 4.4.
Base class for configurable interfaces.
A configurable interface is an (abstract) class whose constructor acts as a factory function for one of its implementation subclasses. The implementation subclass as well as optional keyword arguments to its initializer can be set globally at runtime with
By using the constructor as the factory method, the interface looks like a normal class,
isinstanceworks as usual, etc. This pattern is most useful when the choice of implementation is likely to be a global decision (e.g. when
epollis available, always use it instead of
select), or when a previously-monolithic class has been split into specialized subclasses.
Changed in version 5.0: It is now possible for configuration to be specified at multiple levels of a class hierarchy.
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).
Returns the implementation class to be used if none is configured.
initialize() → None¶
Configurable classes should use
When used with
mypy, subclasses will often need
# type: ignoreannotations on this method because
mypydoes not recognize that its arguments may change in subclasses (as it does for
Changed in version 4.2: Now accepts positional arguments in addition to keyword arguments.
Sets the class to use when the base class is instantiated.
Keyword arguments will be saved and added to the arguments passed to the constructor. This can be used to set global defaults for some parameters.
Returns the currently configured class.
ArgReplacer(func: Callable, name: str)¶
Replaces one value in an
Inspects the function signature to find an argument by name whether it is passed by position or keyword. For use in decorators and similar wrappers.
get_old_value(args: Sequence[Any], kwargs: Dict[str, Any], default: Any = None) → Any¶
Returns the old value of the named argument without replacing it.
defaultif the argument is not present.
replace(new_value: Any, args: Sequence[Any], kwargs: Dict[str, Any]) → Tuple[Any, Sequence[Any], Dict[str, Any]]¶
Replace the named argument in
(old_value, args, kwargs). The returned
kwargsobjects may not be the same as the input objects, or the input objects may be mutated.
If the named argument was not found,
new_valuewill be added to
kwargsand None will be returned as
Equivalent to td.total_seconds() (introduced in python 2.7).