Commit 7362c3ef authored by Nick Coghlan's avatar Nick Coghlan

Issue #14814: Finish review of ipaddress network object docs (initial patch was by Eli Bendersky)

parent 730f67f2
...@@ -196,10 +196,6 @@ write code that handles both IP versions correctly. ...@@ -196,10 +196,6 @@ write code that handles both IP versions correctly.
>>> ipaddress.IPv6Address('2001:db8::1000') >>> ipaddress.IPv6Address('2001:db8::1000')
IPv6Address('2001:db8::1000') IPv6Address('2001:db8::1000')
All the attributes implemented by :class:`IPv4Address` are supported. In
addition, the following attributs are implemented only by
:class:`IPv6Address`.
.. attribute:: compressed .. attribute:: compressed
The short form of the address representation, with leading zeroes in The short form of the address representation, with leading zeroes in
...@@ -372,10 +368,29 @@ so to avoid duplication they are only documented for :class:`IPv4Network`. ...@@ -372,10 +368,29 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
then :exc:`ValueError` is raised. Otherwise, the host bits are masked out then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
to determine the appropriate network address. to determine the appropriate network address.
This class implements all the attributes of :class:`IPv4Address`, and also Unless stated otherwise, all network methods accepting other network/address
the following attributes and methods. Unless stated otherwise, all methods objects will raise :exc:`TypeError` if the argument's IP version is
accepting other network / address objects will raise :exc:`TypeError` if incompatible to ``self``
the argument's IP version is incompatible to ``self``:
.. attribute:: version
.. attribute:: max_prefixlen
Refer to the corresponding attribute documentation in
:class:`IPv4Address`
.. attribute:: is_multicast
.. attribute:: is_private
.. attribute:: is_unspecified
.. attribute:: is_reserved
.. attribute:: is_loopback
.. attribute:: is_link_local
These attributes are true for the network as a whole if they are true
true for both the network address and the broadcast address
.. attribute:: network_address
The broadcast address for the network.
.. attribute:: broadcast_address .. attribute:: broadcast_address
...@@ -386,16 +401,25 @@ so to avoid duplication they are only documented for :class:`IPv4Network`. ...@@ -386,16 +401,25 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
The host mask, as a string. The host mask, as a string.
.. attribute:: with_prefixlen .. attribute:: with_prefixlen
.. attribute:: compressed
.. attribute:: exploded
A string representation of the network, with the mask in prefix notation. A string representation of the network, with the mask in prefix
notation.
``with_prefixlen`` and ``compressed`` are always the same as
``str(network)``.
``exploded`` uses the exploded form the network address.
.. attribute:: with_netmask .. attribute:: with_netmask
A string representation of the network, with the mask in net mask notation. A string representation of the network, with the mask in net mask
notation.
.. attribute:: with_hostmask .. attribute:: with_hostmask
A string representation of the network, with the mask in host mask notation. A string representation of the network, with the mask in host mask
notation.
.. attribute:: num_addresses .. attribute:: num_addresses
...@@ -403,13 +427,13 @@ so to avoid duplication they are only documented for :class:`IPv4Network`. ...@@ -403,13 +427,13 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
.. attribute:: prefixlen .. attribute:: prefixlen
Length of the prefix, in bits. Length of the network prefix, in bits.
.. method:: hosts() .. method:: hosts()
Generates an iterator over the usable hosts in the network. The usable hosts Returns an iterator over the usable hosts in the network. The usable
are all the IP addresses that belong to the network, except the network hosts are all the IP addresses that belong to the network, except the
address itself and the network broadcast address. network address itself and the network broadcast address.
>>> list(ip_network('192.0.2.0/29').hosts()) >>> list(ip_network('192.0.2.0/29').hosts())
[IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'), [IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
...@@ -418,13 +442,15 @@ so to avoid duplication they are only documented for :class:`IPv4Network`. ...@@ -418,13 +442,15 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
.. method:: overlaps(other) .. method:: overlaps(other)
``True`` if this network is partly contained in *other*. ``True`` if this network is partly or wholly contained in *other* or
or *other* is wholly contained in this network.
.. method:: address_exclude(network) .. method:: address_exclude(network)
Computes the network defintions resulting from removing the given *network* Computes the network definitions resulting from removing the given
from this one. Returns a generator. Raises :exc:`ValueError` if *network* *network* from this one. Returns an iterator of network objects.
is not completely contained in this network. Raises :exc:`ValueError` if *network* is not completely contained in
this network.
>>> n1 = ip_network('192.0.2.0/28') >>> n1 = ip_network('192.0.2.0/28')
>>> n2 = ip_network('192.0.2.1/32') >>> n2 = ip_network('192.0.2.1/32')
...@@ -434,12 +460,12 @@ so to avoid duplication they are only documented for :class:`IPv4Network`. ...@@ -434,12 +460,12 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
.. method:: subnets(prefixlen_diff=1, new_prefix=None) .. method:: subnets(prefixlen_diff=1, new_prefix=None)
The subnets that join to make the current network definition, depending on The subnets that join to make the current network definition, depending
the argument values. *prefixlen_diff* is the amount our prefix length on the argument values. *prefixlen_diff* is the amount our prefix
should be increased by. *new_prefix* is the desired new prefix of the length should be increased by. *new_prefix* is the desired new
subnets; it must be larger than our prefix. One and only one of prefix of the subnets; it must be larger than our prefix. One and
*prefixlen_diff* and *new_prefix* must be set. Returns an iterator of only one of *prefixlen_diff* and *new_prefix* must be set. Returns an
network objects. iterator of network objects.
>>> list(ip_network('192.0.2.0/24').subnets()) >>> list(ip_network('192.0.2.0/24').subnets())
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')] [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
...@@ -456,15 +482,15 @@ so to avoid duplication they are only documented for :class:`IPv4Network`. ...@@ -456,15 +482,15 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
ValueError: new prefix must be longer ValueError: new prefix must be longer
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25)) >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25))
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')] [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
>>>
.. method:: supernet(prefixlen_diff=1, new_prefix=None) .. method:: supernet(prefixlen_diff=1, new_prefix=None)
The supernet containing this network definition, depending on the argument The supernet containing this network definition, depending on the
values. *prefixlen_diff* is the amount our prefix length should be argument values. *prefixlen_diff* is the amount our prefix length
decreased by. *new_prefix* is the desired new prefix of the supernet; it should be decreased by. *new_prefix* is the desired new prefix of
must be smaller than our prefix. One and only one of *prefixlen_diff* and the supernet; it must be smaller than our prefix. One and only one
*new_prefix* must be set. Returns a single network object. of *prefixlen_diff* and *new_prefix* must be set. Returns a single
network object.
>>> ip_network('192.0.2.0/24').supernet() >>> ip_network('192.0.2.0/24').supernet()
IPv4Network('192.0.2.0/23') IPv4Network('192.0.2.0/23')
...@@ -476,8 +502,8 @@ so to avoid duplication they are only documented for :class:`IPv4Network`. ...@@ -476,8 +502,8 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
.. method:: compare_networks(other) .. method:: compare_networks(other)
Compare this network to *other*. In this comparison only the network Compare this network to *other*. In this comparison only the network
addresses are considered; host bits aren't. Returns either ``-1``, ``0`` addresses are considered; host bits aren't. Returns either ``-1``,
or ``1``. ``0`` or ``1``.
>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32')) >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32'))
-1 -1
...@@ -492,7 +518,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`. ...@@ -492,7 +518,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
Construct an IPv6 network definition. *address* can be one of the following: Construct an IPv6 network definition. *address* can be one of the following:
1. A string consisting of an IP address and an optional mask, separated by 1. A string consisting of an IP address and an optional mask, separated by
a slash (``/``). The IP addrses is the network address, and the mask a slash (``/``). The IP address is the network address, and the mask
can be either a single number, which means it's a *prefix*, or a string can be either a single number, which means it's a *prefix*, or a string
representation of an IPv6 address. If it's the latter, the mask is representation of an IPv6 address. If it's the latter, the mask is
interpreted as a *net mask*. If no mask is provided, it's considered to interpreted as a *net mask*. If no mask is provided, it's considered to
...@@ -516,10 +542,38 @@ so to avoid duplication they are only documented for :class:`IPv4Network`. ...@@ -516,10 +542,38 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
then :exc:`ValueError` is raised. Otherwise, the host bits are masked out then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
to determine the appropriate network address. to determine the appropriate network address.
.. describe:: Attributes and methods .. attribute:: version
.. attribute:: max_prefixlen
.. attribute:: is_multicast
.. attribute:: is_private
.. attribute:: is_unspecified
.. attribute:: is_reserved
.. attribute:: is_loopback
.. attribute:: is_link_local
.. attribute:: network_address
.. attribute:: broadcast_address
.. attribute:: host mask
.. attribute:: with_prefixlen
.. attribute:: compressed
.. attribute:: exploded
.. attribute:: with_netmask
.. attribute:: with_hostmask
.. attribute:: num_addresses
.. attribute:: prefixlen
.. method:: hosts()
.. method:: overlaps(other)
.. method:: address_exclude(network)
.. method:: subnets(prefixlen_diff=1, new_prefix=None)
.. method:: supernet(prefixlen_diff=1, new_prefix=None)
.. method:: compare_networks(other)
Refer to the corresponding attribute documentation in
:class:`IPv4Network`
.. attribute:: is_site_local
All attributes and methods implemented by :class:`IPv4Network` and by These attribute is true for the network as a whole if it is true
:class:`IPv6Address` are also implemented by :class:`IPv6Network`. true for both the network address and the broadcast address
Operators Operators
...@@ -529,12 +583,14 @@ Network objects support some operators. Unless stated otherwise, operators can ...@@ -529,12 +583,14 @@ Network objects support some operators. Unless stated otherwise, operators can
only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
IPv6). IPv6).
Logical operators Logical operators
""""""""""""""""" """""""""""""""""
Network objects can be compared with the usual set of logical operators, Network objects can be compared with the usual set of logical operators,
similarly to address objects. similarly to address objects.
Iteration Iteration
""""""""" """""""""
...@@ -563,6 +619,7 @@ example:: ...@@ -563,6 +619,7 @@ example::
IPv4Address('192.0.2.14') IPv4Address('192.0.2.14')
IPv4Address('192.0.2.15') IPv4Address('192.0.2.15')
Networks as containers of addresses Networks as containers of addresses
""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment