API documentation¶
asyncio
¶
The asyncio package, tracking PEP 3156.
- class asyncio.AbstractEventLoop[source]¶
Bases:
object
Abstract event loop.
- close()[source]¶
Close the loop.
The loop should not be running.
This is idempotent and irreversible.
No other methods should be called after this one.
- async connect_read_pipe(protocol_factory, pipe)[source]¶
Register read pipe in event loop. Set the pipe to non-blocking mode.
protocol_factory should instantiate object with Protocol interface. pipe is a file-like object. Return pair (transport, protocol), where transport supports the ReadTransport interface.
- async connect_write_pipe(protocol_factory, pipe)[source]¶
Register write pipe in event loop.
protocol_factory should instantiate object with BaseProtocol interface. Pipe is file-like object already switched to nonblocking. Return pair (transport, protocol), where transport support WriteTransport interface.
- async create_datagram_endpoint(protocol_factory, local_addr=None, remote_addr=None, *, family=0, proto=0, flags=0, reuse_address=None, reuse_port=None, allow_broadcast=None, sock=None)[source]¶
A coroutine which creates a datagram endpoint.
This method will try to establish the endpoint in the background. When successful, the coroutine returns a (transport, protocol) pair.
protocol_factory must be a callable returning a protocol instance.
socket family AF_INET, socket.AF_INET6 or socket.AF_UNIX depending on host (or family if specified), socket type SOCK_DGRAM.
reuse_address tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire. If not specified it will automatically be set to True on UNIX.
reuse_port tells the kernel to allow this endpoint to be bound to the same port as other existing endpoints are bound to, so long as they all set this flag when being created. This option is not supported on Windows and some UNIX’s. If the
SO_REUSEPORT
constant is not defined then this capability is unsupported.allow_broadcast tells the kernel to allow this endpoint to send messages to the broadcast address.
sock can optionally be specified in order to use a preexisting socket object.
- async create_server(protocol_factory, host=None, port=None, *, family=AddressFamily.AF_UNSPEC, flags=AddressInfo.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None, reuse_port=None, ssl_handshake_timeout=None, start_serving=True)[source]¶
A coroutine which creates a TCP server bound to host and port.
The return value is a Server object which can be used to stop the service.
If host is an empty string or None all interfaces are assumed and a list of multiple sockets will be returned (most likely one for IPv4 and another one for IPv6). The host parameter can also be a sequence (e.g. list) of hosts to bind to.
family can be set to either AF_INET or AF_INET6 to force the socket to use IPv4 or IPv6. If not set it will be determined from host (defaults to AF_UNSPEC).
flags is a bitmask for getaddrinfo().
sock can optionally be specified in order to use a preexisting socket object.
backlog is the maximum number of queued connections passed to listen() (defaults to 100).
ssl can be set to an SSLContext to enable SSL over the accepted connections.
reuse_address tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire. If not specified will automatically be set to True on UNIX.
reuse_port tells the kernel to allow this endpoint to be bound to the same port as other existing endpoints are bound to, so long as they all set this flag when being created. This option is not supported on Windows.
ssl_handshake_timeout is the time in seconds that an SSL server will wait for completion of the SSL handshake before aborting the connection. Default is 60s.
start_serving set to True (default) causes the created server to start accepting connections immediately. When set to False, the user should await Server.start_serving() or Server.serve_forever() to make the server to start accepting connections.
- async create_unix_server(protocol_factory, path=None, *, sock=None, backlog=100, ssl=None, ssl_handshake_timeout=None, start_serving=True)[source]¶
A coroutine which creates a UNIX Domain Socket server.
The return value is a Server object, which can be used to stop the service.
path is a str, representing a file system path to bind the server socket to.
sock can optionally be specified in order to use a preexisting socket object.
backlog is the maximum number of queued connections passed to listen() (defaults to 100).
ssl can be set to an SSLContext to enable SSL over the accepted connections.
ssl_handshake_timeout is the time in seconds that an SSL server will wait for the SSL handshake to complete (defaults to 60s).
start_serving set to True (default) causes the created server to start accepting connections immediately. When set to False, the user should await Server.start_serving() or Server.serve_forever() to make the server to start accepting connections.
- run_until_complete(future)[source]¶
Run the event loop until a Future is done.
Return the Future’s result, or raise its exception.
- async sendfile(transport, file, offset=0, count=None, *, fallback=True)[source]¶
Send a file through a transport.
Return an amount of sent bytes.
- asyncio.gather(*coros_or_futures, loop=None, return_exceptions=False)[source]¶
Return a future aggregating results from the given coroutines/futures.
Coroutines will be wrapped in a future and scheduled in the event loop. They will not necessarily be scheduled in the same order as passed in.
All futures must share the same event loop. If all the tasks are done successfully, the returned future’s result is the list of results (in the order of the original sequence, not necessarily the order of results arrival). If return_exceptions is True, exceptions in the tasks are treated the same as successful results, and gathered in the result list; otherwise, the first raised exception will be immediately propagated to the returned future.
Cancellation: if the outer Future is cancelled, all children (that have not completed yet) are also cancelled. If any child is cancelled, this is treated as if it raised CancelledError – the outer Future is not cancelled in this case. (This is to prevent the cancellation of one child to cause other children to be cancelled.)
If return_exceptions is False, cancelling gather() after it has been marked done won’t cancel any submitted awaitables. For instance, gather can be marked done after propagating an exception to the caller, therefore, calling
gather.cancel()
after catching an exception (raised by one of the awaitables) from gather won’t cancel any other awaitables.
- asyncio.run(main, *, debug=None)[source]¶
Execute the coroutine and return the result.
This function runs the passed coroutine, taking care of managing the asyncio event loop and finalizing asynchronous generators.
This function cannot be called when another asyncio event loop is running in the same thread.
If debug is True, the event loop will be run in debug mode.
This function always creates a new event loop and closes it at the end. It should be used as a main entry point for asyncio programs, and should ideally only be called once.
Example:
- async def main():
await asyncio.sleep(1) print(‘hello’)
asyncio.run(main())
docutils
¶
This is the Docutils (Python Documentation Utilities) package.
Package Structure¶
Modules:
__init__.py: Contains component base classes, exception classes, and Docutils version information.
core.py: Contains the
Publisher
class andpublish_*()
convenience functions.frontend.py: Runtime settings (command-line interface, configuration files) processing, for Docutils front-ends.
io.py: Provides a uniform API for low-level input and output.
nodes.py: Docutils document tree (doctree) node class library.
statemachine.py: A finite state machine specialized for regular-expression-based text filters.
Subpackages:
languages: Language-specific mappings of terms.
parsers: Syntax-specific input parser modules or packages.
readers: Context-specific input handlers which understand the data source and manage a parser.
transforms: Modules used by readers and writers to modify the Docutils document tree.
utils: Contains the
Reporter
system warning class and miscellaneous utilities used by readers, writers, and transforms.utils/urischemes.py: Contains a complete mapping of known URI addressing scheme names to descriptions.
utils/math: Contains functions for conversion of mathematical notation between different formats (LaTeX, MathML, text, …).
writers: Format-specific output translators.
- exception docutils.DataError[source]¶
Bases:
ApplicationError
- class docutils.Component[source]¶
Bases:
SettingsSpec
,TransformSpec
Base class for Docutils components.
- supports(format)[source]¶
Is format supported by this component?
To be used by transforms to ask the dependent component if it supports a certain input context or output format.
- component_type = None¶
Name of the component type (‘reader’, ‘parser’, ‘writer’). Override in subclasses.
- supported = ()¶
Name and aliases for this component. Override in subclasses.
- class docutils.SettingsSpec[source]¶
Bases:
object
Runtime setting specification base class.
SettingsSpec subclass objects used by docutils.frontend.OptionParser.
- config_section = None¶
The name of the config file section specific to this component (lowercase, no brackets). Override in subclasses.
- config_section_dependencies = None¶
A list of names of config file sections that are to be applied before config_section, in order (from general to specific). In other words, the settings in config_section are to be overlaid on top of the settings from these sections. The “general” section is assumed implicitly. Override in subclasses.
- relative_path_settings = ()¶
Settings containing filesystem paths. Override in subclasses. Settings listed here are to be interpreted relative to the current working directory.
- settings_default_overrides = None¶
A dictionary of auxiliary defaults, to override defaults for settings defined in other components’ setting_specs. Override in subclasses.
- settings_defaults = None¶
A dictionary of defaults for settings not in settings_spec (internal settings, intended to be inaccessible by command-line and config file). Override in subclasses.
- settings_spec = ()¶
Runtime settings specification. Override in subclasses.
Defines runtime settings and associated command-line options, as used by docutils.frontend.OptionParser. This is a tuple of:
Option group title (string or None which implies no group, just a list of single options).
Description (string or None).
A sequence of option tuples. Each consists of:
Help text (string)
List of option strings (e.g.
['-Q', '--quux']
).Dictionary of keyword arguments sent to the OptionParser/OptionGroup
add_option
method.Runtime setting names are derived implicitly from long option names (’–a-setting’ becomes
settings.a_setting
) or explicitly from the ‘dest’ keyword argument.Most settings will also have a ‘validator’ keyword & function. The validator function validates setting values (from configuration files and command-line option arguments) and converts them to appropriate types. For example, the
docutils.frontend.validate_boolean
function, required by all boolean settings, converts true values (‘1’, ‘on’, ‘yes’, and ‘true’) to 1 and false values (‘0’, ‘off’, ‘no’, ‘false’, and ‘’) to 0. Validators need only be set once per setting. See the docutils.frontend.validate_* functions.See the optparse docs for more details.
More triples of group title, description, options, as many times as needed. Thus, settings_spec tuples can be simply concatenated.
- class docutils.TransformSpec[source]¶
Bases:
object
Runtime transform specification base class.
TransformSpec subclass objects used by docutils.transforms.Transformer.
- unknown_reference_resolvers = ()¶
List of functions to try to resolve unknown references. Unknown references have a ‘refname’ attribute which doesn’t correspond to any target in the document. Called when the transforms in docutils.transforms.references are unable to find a correct target. The list should contain functions which will try to resolve unknown references, with the following signature:
def reference_resolver(node): '''Returns boolean: true if resolved, false if not.'''
If the function is able to resolve the reference, it should also remove the ‘refname’ attribute and mark the node as resolved:
del node['refname'] node.resolved = 1
Each function must have a “priority” attribute which will affect the order the unknown_reference_resolvers are run:
reference_resolver.priority = 100
Override in subclasses.
- class docutils.VersionInfo(major=0, minor=0, micro=0, releaselevel='final', serial=0, release=True)[source]¶
Bases:
VersionInfo
- docutils.__version__ = '0.19'¶
Docutils version identifier (complies with PEP 440):
major.minor[.micro][releaselevel[serial]][.dev]
For version comparison operations, use __version_info__ (see, below) rather than parsing the text of __version__.
https://docutils.sourceforge.io/docs/dev/policies.html#version-identification
- docutils.__version_details__ = ''¶
Optional extra version details (e.g. ‘snapshot 2005-05-29, r3410’).
For development and release status, use __version__ and `__version_info__.
json
¶
JSON (JavaScript Object Notation) <http://json.org> is a subset of JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data interchange format.
json
exposes an API familiar to users of the standard library
marshal
and pickle
modules. It is derived from a
version of the externally maintained simplejson library.
Encoding basic Python object hierarchies:
>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> print(json.dumps("\"foo\bar"))
"\"foo\bar"
>>> print(json.dumps('\u1234'))
"\u1234"
>>> print(json.dumps('\\'))
"\\"
>>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
{"a": 0, "b": 0, "c": 0}
>>> from io import StringIO
>>> io = StringIO()
>>> json.dump(['streaming API'], io)
>>> io.getvalue()
'["streaming API"]'
Compact encoding:
>>> import json
>>> mydict = {'4': 5, '6': 7}
>>> json.dumps([1,2,3,mydict], separators=(',', ':'))
'[1,2,3,{"4":5,"6":7}]'
Pretty printing:
>>> import json
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
{
"4": 5,
"6": 7
}
Decoding JSON:
>>> import json
>>> obj = ['foo', {'bar': ['baz', None, 1.0, 2]}]
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
True
>>> json.loads('"\\"foo\\bar"') == '"foo\x08ar'
True
>>> from io import StringIO
>>> io = StringIO('["streaming API"]')
>>> json.load(io)[0] == 'streaming API'
True
Specializing JSON object decoding:
>>> import json
>>> def as_complex(dct):
... if '__complex__' in dct:
... return complex(dct['real'], dct['imag'])
... return dct
...
>>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
... object_hook=as_complex)
(1+2j)
>>> from decimal import Decimal
>>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1')
True
Specializing JSON object encoding:
>>> import json
>>> def encode_complex(obj):
... if isinstance(obj, complex):
... return [obj.real, obj.imag]
... raise TypeError(f'Object of type {obj.__class__.__name__} '
... f'is not JSON serializable')
...
>>> json.dumps(2 + 1j, default=encode_complex)
'[2.0, 1.0]'
>>> json.JSONEncoder(default=encode_complex).encode(2 + 1j)
'[2.0, 1.0]'
>>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j))
'[2.0, 1.0]'
Using json.tool from the shell to validate and pretty-print:
$ echo '{"json":"obj"}' | python -m json.tool
{
"json": "obj"
}
$ echo '{ 1.2:3.4}' | python -m json.tool
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
- exception json.JSONDecodeError(msg, doc, pos)[source]¶
Bases:
ValueError
Subclass of ValueError with the following additional properties:
msg: The unformatted error message doc: The JSON document being parsed pos: The start index of doc where parsing failed lineno: The line corresponding to pos colno: The column corresponding to pos
- class json.JSONDecoder(*, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)[source]¶
Bases:
object
Simple JSON <http://json.org> decoder
Performs the following translations in decoding by default:
JSON
Python
object
dict
array
list
string
str
number (int)
int
number (real)
float
true
True
false
False
null
None
It also understands
NaN
,Infinity
, and-Infinity
as their correspondingfloat
values, which is outside the JSON spec.
- class json.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]¶
Bases:
object
Extensible JSON <http://json.org> encoder for Python data structures.
Supports the following objects and types by default:
Python
JSON
dict
object
list, tuple
array
str
string
int, float
number
True
true
False
false
None
null
To extend this to recognize other objects, subclass and implement a
.default()
method with another method that returns a serializable object foro
if possible, otherwise it should call the superclass implementation (to raiseTypeError
).- default(o)[source]¶
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)[source]¶
Serialize
obj
as a JSON formatted stream tofp
(a.write()
-supporting file-like object).If
skipkeys
is true thendict
keys that are not basic types (str
,int
,float
,bool
,None
) will be skipped instead of raising aTypeError
.If
ensure_ascii
is false, then the strings written tofp
can contain non-ASCII characters if they appear in strings contained inobj
. Otherwise, all such characters are escaped in JSON strings.If
check_circular
is false, then the circular reference check for container types will be skipped and a circular reference will result in anOverflowError
(or worse).If
allow_nan
is false, then it will be aValueError
to serialize out of rangefloat
values (nan
,inf
,-inf
) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN
,Infinity
,-Infinity
).If
indent
is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines.None
is the most compact representation.If specified,
separators
should be an(item_separator, key_separator)
tuple. The default is(', ', ': ')
if indent isNone
and(',', ': ')
otherwise. To get the most compact JSON representation, you should specify(',', ':')
to eliminate whitespace.default(obj)
is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.If sort_keys is true (default:
False
), then the output of dictionaries will be sorted by key.To use a custom
JSONEncoder
subclass (e.g. one that overrides the.default()
method to serialize additional types), specify it with thecls
kwarg; otherwiseJSONEncoder
is used.
- json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)[source]¶
Serialize
obj
to a JSON formattedstr
.If
skipkeys
is true thendict
keys that are not basic types (str
,int
,float
,bool
,None
) will be skipped instead of raising aTypeError
.If
ensure_ascii
is false, then the return value can contain non-ASCII characters if they appear in strings contained inobj
. Otherwise, all such characters are escaped in JSON strings.If
check_circular
is false, then the circular reference check for container types will be skipped and a circular reference will result in anOverflowError
(or worse).If
allow_nan
is false, then it will be aValueError
to serialize out of rangefloat
values (nan
,inf
,-inf
) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN
,Infinity
,-Infinity
).If
indent
is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines.None
is the most compact representation.If specified,
separators
should be an(item_separator, key_separator)
tuple. The default is(', ', ': ')
if indent isNone
and(',', ': ')
otherwise. To get the most compact JSON representation, you should specify(',', ':')
to eliminate whitespace.default(obj)
is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.If sort_keys is true (default:
False
), then the output of dictionaries will be sorted by key.To use a custom
JSONEncoder
subclass (e.g. one that overrides the.default()
method to serialize additional types), specify it with thecls
kwarg; otherwiseJSONEncoder
is used.
- json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)[source]¶
Deserialize
fp
(a.read()
-supporting file-like object containing a JSON document) to a Python object.object_hook
is an optional function that will be called with the result of any object literal decode (adict
). The return value ofobject_hook
will be used instead of thedict
. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).object_pairs_hook
is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value ofobject_pairs_hook
will be used instead of thedict
. This feature can be used to implement custom decoders. Ifobject_hook
is also defined, theobject_pairs_hook
takes priority.To use a custom
JSONDecoder
subclass, specify it with thecls
kwarg; otherwiseJSONDecoder
is used.
- json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)[source]¶
Deserialize
s
(astr
,bytes
orbytearray
instance containing a JSON document) to a Python object.object_hook
is an optional function that will be called with the result of any object literal decode (adict
). The return value ofobject_hook
will be used instead of thedict
. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).object_pairs_hook
is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value ofobject_pairs_hook
will be used instead of thedict
. This feature can be used to implement custom decoders. Ifobject_hook
is also defined, theobject_pairs_hook
takes priority.parse_float
, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal).parse_int
, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).parse_constant
, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN. This can be used to raise an exception if invalid JSON numbers are encountered.To use a custom
JSONDecoder
subclass, specify it with thecls
kwarg; otherwiseJSONDecoder
is used.