Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/bleach/_vendor/html5lib/_trie/_base.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 from __future__ import absolute_import, division, unicode_literals | |
2 | |
3 try: | |
4 from collections.abc import Mapping | |
5 except ImportError: # Python 2.7 | |
6 from collections import Mapping | |
7 | |
8 | |
9 class Trie(Mapping): | |
10 """Abstract base class for tries""" | |
11 | |
12 def keys(self, prefix=None): | |
13 # pylint:disable=arguments-differ | |
14 keys = super(Trie, self).keys() | |
15 | |
16 if prefix is None: | |
17 return set(keys) | |
18 | |
19 return {x for x in keys if x.startswith(prefix)} | |
20 | |
21 def has_keys_with_prefix(self, prefix): | |
22 for key in self.keys(): | |
23 if key.startswith(prefix): | |
24 return True | |
25 | |
26 return False | |
27 | |
28 def longest_prefix(self, prefix): | |
29 if prefix in self: | |
30 return prefix | |
31 | |
32 for i in range(1, len(prefix) + 1): | |
33 if prefix[:-i] in self: | |
34 return prefix[:-i] | |
35 | |
36 raise KeyError(prefix) | |
37 | |
38 def longest_prefix_item(self, prefix): | |
39 lprefix = self.longest_prefix(prefix) | |
40 return (lprefix, self[lprefix]) |