From b5d2986cc720fc144769500c1ac912e00c1cdeed Mon Sep 17 00:00:00 2001 From: VimT Date: Wed, 11 Sep 2024 21:38:08 +0800 Subject: [PATCH 1/3] fix: cache[value] instead of cache[id(value)] (#12) --- mmdb_writer.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mmdb_writer.py b/mmdb_writer.py index ee08b65..cc905e7 100644 --- a/mmdb_writer.py +++ b/mmdb_writer.py @@ -7,6 +7,7 @@ from decimal import Decimal from enum import IntEnum from typing import Dict, List, Literal, Union +from weakref import WeakKeyDictionary from netaddr import IPNetwork, IPSet @@ -355,6 +356,13 @@ def python_type_id(self, value): return MMDBTypeID.DOUBLE raise TypeError(f"unknown type {value_type}") + def _freeze(self, value): + if isinstance(value, dict): + return tuple((k, self._freeze(v)) for k, v in value.items()) + elif isinstance(value, list): + return tuple(self._freeze(v) for v in value) + return value + def encode_meta(self, meta): res = self._make_header(MMDBTypeID.MAP, len(meta)) meta_type = { @@ -373,8 +381,9 @@ def encode_meta(self, meta): def encode(self, value, type_id=None): if self.cache: + cache_key = self._freeze(value) try: - return self.data_cache[id(value)] + return self.data_cache[cache_key] except KeyError: pass @@ -401,7 +410,7 @@ def encode(self, value, type_id=None): pointer_position = self.data_pointer self.data_pointer += len(res) pointer = self.encode(pointer_position, 1) - self.data_cache[id(value)] = pointer + self.data_cache[cache_key] = pointer return pointer return res From 17e0a5535bc72322e1ce80f49d139bb5e8adfdde Mon Sep 17 00:00:00 2001 From: VimT Date: Thu, 12 Sep 2024 21:23:54 +0800 Subject: [PATCH 2/3] release: v0.2.3 --- mmdb_writer.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mmdb_writer.py b/mmdb_writer.py index cc905e7..194b825 100644 --- a/mmdb_writer.py +++ b/mmdb_writer.py @@ -1,4 +1,4 @@ -__version__ = "0.2.2" +__version__ = "0.2.3" import logging import math @@ -7,7 +7,6 @@ from decimal import Decimal from enum import IntEnum from typing import Dict, List, Literal, Union -from weakref import WeakKeyDictionary from netaddr import IPNetwork, IPSet From 1671f07acedc1bdcd719168bc0d93fbf202d9165 Mon Sep 17 00:00:00 2001 From: VimT Date: Thu, 12 Sep 2024 21:35:14 +0800 Subject: [PATCH 3/3] doc: readme --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f0aaaba..d652ead 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,18 @@ Make `mmdb` format ip library file which can be read by [`maxmind` official language reader](https://dev.maxmind.com/geoip/geoip2/downloadable/) -[The official perl writer](https://github.com/maxmind/MaxMind-DB-Writer-perl) was written in perl, which was difficult to customize. So I implemented the `MaxmindDB format` ip library in python language +~~[The official perl writer](https://github.com/maxmind/MaxMind-DB-Writer-perl) was written in perl, +which was difficult to customize. +So I implemented the `MaxmindDB format` ip library in python language.~~ + +MaxMind has now released an official Go version of the MMDB writer. +If you prefer using Go, you can check out the official Go implementation [mmdbwriter](https://github.com/maxmind/mmdbwriter). +This project still provides a Python alternative for those who need it. + ## Install + ```shell script -pip install -U git+https://github.com/VimT/MaxMind-DB-Writer-python +pip install -U mmdb_writer ``` ## Usage