Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7362c3e

Browse files
committed
Issue #14814: Finish review of ipaddress network object docs (initial patch was by Eli Bendersky)
1 parent 730f67f commit 7362c3e

1 file changed

Lines changed: 135 additions & 78 deletions

File tree

Doc/library/ipaddress.rst

Lines changed: 135 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ write code that handles both IP versions correctly.
196196
>>> ipaddress.IPv6Address('2001:db8::1000')
197197
IPv6Address('2001:db8::1000')
198198

199-
All the attributes implemented by :class:`IPv4Address` are supported. In
200-
addition, the following attributs are implemented only by
201-
:class:`IPv6Address`.
202-
203199
.. attribute:: compressed
204200

205201
The short form of the address representation, with leading zeroes in
@@ -372,127 +368,157 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
372368
then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
373369
to determine the appropriate network address.
374370

375-
This class implements all the attributes of :class:`IPv4Address`, and also
376-
the following attributes and methods. Unless stated otherwise, all methods
377-
accepting other network / address objects will raise :exc:`TypeError` if
378-
the argument's IP version is incompatible to ``self``:
371+
Unless stated otherwise, all network methods accepting other network/address
372+
objects will raise :exc:`TypeError` if the argument's IP version is
373+
incompatible to ``self``
374+
375+
.. attribute:: version
376+
.. attribute:: max_prefixlen
377+
378+
Refer to the corresponding attribute documentation in
379+
:class:`IPv4Address`
380+
381+
.. attribute:: is_multicast
382+
.. attribute:: is_private
383+
.. attribute:: is_unspecified
384+
.. attribute:: is_reserved
385+
.. attribute:: is_loopback
386+
.. attribute:: is_link_local
387+
388+
These attributes are true for the network as a whole if they are true
389+
true for both the network address and the broadcast address
390+
391+
.. attribute:: network_address
392+
393+
The broadcast address for the network.
379394

380395
.. attribute:: broadcast_address
381396

382-
The broadcast address for the network.
397+
The broadcast address for the network.
383398

384399
.. attribute:: host mask
385400

386-
The host mask, as a string.
401+
The host mask, as a string.
387402

388403
.. attribute:: with_prefixlen
404+
.. attribute:: compressed
405+
.. attribute:: exploded
406+
407+
A string representation of the network, with the mask in prefix
408+
notation.
389409

390-
A string representation of the network, with the mask in prefix notation.
410+
``with_prefixlen`` and ``compressed`` are always the same as
411+
``str(network)``.
412+
``exploded`` uses the exploded form the network address.
391413

392414
.. attribute:: with_netmask
393415

394-
A string representation of the network, with the mask in net mask notation.
416+
A string representation of the network, with the mask in net mask
417+
notation.
395418

396419
.. attribute:: with_hostmask
397420

398-
A string representation of the network, with the mask in host mask notation.
421+
A string representation of the network, with the mask in host mask
422+
notation.
399423

400424
.. attribute:: num_addresses
401425

402-
The total number of addresses in the network.
426+
The total number of addresses in the network.
403427

404428
.. attribute:: prefixlen
405429

406-
Length of the prefix, in bits.
430+
Length of the network prefix, in bits.
407431

408432
.. method:: hosts()
409433

410-
Generates an iterator over the usable hosts in the network. The usable hosts
411-
are all the IP addresses that belong to the network, except the network
412-
address itself and the network broadcast address.
434+
Returns an iterator over the usable hosts in the network. The usable
435+
hosts are all the IP addresses that belong to the network, except the
436+
network address itself and the network broadcast address.
413437

414-
>>> list(ip_network('192.0.2.0/29').hosts())
415-
[IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
416-
IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'),
417-
IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
438+
>>> list(ip_network('192.0.2.0/29').hosts())
439+
[IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
440+
IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'),
441+
IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
418442

419443
.. method:: overlaps(other)
420444

421-
``True`` if this network is partly contained in *other*.
445+
``True`` if this network is partly or wholly contained in *other* or
446+
or *other* is wholly contained in this network.
422447

423448
.. method:: address_exclude(network)
424449

425-
Computes the network defintions resulting from removing the given *network*
426-
from this one. Returns a generator. Raises :exc:`ValueError` if *network*
427-
is not completely contained in this network.
450+
Computes the network definitions resulting from removing the given
451+
*network* from this one. Returns an iterator of network objects.
452+
Raises :exc:`ValueError` if *network* is not completely contained in
453+
this network.
428454

429-
>>> n1 = ip_network('192.0.2.0/28')
430-
>>> n2 = ip_network('192.0.2.1/32')
431-
>>> list(n1.address_exclude(n2))
432-
[IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'),
433-
IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]
455+
>>> n1 = ip_network('192.0.2.0/28')
456+
>>> n2 = ip_network('192.0.2.1/32')
457+
>>> list(n1.address_exclude(n2))
458+
[IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'),
459+
IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]
434460

435461
.. method:: subnets(prefixlen_diff=1, new_prefix=None)
436462

437-
The subnets that join to make the current network definition, depending on
438-
the argument values. *prefixlen_diff* is the amount our prefix length
439-
should be increased by. *new_prefix* is the desired new prefix of the
440-
subnets; it must be larger than our prefix. One and only one of
441-
*prefixlen_diff* and *new_prefix* must be set. Returns an iterator of
442-
network objects.
443-
444-
>>> list(ip_network('192.0.2.0/24').subnets())
445-
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
446-
>>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2))
447-
[IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
448-
IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
449-
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26))
450-
[IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
451-
IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
452-
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23))
453-
Traceback (most recent call last):
454-
File "<stdin>", line 1, in <module>
455-
raise ValueError('new prefix must be longer')
456-
ValueError: new prefix must be longer
457-
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25))
458-
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
459-
>>>
463+
The subnets that join to make the current network definition, depending
464+
on the argument values. *prefixlen_diff* is the amount our prefix
465+
length should be increased by. *new_prefix* is the desired new
466+
prefix of the subnets; it must be larger than our prefix. One and
467+
only one of *prefixlen_diff* and *new_prefix* must be set. Returns an
468+
iterator of network objects.
469+
470+
>>> list(ip_network('192.0.2.0/24').subnets())
471+
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
472+
>>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2))
473+
[IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
474+
IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
475+
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26))
476+
[IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
477+
IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
478+
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23))
479+
Traceback (most recent call last):
480+
File "<stdin>", line 1, in <module>
481+
raise ValueError('new prefix must be longer')
482+
ValueError: new prefix must be longer
483+
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25))
484+
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
460485

461486
.. method:: supernet(prefixlen_diff=1, new_prefix=None)
462487

463-
The supernet containing this network definition, depending on the argument
464-
values. *prefixlen_diff* is the amount our prefix length should be
465-
decreased by. *new_prefix* is the desired new prefix of the supernet; it
466-
must be smaller than our prefix. One and only one of *prefixlen_diff* and
467-
*new_prefix* must be set. Returns a single network object.
488+
The supernet containing this network definition, depending on the
489+
argument values. *prefixlen_diff* is the amount our prefix length
490+
should be decreased by. *new_prefix* is the desired new prefix of
491+
the supernet; it must be smaller than our prefix. One and only one
492+
of *prefixlen_diff* and *new_prefix* must be set. Returns a single
493+
network object.
468494

469-
>>> ip_network('192.0.2.0/24').supernet()
470-
IPv4Network('192.0.2.0/23')
471-
>>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2)
472-
IPv4Network('192.0.0.0/22')
473-
>>> ip_network('192.0.2.0/24').supernet(new_prefix=20)
474-
IPv4Network('192.0.0.0/20')
495+
>>> ip_network('192.0.2.0/24').supernet()
496+
IPv4Network('192.0.2.0/23')
497+
>>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2)
498+
IPv4Network('192.0.0.0/22')
499+
>>> ip_network('192.0.2.0/24').supernet(new_prefix=20)
500+
IPv4Network('192.0.0.0/20')
475501

476502
.. method:: compare_networks(other)
477503

478-
Compare this network to *other*. In this comparison only the network
479-
addresses are considered; host bits aren't. Returns either ``-1``, ``0``
480-
or ``1``.
504+
Compare this network to *other*. In this comparison only the network
505+
addresses are considered; host bits aren't. Returns either ``-1``,
506+
``0`` or ``1``.
481507

482-
>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32'))
483-
-1
484-
>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32'))
485-
1
486-
>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32'))
487-
0
508+
>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32'))
509+
-1
510+
>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32'))
511+
1
512+
>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32'))
513+
0
488514

489515

490516
.. class:: IPv6Network(address, strict=True)
491517

492518
Construct an IPv6 network definition. *address* can be one of the following:
493519

494520
1. A string consisting of an IP address and an optional mask, separated by
495-
a slash (``/``). The IP addrses is the network address, and the mask
521+
a slash (``/``). The IP address is the network address, and the mask
496522
can be either a single number, which means it's a *prefix*, or a string
497523
representation of an IPv6 address. If it's the latter, the mask is
498524
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`.
516542
then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
517543
to determine the appropriate network address.
518544

519-
.. describe:: Attributes and methods
545+
.. attribute:: version
546+
.. attribute:: max_prefixlen
547+
.. attribute:: is_multicast
548+
.. attribute:: is_private
549+
.. attribute:: is_unspecified
550+
.. attribute:: is_reserved
551+
.. attribute:: is_loopback
552+
.. attribute:: is_link_local
553+
.. attribute:: network_address
554+
.. attribute:: broadcast_address
555+
.. attribute:: host mask
556+
.. attribute:: with_prefixlen
557+
.. attribute:: compressed
558+
.. attribute:: exploded
559+
.. attribute:: with_netmask
560+
.. attribute:: with_hostmask
561+
.. attribute:: num_addresses
562+
.. attribute:: prefixlen
563+
.. method:: hosts()
564+
.. method:: overlaps(other)
565+
.. method:: address_exclude(network)
566+
.. method:: subnets(prefixlen_diff=1, new_prefix=None)
567+
.. method:: supernet(prefixlen_diff=1, new_prefix=None)
568+
.. method:: compare_networks(other)
569+
570+
Refer to the corresponding attribute documentation in
571+
:class:`IPv4Network`
572+
573+
.. attribute:: is_site_local
520574

521-
All attributes and methods implemented by :class:`IPv4Network` and by
522-
:class:`IPv6Address` are also implemented by :class:`IPv6Network`.
575+
These attribute is true for the network as a whole if it is true
576+
true for both the network address and the broadcast address
523577

524578

525579
Operators
@@ -529,12 +583,14 @@ Network objects support some operators. Unless stated otherwise, operators can
529583
only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
530584
IPv6).
531585

586+
532587
Logical operators
533588
"""""""""""""""""
534589

535590
Network objects can be compared with the usual set of logical operators,
536591
similarly to address objects.
537592

593+
538594
Iteration
539595
"""""""""
540596

@@ -563,6 +619,7 @@ example::
563619
IPv4Address('192.0.2.14')
564620
IPv4Address('192.0.2.15')
565621

622+
566623
Networks as containers of addresses
567624
"""""""""""""""""""""""""""""""""""
568625

0 commit comments

Comments
 (0)