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 diff --git a/mmdb_writer.py b/mmdb_writer.py index ee08b65..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 @@ -355,6 +355,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 +380,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 +409,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