{"id":13836650,"url":"https://github.com/pytries/datrie","last_synced_at":"2025-12-11T21:03:56.710Z","repository":{"id":3864124,"uuid":"4949485","full_name":"pytries/datrie","owner":"pytries","description":"Fast, efficiently stored Trie for Python. Uses libdatrie.","archived":false,"fork":false,"pushed_at":"2024-02-01T20:39:44.000Z","size":2205,"stargazers_count":533,"open_issues_count":40,"forks_count":87,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-03-28T10:08:56.144Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://pypi.python.org/pypi/datrie/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pytries.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES.rst","contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2012-07-08T20:18:28.000Z","updated_at":"2025-02-23T22:34:11.000Z","dependencies_parsed_at":"2024-03-25T00:42:45.512Z","dependency_job_id":null,"html_url":"https://github.com/pytries/datrie","commit_stats":{"total_commits":201,"total_committers":16,"mean_commits":12.5625,"dds":"0.42288557213930345","last_synced_commit":"ba96de5dd42420237e84b6083f1836ef345592c6"},"previous_names":["kmike/datrie"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pytries%2Fdatrie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pytries%2Fdatrie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pytries%2Fdatrie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pytries%2Fdatrie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pytries","download_url":"https://codeload.github.com/pytries/datrie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247166169,"owners_count":20894654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T15:00:51.800Z","updated_at":"2025-12-11T21:03:51.679Z","avatar_url":"https://github.com/pytries.png","language":"Python","funding_links":[],"categories":["C"],"sub_categories":[],"readme":"datrie |travis| |appveyor|\n==========================\n\n.. |travis| image:: https://travis-ci.org/pytries/datrie.svg\n   :target: https://travis-ci.org/pytries/datrie\n\n.. |appveyor| image:: https://ci.appveyor.com/api/projects/status/6bpvhllpjhlau7x0?svg=true\n   :target: https://ci.appveyor.com/project/superbobry/datrie\n\nSuper-fast, efficiently stored Trie for Python (2.x and 3.x).\nUses `libdatrie`_.\n\n.. _libdatrie: https://linux.thai.net/~thep/datrie/datrie.html\n\nInstallation\n============\n\n::\n\n    pip install datrie\n\nUsage\n=====\n\nCreate a new trie capable of storing items with lower-case ascii keys::\n\n    \u003e\u003e\u003e import string\n    \u003e\u003e\u003e import datrie\n    \u003e\u003e\u003e trie = datrie.Trie(string.ascii_lowercase)\n\n``trie`` variable is a dict-like object that can have unicode keys of\ncertain ranges and Python objects as values.\n\nIn addition to implementing the mapping interface, tries facilitate\nfinding the items for a given prefix, and vice versa, finding the\nitems whose keys are prefixes of a given string. As a common special\ncase, finding the longest-prefix item is also supported.\n\n.. warning::\n\n    For efficiency you must define allowed character range(s) while\n    creating trie. ``datrie`` doesn't check if keys are in allowed\n    ranges at runtime, so be careful! Invalid keys are OK at lookup time\n    but values won't be stored correctly for such keys.\n\nAdd some values to it (datrie keys must be unicode; the examples\nare for Python 2.x)::\n\n    \u003e\u003e\u003e trie[u'foo'] = 5\n    \u003e\u003e\u003e trie[u'foobar'] = 10\n    \u003e\u003e\u003e trie[u'bar'] = 'bar value'\n    \u003e\u003e\u003e trie.setdefault(u'foobar', 15)\n    10\n\nCheck if u'foo' is in trie::\n\n    \u003e\u003e\u003e u'foo' in trie\n    True\n\nGet a value::\n\n    \u003e\u003e\u003e trie[u'foo']\n    5\n\nFind all prefixes of a word::\n\n    \u003e\u003e\u003e trie.prefixes(u'foobarbaz')\n    [u'foo', u'foobar']\n\n    \u003e\u003e\u003e trie.prefix_items(u'foobarbaz')\n    [(u'foo', 5), (u'foobar', 10)]\n\n    \u003e\u003e\u003e trie.iter_prefixes(u'foobarbaz')\n    \u003cgenerator object ...\u003e\n\n    \u003e\u003e\u003e trie.iter_prefix_items(u'foobarbaz')\n    \u003cgenerator object ...\u003e\n\nFind the longest prefix of a word::\n\n    \u003e\u003e\u003e trie.longest_prefix(u'foo')\n    u'foo'\n\n    \u003e\u003e\u003e trie.longest_prefix(u'foobarbaz')\n    u'foobar'\n\n    \u003e\u003e\u003e trie.longest_prefix(u'gaz')\n    KeyError: u'gaz'\n\n    \u003e\u003e\u003e trie.longest_prefix(u'gaz', default=u'vasia')\n    u'vasia'\n\n    \u003e\u003e\u003e trie.longest_prefix_item(u'foobarbaz')\n    (u'foobar', 10)\n\nCheck if the trie has keys with a given prefix::\n\n    \u003e\u003e\u003e trie.has_keys_with_prefix(u'fo')\n    True\n\n    \u003e\u003e\u003e trie.has_keys_with_prefix(u'FO')\n    False\n\nGet all items with a given prefix from a trie::\n\n    \u003e\u003e\u003e trie.keys(u'fo')\n    [u'foo', u'foobar']\n\n    \u003e\u003e\u003e trie.items(u'ba')\n    [(u'bar', 'bar value')]\n\n    \u003e\u003e\u003e trie.values(u'foob')\n    [10]\n\nGet all suffixes of certain word starting with a given prefix from a trie::\n\n    \u003e\u003e\u003e trie.suffixes()\n    [u'pro', u'producer', u'producers', u'product', u'production', u'productivity', u'prof']\n    \u003e\u003e\u003e trie.suffixes(u'prod')\n    [u'ucer', u'ucers', u'uct', u'uction', u'uctivity']\n\n\nSave \u0026 load a trie (values must be picklable)::\n\n    \u003e\u003e\u003e trie.save('my.trie')\n    \u003e\u003e\u003e trie2 = datrie.Trie.load('my.trie')\n\n\n\nTrie and BaseTrie\n=================\n\nThere are two Trie classes in datrie package: ``datrie.Trie`` and\n``datrie.BaseTrie``. ``datrie.BaseTrie`` is slightly faster and uses less\nmemory but it can store only integer numbers -2147483648 \u003c= x \u003c= 2147483647.\n``datrie.Trie`` is a bit slower but can store any Python object as a value.\n\nIf you don't need values or integer values are OK then use ``datrie.BaseTrie``::\n\n    import datrie\n    import string\n    trie = datrie.BaseTrie(string.ascii_lowercase)\n\nCustom iteration\n================\n\nIf the built-in trie methods don't fit you can use ``datrie.State`` and\n``datrie.Iterator`` to implement custom traversal.\n\n.. note::\n\n    If you use ``datrie.BaseTrie`` you need ``datrie.BaseState`` and\n    ``datrie.BaseIterator`` for custom traversal.\n\n\nFor example, let's find all suffixes of ``'fo'`` for our trie and get\nthe values::\n\n    \u003e\u003e\u003e state = datrie.State(trie)\n    \u003e\u003e\u003e state.walk(u'foo')\n    \u003e\u003e\u003e it = datrie.Iterator(state)\n    \u003e\u003e\u003e while it.next():\n    ...     print(it.key())\n    ...     print(it.data))\n    o\n    5\n    obar\n    10\n\nPerformance\n===========\n\nPerformance is measured for ``datrie.Trie`` against Python's dict with\n100k unique unicode words (English and Russian) as keys and '1' numbers\nas values.\n\n``datrie.Trie`` uses about 5M memory for 100k words; Python's dict\nuses about 22M for this according to my unscientific tests.\n\nThis trie implementation is 2-6 times slower than python's dict\non __getitem__. Benchmark results (macbook air i5 1.8GHz,\n\"1.000M ops/sec\" == \"1 000 000 operations per second\")::\n\n    Python 2.6:\n    dict __getitem__: 7.107M ops/sec\n    trie __getitem__: 2.478M ops/sec\n\n    Python 2.7:\n    dict __getitem__: 6.550M ops/sec\n    trie __getitem__: 2.474M ops/sec\n\n    Python 3.2:\n    dict __getitem__: 8.185M ops/sec\n    trie __getitem__: 2.684M ops/sec\n\n    Python 3.3:\n    dict __getitem__: 7.050M ops/sec\n    trie __getitem__: 2.755M ops/sec\n\nLooking for prefixes of a given word is almost as fast as\n``__getitem__`` (results are for Python 3.3)::\n\n    trie.iter_prefix_items (hits):      0.461M ops/sec\n    trie.prefix_items (hits):           0.743M ops/sec\n    trie.prefix_items loop (hits):      0.629M ops/sec\n    trie.iter_prefixes (hits):          0.759M ops/sec\n    trie.iter_prefixes (misses):        1.538M ops/sec\n    trie.iter_prefixes (mixed):         1.359M ops/sec\n    trie.has_keys_with_prefix (hits):   1.896M ops/sec\n    trie.has_keys_with_prefix (misses): 2.590M ops/sec\n    trie.longest_prefix (hits):         1.710M ops/sec\n    trie.longest_prefix (misses):       1.506M ops/sec\n    trie.longest_prefix (mixed):        1.520M ops/sec\n    trie.longest_prefix_item (hits):    1.276M ops/sec\n    trie.longest_prefix_item (misses):  1.292M ops/sec\n    trie.longest_prefix_item (mixed):   1.379M ops/sec\n\nLooking for all words starting with a given prefix is mostly limited\nby overall result count (this can be improved in future because a\nlot of time is spent decoding strings from utf_32_le to Python's\nunicode)::\n\n    trie.items(prefix=\"xxx\"), avg_len(res)==415:        0.609K ops/sec\n    trie.keys(prefix=\"xxx\"), avg_len(res)==415:         0.642K ops/sec\n    trie.values(prefix=\"xxx\"), avg_len(res)==415:       4.974K ops/sec\n    trie.items(prefix=\"xxxxx\"), avg_len(res)==17:       14.781K ops/sec\n    trie.keys(prefix=\"xxxxx\"), avg_len(res)==17:        15.766K ops/sec\n    trie.values(prefix=\"xxxxx\"), avg_len(res)==17:      96.456K ops/sec\n    trie.items(prefix=\"xxxxxxxx\"), avg_len(res)==3:     75.165K ops/sec\n    trie.keys(prefix=\"xxxxxxxx\"), avg_len(res)==3:      77.225K ops/sec\n    trie.values(prefix=\"xxxxxxxx\"), avg_len(res)==3:    320.755K ops/sec\n    trie.items(prefix=\"xxxxx..xx\"), avg_len(res)==1.4:  173.591K ops/sec\n    trie.keys(prefix=\"xxxxx..xx\"), avg_len(res)==1.4:   180.678K ops/sec\n    trie.values(prefix=\"xxxxx..xx\"), avg_len(res)==1.4: 503.392K ops/sec\n    trie.items(prefix=\"xxx\"), NON_EXISTING:             2023.647K ops/sec\n    trie.keys(prefix=\"xxx\"), NON_EXISTING:              1976.928K ops/sec\n    trie.values(prefix=\"xxx\"), NON_EXISTING:            2060.372K ops/sec\n\nRandom insert time is very slow compared to dict, this is the limitation\nof double-array tries; updates are quite fast. If you want to build a trie,\nconsider sorting keys before the insertion::\n\n    dict __setitem__ (updates):            6.497M ops/sec\n    trie __setitem__ (updates):            2.633M ops/sec\n    dict __setitem__ (inserts, random):    5.808M ops/sec\n    trie __setitem__ (inserts, random):    0.053M ops/sec\n    dict __setitem__ (inserts, sorted):    5.749M ops/sec\n    trie __setitem__ (inserts, sorted):    0.624M ops/sec\n    dict setdefault (updates):             3.455M ops/sec\n    trie setdefault (updates):             1.910M ops/sec\n    dict setdefault (inserts):             3.466M ops/sec\n    trie setdefault (inserts):             0.053M ops/sec\n\nOther results (note that ``len(trie)`` is currently implemented\nusing trie traversal)::\n\n    dict __contains__ (hits):    6.801M ops/sec\n    trie __contains__ (hits):    2.816M ops/sec\n    dict __contains__ (misses):  5.470M ops/sec\n    trie __contains__ (misses):  4.224M ops/sec\n    dict __len__:                334336.269 ops/sec\n    trie __len__:                22.900 ops/sec\n    dict values():               406.507 ops/sec\n    trie values():               20.864 ops/sec\n    dict keys():                 189.298 ops/sec\n    trie keys():                 2.773 ops/sec\n    dict items():                48.734 ops/sec\n    trie items():                2.611 ops/sec\n\nPlease take this benchmark results with a grain of salt; this\nis a very simple benchmark and may not cover your use case.\n\nCurrent Limitations\n===================\n\n* keys must be unicode (no implicit conversion for byte strings\n  under Python 2.x, sorry);\n* there are no iterator versions of keys/values/items (this is not\n  implemented yet);\n* it is painfully slow and maybe buggy under pypy;\n* library is not tested with narrow Python builds.\n\nContributing\n============\n\nDevelopment happens at github: https://github.com/pytries/datrie.\n\nFeel free to submit ideas, bugs, pull requests.\n\nRunning tests and benchmarks\n----------------------------\n\nMake sure `tox`_ is installed and run\n\n::\n\n    $ tox\n\nfrom the source checkout. Tests should pass under Python 2.7 and 3.4+.\n\n::\n\n    $ tox -c tox-bench.ini\n\nruns benchmarks.\n\nIf you've changed anything in the source code then\nmake sure `cython`_ is installed and run\n\n::\n\n    $ update_c.sh\n\nbefore each ``tox`` command.\n\nPlease note that benchmarks are not included in the release\ntar.gz's because benchmark data is large and this\nsaves a lot of bandwidth; use source checkouts from\ngithub or bitbucket for the benchmarks.\n\n.. _cython: https://cython.org/\n.. _tox: https://tox.readthedocs.io/\n\nAuthors \u0026 Contributors\n----------------------\n\nSee https://github.com/pytries/datrie/graphs/contributors.\n\nThis module is based on `libdatrie`_ C library by Theppitak Karoonboonyanan\nand is inspired by `fast_trie`_ Ruby bindings, `PyTrie`_ pure\nPython implementation and `Tree::Trie`_ Perl implementation;\nsome docs and API ideas are borrowed from these projects.\n\n.. _fast_trie: https://github.com/tyler/trie\n.. _PyTrie: https://github.com/gsakkis/pytrie\n.. _Tree::Trie: https://metacpan.org/pod/release/AVIF/Tree-Trie-1.9/Trie.pm\n\nLicense\n=======\n\nLicensed under LGPL v2.1.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpytries%2Fdatrie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpytries%2Fdatrie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpytries%2Fdatrie/lists"}