for a 'good python'

Barry barry at barrys-emacs.org
Wed Apr 12 17:36:57 EDT 2023





> On 12 Apr 2023, at 18:10, jak <nospam at please.ty> wrote:
> Hi everyone,
> some time ago I wrote a class to determine if an ipv4 address belonged
> to a subnet. Seldom using python I'm pretty sure it's not written in
> 'good python' nor too portable. Could you give me some advice to make it
> better?
> 
> class calcip:
>    def __init__(self, psubnet: str):
>        ssubnet, scidr = psubnet.replace(' ', '').split('/')
>        subnet = int.from_bytes(tuple(
>                                  map(lambda n: (int(n)), ssubnet.split('.'))),
>                                'big')
>        cidr = int(scidr)
>        mask = ((2 ** cidr) - 1) << (32 - cidr)
>        self.__network = subnet & mask
>        self.__wildcard = ~mask & 0xffffffff
>        self.__broadcast = (subnet | self.__wildcard) & 0xffffffff
>        self.__tsubnet = tuple(subnet.to_bytes(4, 'big'))
>        self.__tnetwork = tuple(self.__network.to_bytes(4, 'big'))
>        self.__tbroadcast = tuple(self.__broadcast.to_bytes(4, 'big'))
>        self.__tmask = tuple(mask.to_bytes(4, 'big'))
>        self.__twildcard = tuple(self.__wildcard.to_bytes(4, 'big'))
>        self.__host_min = tuple((self.__network + 1).to_bytes(4, 'big'))
>        self.__host_max = tuple((self.__broadcast - 1).to_bytes(4, 'big'))
> 
>    @staticmethod
>    def __to_str(val: tuple):
>        return '.'.join(str(v) for v in val)
> 
>    @property
>    def subnet(self):
>        return self.__to_str(self.__tsubnet)
> 
>    @property
>    def network(self):
>        return self.__to_str(self.__tnetwork)
> 
>    @property
>    def broadcast(self):
>        return self.__to_str(self.__tbroadcast)
> 
>    @property
>    def mask(self):
>        return self.__to_str(self.__tmask)
> 
>    @property
>    def wildcard(self):
>        return self.__to_str(self.__twildcard)
> 
>    @property
>    def host_min(self):
>        return self.__to_str(self.__host_min)
> 
>    @property
>    def host_max(self):
>        return self.__to_str(self.__host_max)
> 
>    @property
>    def hosts_num(self):
>        return self.__wildcard - 1
> 
>    @property
>    def net_class(self):
>        tst = (self.__tnetwork[0] & 0xf0) >> 4
>        if (tst & 0x8) == 0:
>            clx = 'A'
>        elif (tst & 0xc) == 0x8:
>            clx = 'B'
>        elif (tst & 0xe) == 0xc:
>            clx = 'C'
>        elif (tst & 0xf) == 0xe:
>            clx = 'D'
>        elif (tst & 0xf) == 0xf:
>            clx = 'E'
>        return clx
> 
>    def __contains__(self, item):
>        ret = True
>        row_hdr = None
>        try:
>            row_hdr = int.from_bytes(tuple(map(lambda n: (int(n)), item.split('.'))), 'big')
>        except:
>            ret = False
>        if ret:
>            if not self.__network < row_hdr < self.__broadcast:
>                ret = False
>        return ret
> 
> 
> def main():
>    sn = calcip('10.0.0.0/26')
> 
>    print(f"subnet: {sn.subnet}")
>    print(f"network: {sn.network}")
>    print(f"broadcast: {sn.broadcast}")
>    print(f"mask: {sn.mask}")
>    print(f"wildcard: {sn.wildcard}")
>    print(f"host_min: {sn.host_min}")
>    print(f"host_max: {sn.host_max}")
>    print(f"Avaible hosts: {sn.hosts_num}")
>    print(f"Class: {sn.net_class}")
> 
>    tst_hdr = '10.0.0.31'
>    is_not = 'is '
>    if not tst_hdr in sn:
>        is_not = 'is NOT '
>    print("hdr %s %sin range %s - %s" %
>          (tst_hdr, is_not, sn.host_min, sn.host_max))
> 
> if __name__ == '__main__':
>    main()

There is this https://docs.python.org/3/howto/ipaddress.html if you just want a solution.

Or are you after code review feedback?

Barry

> -- 
> https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list