Return a string representation in compressed format using '::' Notation. >>> IP('127.0.0.1').strCompressed() '127.0.0.1' >>> IP('2001:0658:022a:cafe:0200::1').strCompressed() '2001:658:22a:cafe:200::1' >>> IP('ffff:ffff:ffff:ffff:ffff:f:f:fffc/127').strCompre
(self, wantprefixlen=None)
| 306 | return '0' * (bits - len(ret)) + ret + self._printPrefix(wantprefixlen) |
| 307 | |
| 308 | def strCompressed(self, wantprefixlen=None): |
| 309 | """Return a string representation in compressed format using '::' Notation. |
| 310 | |
| 311 | >>> IP('127.0.0.1').strCompressed() |
| 312 | '127.0.0.1' |
| 313 | >>> IP('2001:0658:022a:cafe:0200::1').strCompressed() |
| 314 | '2001:658:22a:cafe:200::1' |
| 315 | >>> IP('ffff:ffff:ffff:ffff:ffff:f:f:fffc/127').strCompressed() |
| 316 | 'ffff:ffff:ffff:ffff:ffff:f:f:fffc/127' |
| 317 | """ |
| 318 | |
| 319 | if self.WantPrefixLen == None and wantprefixlen == None: |
| 320 | wantprefixlen = 1 |
| 321 | |
| 322 | if self._ipversion == 4: |
| 323 | return self.strFullsize(wantprefixlen) |
| 324 | else: |
| 325 | if self.ip >> 32 == 0xffff: |
| 326 | ipv4 = intToIp(self.ip & 0xffffffff, 4) |
| 327 | text = "::ffff:" + ipv4 + self._printPrefix(wantprefixlen) |
| 328 | return text |
| 329 | # find the longest sequence of '0' |
| 330 | hextets = [int(x, 16) for x in self.strFullsize(0).split(':')] |
| 331 | # every element of followingzeros will contain the number of zeros |
| 332 | # following the corresponding element of hextets |
| 333 | followingzeros = [0] * 8 |
| 334 | for i in range(len(hextets)): |
| 335 | followingzeros[i] = _countFollowingZeros(hextets[i:]) |
| 336 | # compressionpos is the position where we can start removing zeros |
| 337 | compressionpos = followingzeros.index(max(followingzeros)) |
| 338 | if max(followingzeros) > 1: |
| 339 | # genererate string with the longest number of zeros cut out |
| 340 | # now we need hextets as strings |
| 341 | hextets = [x for x in self.strNormal(0).split(':')] |
| 342 | while compressionpos < len(hextets) and hextets[compressionpos] == '0': |
| 343 | del (hextets[compressionpos]) |
| 344 | hextets.insert(compressionpos, '') |
| 345 | if compressionpos + 1 >= len(hextets): |
| 346 | hextets.append('') |
| 347 | if compressionpos == 0: |
| 348 | hextets = [''] + hextets |
| 349 | return ':'.join(hextets) + self._printPrefix(wantprefixlen) |
| 350 | else: |
| 351 | return self.strNormal(0) + self._printPrefix(wantprefixlen) |
| 352 | |
| 353 | def strNormal(self, wantprefixlen=None): |
| 354 | """Return a string representation in the usual format. |
no test coverage detected