Python Forum
program/scrpt wanted (in python, of course)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
program/scrpt wanted (in python, of course)
#4
Use ipaddress module,handles IPv4 and IPv6 addresses.
>>> import ipaddress

>>> ip_1 = ipaddress.ip_address('fc00::1')
>>> ip_2 = ipaddress.ip_address('fc00:0:2::1')
>>> ip_1 < ip_2
True
Address and Network objects are not sortable by default.
If still need to do it there is ipaddress.get_mixed_type_key that be used  as a key in sorted().
import ipaddress

>>> ip_1 = ipaddress.ip_address('fc00::9')
>>> ip_2 = ipaddress.ip_address('fc00::5')
>>> ip_3 = ipaddress.ip_address('fc00::3')
>>> ip_4 = ipaddress.ip_address('fc00::100')
>>> sorted([ip_1, ip_2, ip_3, ip_4], key=ipaddress.get_mixed_type_key)
[IPv6Address('fc00::3'), IPv6Address('fc00::5'), IPv6Address('fc00::9'), IPv6Address('fc00::100')]
Reply


Messages In This Thread
RE: program/scrpt wanted (in python, of course) - by snippsat - Oct-22-2017, 10:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Python Developer Wanted] ‘Women in Tech’ Worldwide Training Initiative from Oxford vanicci 2 2,876 Jun-27-2018, 04:25 PM
Last Post: vanicci
  Command wanted in Python: mgrep Skaperen 1 2,860 Mar-13-2018, 07:38 AM
Last Post: Gribouillis
  Can a python program execute an action on another software program? lex 4 4,011 Jan-26-2018, 03:10 PM
Last Post: buran
  wanted: python code: sleep to a time period Skaperen 2 6,624 Nov-06-2017, 03:00 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020