Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datrie.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from __future__ import absolute_import, division, unicode_literals
  2. from datrie import Trie as DATrie
  3. from pip._vendor.six import text_type
  4. from ._base import Trie as ABCTrie
  5. class Trie(ABCTrie):
  6. def __init__(self, data):
  7. chars = set()
  8. for key in data.keys():
  9. if not isinstance(key, text_type):
  10. raise TypeError("All keys must be strings")
  11. for char in key:
  12. chars.add(char)
  13. self._data = DATrie("".join(chars))
  14. for key, value in data.items():
  15. self._data[key] = value
  16. def __contains__(self, key):
  17. return key in self._data
  18. def __len__(self):
  19. return len(self._data)
  20. def __iter__(self):
  21. raise NotImplementedError()
  22. def __getitem__(self, key):
  23. return self._data[key]
  24. def keys(self, prefix=None):
  25. return self._data.keys(prefix)
  26. def has_keys_with_prefix(self, prefix):
  27. return self._data.has_keys_with_prefix(prefix)
  28. def longest_prefix(self, prefix):
  29. return self._data.longest_prefix(prefix)
  30. def longest_prefix_item(self, prefix):
  31. return self._data.longest_prefix_item(prefix)