diff --git a/binding/python/util_test.py b/binding/python/util_test.py new file mode 100644 index 0000000..8ee69a3 --- /dev/null +++ b/binding/python/util_test.py @@ -0,0 +1,68 @@ +# Copyright 2022 The Ip2Region Authors. All rights reserved. +# Use of this source code is governed by a Apache2.0-style +# license that can be found in the LICENSE file. + +# util test script on 2025/10/29 +# Author Leon + +import os +import sys +import time +from xdb import util + +script_dir = os.path.dirname(__file__) +data_dir = os.path.join(script_dir, '../../data/') +xdb_v4_path = os.path.join(data_dir, "ip2region_v4.xdb") +xdb_v6_path = os.path.join(data_dir, "ip2region_v6.xdb") + +# print(script_dir, data_dir, xdb_v4_path, xdb_v6_path) +def test_version(): + print("1, version contants: ") + print("IPv4 -> ", util.IPv4) + print("IPv6 -> ", util.IPv6) + + # version from name + print("2, version from name: ") + for name in ["v4", "IPv4", "v4x", "v6", "IPv6", "v6x"]: + print("version_from_name({}) -> ".format(name), util.version_from_name(name)) + + # version from header + print("3, version from header: ") + v4_header = util.load_header_from_file(xdb_v4_path) + v6_header = util.load_header_from_file(xdb_v6_path) + print("version_from_header(v4_header) -> ", util.version_from_header(v4_header)) + print("version_from_header(v6_header) -> ", util.version_from_header(v6_header)) + +def test_load_header(): + v4_header = util.load_header_from_file(xdb_v4_path) + v6_header = util.load_header_from_file(xdb_v6_path) + print("v4_header -> ", v4_header) + print("v6_header -> ", v6_header) + +def test_load_vector_index(): + v4_v_index = util.load_vector_index_from_file(xdb_v4_path) + v6_v_index = util.load_vector_index_from_file(xdb_v6_path) + print("v4_v_index.length={}".format(len(v4_v_index))) + print("v6_v_index.length={}".format(len(v6_v_index))) + +def test_load_content(): + v4_content = util.load_content_from_file(xdb_v4_path) + v6_content = util.load_content_from_file(xdb_v6_path) + print("v4_content.length={}".format(len(v4_content))) + print("v6_content.length={}".format(len(v6_content))) + +if __name__ == "__main__": + # check and call the specified function + if len(sys.argv) < 2: + sys.exit("please specified the function to test") + + func = sys.argv[1] + all_ids = globals() + if func in all_ids and callable(all_ids[func]): + print("+---calling test function {} ...".format(func)) + s_time = time.time() + all_ids[func]() + c_time = time.time() - s_time + print(f"|---Done, elapsed {c_time:.6f}s") + else: + sys.exit("unable to call function {}".format(func)) \ No newline at end of file diff --git a/binding/python/xdb/util.py b/binding/python/xdb/util.py index f66b600..afc715a 100644 --- a/binding/python/xdb/util.py +++ b/binding/python/xdb/util.py @@ -6,7 +6,6 @@ # Author Leon import io -import struct # global constants XdbStructure20 = 2 @@ -18,21 +17,24 @@ HeaderInfoLength = 256 VectorIndexRows = 256 VectorIndexCols = 256 VectorIndexSize = 8 +# cache of VectorIndexCols × VectorIndexRows × VectorIndexSize +VectorIndexLength = 524288 class Header(object): ''' header class ''' def __init__(self, buff): - self.version = struct.unpack_from("", IPv4) - print("IPv6 ->", IPv6) - print("version_from_name(v4) ->", version_from_name("v4")) - print("version_from_name(v6) ->", version_from_name("v4")) - print("version_from_header() ->", version_from_header(header)) +def load_content(handle): + ''' + load the whole xdb content from a specified file handle + ''' + handle.seek(0) + return handle.read() + +def load_content_from_file(db_file): + handle = io.open(db_file, "rb") + c_buff = load_content(handle) + handle.close() + return c_buff \ No newline at end of file