MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / __format__

Method __format__

tools/python-3.11.9-amd64/Lib/ipaddress.py:621–668  ·  view source on GitHub ↗

Returns an IP address as a formatted string. Supported presentation types are: 's': returns the IP address as a string (default) 'b': converts to binary and returns a zero-padded string 'X' or 'x': converts to upper- or lower-case hex and returns a zero-padded s

(self, fmt)

Source from the content-addressed store, hash-verified

619 return self.__class__, (self._ip,)
620
621 def __format__(self, fmt):
622 """Returns an IP address as a formatted string.
623
624 Supported presentation types are:
625 's': returns the IP address as a string (default)
626 'b': converts to binary and returns a zero-padded string
627 'X' or 'x': converts to upper- or lower-case hex and returns a zero-padded string
628 'n': the same as 'b' for IPv4 and 'x' for IPv6
629
630 For binary and hex presentation types, the alternate form specifier
631 '#' and the grouping option '_' are supported.
632 """
633
634 # Support string formatting
635 if not fmt or fmt[-1] == 's':
636 return format(str(self), fmt)
637
638 # From here on down, support for 'bnXx'
639 global _address_fmt_re
640 if _address_fmt_re is None:
641 import re
642 _address_fmt_re = re.compile('(#?)(_?)([xbnX])')
643
644 m = _address_fmt_re.fullmatch(fmt)
645 if not m:
646 return super().__format__(fmt)
647
648 alternate, grouping, fmt_base = m.groups()
649
650 # Set some defaults
651 if fmt_base == 'n':
652 if self._version == 4:
653 fmt_base = 'b' # Binary is default for ipv4
654 else:
655 fmt_base = 'x' # Hex is default for ipv6
656
657 if fmt_base == 'b':
658 padlen = self._max_prefixlen
659 else:
660 padlen = self._max_prefixlen // 4
661
662 if grouping:
663 padlen += padlen // 4 - 1
664
665 if alternate:
666 padlen += 2 # 0b or 0x
667
668 return format(int(self), f'{alternate}0{padlen}{grouping}{fmt_base}')
669
670
671@functools.total_ordering

Callers

nothing calls this directly

Calls 4

strFunction · 0.85
formatFunction · 0.70
compileMethod · 0.45
groupsMethod · 0.45

Tested by

no test coverage detected