From d970b435b4437841d758a5660e3b8bcd37472e56 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 27 Jul 2022 12:49:03 -0700 Subject: [PATCH] do str/int conversions up front --- jc/parsers/ip_address.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/jc/parsers/ip_address.py b/jc/parsers/ip_address.py index 23bcd752..fbb6a8b6 100644 --- a/jc/parsers/ip_address.py +++ b/jc/parsers/ip_address.py @@ -27,7 +27,7 @@ Examples: $ ip_address | jc --ip_address -p -r [] """ -from typing import List, Dict +from typing import Dict import binascii import ipaddress from collections import deque @@ -100,21 +100,21 @@ def parse( interface = ipaddress.ip_interface(data.strip()) network_string = str(interface.network).split('/')[0] - network_cidr = str(interface.with_prefixlen).split('/')[1] + network_cidr = int(str(interface.with_prefixlen).split('/')[1]) network = ipaddress.ip_network(f'{network_string}/{network_cidr}') bare_ip_string = str(interface.ip) bare_ip = ipaddress.ip_address(bare_ip_string) ip_ptr = bare_ip.reverse_pointer - first_host = next(network.hosts()) + first_host = str(next(network.hosts())) # hack to speed up iterating through large ipv6 subnets # only do last_host for masks >= /16 (ipv4) or >= /120 (ipv6) last_host = None if any(( - int(interface.version) == 4 and int(network_cidr) >= 16, - int(interface.version) == 6 and int(network_cidr) >= 120, + int(interface.version) == 4 and network_cidr >= 16, + int(interface.version) == 6 and network_cidr >= 120, )): dd = deque(network.hosts(), maxlen=1) last_host = str(dd.pop()) @@ -130,8 +130,8 @@ def parse( 'broadcast': str(network.broadcast_address), 'hostmask': str(interface.with_hostmask).split('/')[1], 'netmask': str(interface.with_netmask).split('/')[1], - 'cidr_netmask': int(network_cidr), - 'first_host': str(first_host), + 'cidr_netmask': network_cidr, + 'first_host': first_host, 'last_host': last_host, # None if netmask is too small 'is_multicast': interface.is_multicast, 'is_private': interface.is_private,