|
17 | 17 | from typing import Any, cast, Dict, Optional, Sequence, Union |
18 | 18 |
|
19 | 19 | import geoip2.records |
20 | | -from geoip2.mixins import SimpleEquality |
| 20 | +from geoip2.mixins import Model |
21 | 21 |
|
22 | 22 |
|
23 | | -class Country(SimpleEquality): |
| 23 | +class Country(Model): |
24 | 24 | """Model for the Country web service and Country database. |
25 | 25 |
|
26 | 26 | This class provides the following attributes: |
@@ -97,12 +97,9 @@ def __init__( |
97 | 97 | self.maxmind = geoip2.records.MaxMind(**raw_response.get("maxmind", {})) |
98 | 98 |
|
99 | 99 | self.traits = geoip2.records.Traits(**raw_response.get("traits", {})) |
100 | | - self.raw = raw_response |
101 | 100 |
|
102 | 101 | def __repr__(self) -> str: |
103 | | - return ( |
104 | | - f"{self.__module__}.{self.__class__.__name__}({self.raw}, {self._locales})" |
105 | | - ) |
| 102 | + return f"{self.__module__}.{self.__class__.__name__}({self.to_dict()}, {self._locales})" |
106 | 103 |
|
107 | 104 |
|
108 | 105 | class City(Country): |
@@ -321,22 +318,27 @@ class Enterprise(City): |
321 | 318 | """ |
322 | 319 |
|
323 | 320 |
|
324 | | -class SimpleModel(SimpleEquality, metaclass=ABCMeta): |
| 321 | +class SimpleModel(Model, metaclass=ABCMeta): |
325 | 322 | """Provides basic methods for non-location models""" |
326 | 323 |
|
327 | | - raw: Dict[str, Union[bool, str, int]] |
328 | 324 | ip_address: str |
329 | 325 | _network: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]] |
330 | 326 | _prefix_len: int |
331 | 327 |
|
332 | 328 | def __init__(self, raw: Dict[str, Union[bool, str, int]]) -> None: |
333 | | - self.raw = raw |
334 | | - self._network = None |
335 | | - self._prefix_len = cast(int, raw.get("prefix_len")) |
| 329 | + if network := raw.get("network"): |
| 330 | + self._network = ipaddress.ip_network(network, False) |
| 331 | + self._prefix_len = self._network.prefixlen |
| 332 | + else: |
| 333 | + # This case is for MMDB lookups where performance is paramount. |
| 334 | + # This is why we don't generate the network unless .network is |
| 335 | + # used. |
| 336 | + self._network = None |
| 337 | + self._prefix_len = cast(int, raw.get("prefix_len")) |
336 | 338 | self.ip_address = cast(str, raw.get("ip_address")) |
337 | 339 |
|
338 | 340 | def __repr__(self) -> str: |
339 | | - return f"{self.__module__}.{self.__class__.__name__}({self.raw})" |
| 341 | + return f"{self.__module__}.{self.__class__.__name__}({self.to_dict()})" |
340 | 342 |
|
341 | 343 | @property |
342 | 344 | def network(self) -> Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]: |
|
0 commit comments