Skip to content

Commit

Permalink
Code refactor to optimize how methods are inherited
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 2, 2024
1 parent 82d3ca9 commit 00d528d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 112 deletions.
4 changes: 2 additions & 2 deletions pytcp/lib/net_addr/ip4_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from .mac_address import MacAddress

IP4__ADDRESS_LEN = 4

IP4__MASK = 0xFF_FF_FF_FF
IP4__REGEX = (
r"((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}"
r"(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"
Expand Down Expand Up @@ -74,7 +74,7 @@ def __init__(
return

if isinstance(address, int):
if address & 0xFF_FF_FF_FF == address:
if address & IP4__MASK == address:
self._address = address
return

Expand Down
22 changes: 1 addition & 21 deletions pytcp/lib/net_addr/ip4_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@
from .ip_host import IpHost


class Ip4Host(IpHost):
class Ip4Host(IpHost[Ip4Address, Ip4Network]):
"""
IPv4 host support class.
"""

_address: Ip4Address
_network: Ip4Network
_version: int = 4
_gateway: Ip4Address | None = None

Expand Down Expand Up @@ -103,24 +101,6 @@ def __init__(

raise Ip4HostFormatError(host)

@property
@override
def address(self) -> Ip4Address:
"""
Get the IPv4 host address '_address' attribute.
"""

return self._address

@property
@override
def network(self) -> Ip4Network:
"""
Get the IPv4 host address '_network' attribute.
"""

return self._network

@property
@override
def gateway(self) -> Ip4Address | None:
Expand Down
26 changes: 3 additions & 23 deletions pytcp/lib/net_addr/ip4_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@
from .ip_network import IpNetwork


class Ip4Network(IpNetwork):
class Ip4Network(IpNetwork[Ip4Address, Ip4Mask]):
"""
IPv4 network support class.
"""

_version = 4

def __init__(
self,
network: Ip4Network | tuple[Ip4Address, Ip4Mask] | str | None = None,
Expand All @@ -60,10 +62,6 @@ def __init__(
Create a new IPv4 network object.
"""

self._address: Ip4Address
self._mask: Ip4Mask
self._version: int = 4

if network is None:
self._address = Ip4Address()
self._mask = Ip4Mask()
Expand Down Expand Up @@ -101,24 +99,6 @@ def __init__(

raise Ip4NetworkFormatError(network)

@property
@override
def address(self) -> Ip4Address:
"""
Get the IPv4 network '_address' attribute.
"""

return self._address

@property
@override
def mask(self) -> Ip4Mask:
"""
Getter the IPv4 network '_mask' attribute.
"""

return self._mask

@property
@override
def last(self) -> Ip4Address:
Expand Down
4 changes: 2 additions & 2 deletions pytcp/lib/net_addr/ip6_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from .mac_address import MacAddress

IP6__ADDRESS_LEN = 16

IP6__MASK = 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF
IP6__REGEX = (
r"(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|"
r"([0-9a-fA-F]{1,4}:){1,7}:|"
Expand Down Expand Up @@ -81,7 +81,7 @@ def __init__(
return

if isinstance(address, int):
if address & 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF == address:
if address & IP6__MASK == address:
self._address = address
return

Expand Down
22 changes: 1 addition & 21 deletions pytcp/lib/net_addr/ip6_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@
from .ip_host import IpHost


class Ip6Host(IpHost):
class Ip6Host(IpHost[Ip6Address, Ip6Network]):
"""
IPv6 host support class.
"""

_address: Ip6Address
_network: Ip6Network
_version: int = 6
_gateway: Ip6Address | None = None

Expand Down Expand Up @@ -105,24 +103,6 @@ def __init__(

raise Ip6HostFormatError(host)

@property
@override
def address(self) -> Ip6Address:
"""
Get the IPv6 host address '_address' attribute.
"""

return self._address

@property
@override
def network(self) -> Ip6Network:
"""
Get the IPv6 host address '_network' attribute.
"""

return self._network

@property
@override
def gateway(self) -> Ip6Address | None:
Expand Down
26 changes: 3 additions & 23 deletions pytcp/lib/net_addr/ip6_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@
from .ip_network import IpNetwork


class Ip6Network(IpNetwork):
class Ip6Network(IpNetwork[Ip6Address, Ip6Mask]):
"""
IPv6 network support class.
"""

_version: int = 6

def __init__(
self,
network: Ip6Network | tuple[Ip6Address, Ip6Mask] | str | None = None,
Expand All @@ -60,10 +62,6 @@ def __init__(
Create a new IPv4 network object.
"""

self._address: Ip6Address
self._mask: Ip6Mask
self._version: int = 6

if network is None:
self._address = Ip6Address()
self._mask = Ip6Mask()
Expand Down Expand Up @@ -98,24 +96,6 @@ def __init__(

raise Ip6NetworkFormatError(network)

@property
@override
def address(self) -> Ip6Address:
"""
Get the IPv6 network '_address' attribute.
"""

return self._address

@property
@override
def mask(self) -> Ip6Mask:
"""
Get the IPv6 network '_mask' attribute.
"""

return self._mask

@property
@override
def last(self) -> Ip6Address:
Expand Down
22 changes: 12 additions & 10 deletions pytcp/lib/net_addr/ip_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,25 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Generic, TypeVar

from .ip_address import IpAddress

if TYPE_CHECKING:
from .ip_network import IpNetwork


class IpHost(ABC):
A = TypeVar("A", bound="IpAddress")
N = TypeVar("N", bound="IpNetwork")


class IpHost(ABC, Generic[A, N]):
"""
IP host support base class.
"""

_address: IpAddress
_network: IpNetwork
_address: A
_network: N
_version: int
_gateway: IpAddress | None

Expand Down Expand Up @@ -107,22 +111,20 @@ def is_ip4(self) -> bool:
return self._version == 4

@property
@abstractmethod
def address(self) -> IpAddress:
def address(self) -> A:
"""
Get the IP host address '_address' attribute.
"""

raise NotImplementedError
return self._address

@property
@abstractmethod
def network(self) -> IpNetwork:
def network(self) -> N:
"""
Get the IP host address '_network' attribute.
"""

raise NotImplementedError
return self._network

@property
@abstractmethod
Expand Down
22 changes: 12 additions & 10 deletions pytcp/lib/net_addr/ip_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Generic, TypeVar

from .ip_address import IpAddress
from .ip_host import IpHost
Expand All @@ -45,13 +45,17 @@
from .ip_mask import IpMask


class IpNetwork(ABC):
A = TypeVar("A", bound="IpAddress")
M = TypeVar("M", bound="IpMask")


class IpNetwork(ABC, Generic[A, M]):
"""
IP network support base class.
"""

_address: IpAddress
_mask: IpMask
_address: A
_mask: M
_version: int

def __str__(self) -> str:
Expand Down Expand Up @@ -128,22 +132,20 @@ def is_ip4(self) -> bool:
return self._version == 4

@property
@abstractmethod
def address(self) -> IpAddress:
def address(self) -> A:
"""
Get the IP network '_address' attribute.
"""

raise NotImplementedError
return self._address

@property
@abstractmethod
def mask(self) -> IpMask:
def mask(self) -> M:
"""
Get the IP network '_mask' attribute.
"""

raise NotImplementedError
return self._mask

@property
@abstractmethod
Expand Down

0 comments on commit 00d528d

Please sign in to comment.