Skip to content

Commit c2e2737

Browse files
authored
Merge pull request #11 from JerilynFranz/tests
Updated examples
2 parents 317f620 + 006f211 commit c2e2737

6 files changed

Lines changed: 59 additions & 32 deletions

File tree

examples/assigning_values_to_entries.py

100644100755
Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@
6868
You can also retrieve all entries that are prefixes for or prefixed by
6969
a given key:
7070
71-
- `trie.prefixed_by(key)` returns a set of `TrieEntry` objects that
71+
- `trie.prefixed_by(key)` returns an Iterator of `TrieEntry` objects that
7272
are prefixed_by of the given key.
73-
- `trie.prefixes(key)` returns a set of `TrieEntry` objects that
73+
- `trie.prefixes(key)` returns an Iterator of `TrieEntry` objects that
7474
are prefixes of the given key.
7575
7676
These methods are useful for searching and retrieving entries
@@ -81,21 +81,22 @@
8181
trie = GeneralizedTrie()
8282

8383
# Adding entries using the trie[key] = value syntax
84-
entries: list[tuple[str, str | list[str]]] = [
84+
entries = [
8585
('abcdef', 'value1'),
8686
('abc', 'value2'),
8787
('abcd', 'value3'),
8888
('qrf', 'value4'),
8989
('xyz', ['lists', 'are', 'also', 'supported']),
9090
('xy', 'value6'),
9191
]
92+
9293
for key, value in entries:
9394
trie[key] = value
9495

95-
prefixed_by: set[TrieEntry] = set(trie.prefixed_by('xy'))
96+
prefixed_by: list[TrieEntry] = list(trie.prefixed_by('xy'))
9697
print(f'prefixed_by = {prefixed_by}')
9798

98-
prefixes: set[TrieEntry] = set(trie.prefixes('abcdefg'))
99+
prefixes: list[TrieEntry] = list(trie.prefixes('abcdefg'))
99100
print(f'prefixes = {prefixes}')
100101

101102
# prefixed_by = {
@@ -111,22 +112,22 @@
111112

112113
# Adding using the add method
113114
# This will throw an error if the key already exists.
114-
more_entries: list[tuple[str | tuple[int, ...], str]] = [
115+
more_entries = [
115116
((128, 96, 160, 0), 'value5'),
116117
((128, 90), 'value5b'),
117118
('xy', 'value6'),
118119
]
119-
for key, value in more_entries:
120+
for different_key, different_value in more_entries:
120121
try:
121-
trie.add(key, value)
122-
print(f'Added entry: {key} -> {value}')
122+
trie.add(different_key, different_value)
123+
print(f'Added entry: {different_key} -> {different_value}')
123124
except DuplicateKeyError:
124-
print(f'DuplicateKeyError - entry already exists: {key}')
125+
print(f'DuplicateKeyError - entry already exists: {different_key}')
125126

126-
prefixed_by: set[TrieEntry] = set(trie.prefixed_by([128]))
127+
prefixed_by = list(trie.prefixed_by([128]))
127128
print(f'prefixed_by = {prefixed_by}')
128129

129-
prefixes: set[TrieEntry] = set(trie.prefixes([128, 90]))
130+
prefixes = list(trie.prefixes([128, 90]))
130131
print(f'prefixes = {prefixes}')
131132

132133
# prefixed_by = {
@@ -149,20 +150,26 @@
149150
for key, value in even_more_entries:
150151
trie.update(key, value)
151152

152-
prefixed_by = set(trie.prefixed_by('abcd'))
153+
prefixed_by = list(trie.prefixed_by('abcd'))
153154
print(f'prefixed_by = {prefixed_by}')
154155

155-
prefixes = set(trie.prefixes('abcdefg'))
156+
prefixes = list(trie.prefixes('abcdefg'))
156157
print(f'prefixes = {prefixes}')
157158

158-
# prefixed_by = {
159-
# TrieEntry(ident=3, key='abcd', value='value8'),
160-
# TrieEntry(ident=9, key='abcdefghi', value='value7'),
161-
# TrieEntry(ident=1, key='abcdef', value='value1')
162-
# }
163-
#
164-
# prefixes = {
165-
# TrieEntry(ident=2, key='abc', value='value2'),
166-
# TrieEntry(ident=3, key='abcd', value='value8'),
167-
# TrieEntry(ident=1, key='abcdef', value='value1')
168-
# }
159+
# prefixed_by = [TrieEntry(ident=TrieId(6), key='xy', value='value6'),
160+
# TrieEntry(ident=TrieId(5), key='xyz', value=['lists', 'are', 'also', 'supported'])]
161+
# prefixes = [TrieEntry(ident=TrieId(2), key='abc', value='value2'),
162+
# TrieEntry(ident=TrieId(3), key='abcd', value='value3'),
163+
# TrieEntry(ident=TrieId(1), key='abcdef', value='value1')]
164+
# Added entry: (128, 96, 160, 0) -> value5
165+
# Added entry: (128, 90) -> value5b
166+
# DuplicateKeyError - entry already exists: xy
167+
# prefixed_by = [TrieEntry(ident=TrieId(8), key=(128, 90), value='value5b'),
168+
# TrieEntry(ident=TrieId(7), key=(128, 96, 160, 0), value='value5')]
169+
# prefixes = [TrieEntry(ident=TrieId(8), key=(128, 90), value='value5b')]
170+
# prefixed_by = [TrieEntry(ident=TrieId(3), key='abcd', value='value8'),
171+
# TrieEntry(ident=TrieId(1), key='abcdef', value='value1'),
172+
# TrieEntry(ident=TrieId(9), key='abcdefghi', value='value7')]
173+
# prefixes = [TrieEntry(ident=TrieId(2), key='abc', value='value2'),
174+
# TrieEntry(ident=TrieId(3), key='abcd', value='value8'),
175+
# TrieEntry(ident=TrieId(1), key='abcdef', value='value1')]

examples/prefixed_by_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env python3
22
"""Example of using a GeneralizedTrie for indexing sequences of strings."""
3-
from typing import Generator
3+
from typing import Iterator
44
from gentrie import GeneralizedTrie, TrieEntry
55

66
trie = GeneralizedTrie()
77
keys: list[str] = ['abcdef', 'abc', 'a', 'abcd', 'qrs']
88
for entry in keys:
99
trie.add(entry)
10-
matches: Generator[TrieEntry, None, None] = trie.prefixed_by('abcd')
10+
matches: Iterator[TrieEntry] = trie.prefixed_by('abcd')
1111

1212
for trie_entry in sorted(list(matches)):
1313
print(f'{trie_entry.ident}: {trie_entry.key}')

examples/prefixes_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/usr/bin/env python3
22
"""Example of using prefixes() method"""
3-
from typing import Generator
3+
from typing import Iterator
44

55
from gentrie import GeneralizedTrie, TrieEntry
66

77
trie: GeneralizedTrie = GeneralizedTrie()
88
keys: list[str] = ['abcdef', 'abc', 'a', 'abcd', 'qrs']
99
for entry in keys:
1010
trie.add(entry)
11-
matches: Generator[TrieEntry, None, None] = trie.prefixes('abcd')
11+
matches: Iterator[TrieEntry] = trie.prefixes('abcd')
1212
for trie_entry in sorted(list(matches)):
1313
print(f'{trie_entry.ident}: {trie_entry.key}')
1414

examples/url_suffixes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
"""Example of using a GeneralizedTrie for indexing website URLs by path"""
3-
from typing import Generator
3+
from typing import Iterator
44

55
from gentrie import GeneralizedTrie, TrieEntry
66

@@ -17,13 +17,13 @@
1717

1818
# Find https URLs with "example.com" domain or sub-domain
1919
print("HTTPS URLs for domains or sub-domains of 'example.com'")
20-
prefixed_by: Generator[TrieEntry, None, None] = url_trie.prefixed_by(["https", ":", "//", "com", "example"])
20+
prefixed_by: Iterator[TrieEntry] = url_trie.prefixed_by(["https", ":", "//", "com", "example"])
2121
for entry in prefixed_by:
2222
print(f"Found URL: {entry.value}")
2323

2424
# Find ftp protocol URLs
2525
print("FTP URLs")
26-
prefixed_by: Generator[TrieEntry, None, None] = url_trie.prefixed_by(["ftp"])
26+
prefixed_by = url_trie.prefixed_by(["ftp"])
2727
for entry in prefixed_by:
2828
print(f"Found URL: {entry.value}")
2929

src/gentrie/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ def __eq__(self, other: object) -> bool:
4949
other.key) and self.value == other.value
5050

5151
def __hash__(self) -> int:
52+
print(f'Hashing TrieEntry: ident={self.ident}, key={self.key}, value={self.value}')
5253
return hash((self.ident, tuple(self.key), self.value)) # pyright: ignore[reportUnknownArgumentType]

tests/gentrie/test_gentri.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,25 @@ def test_prefixed_by(self) -> None:
14201420
]
14211421
run_tests_list(self, tests)
14221422

1423+
# New untouched trie for the next sequence of tests
1424+
# Not using clear() here to keep the clear() tests cleanly separated
1425+
# from the prefixed_by() tests.
1426+
trie = GeneralizedTrie()
1427+
entries = [
1428+
('abcdef', 'value1'),
1429+
('abc', 'value2'),
1430+
('abcd', 'value3'),
1431+
('qrf', 'value4'),
1432+
('xyz', ['lists', 'are', 'also', 'supported']),
1433+
('xy', 'value6'),
1434+
]
1435+
for key, value in entries:
1436+
trie.add(key, value)
1437+
with self.subTest(msg='[TGT_TPB013] trie.prefixed_by("xy")'):
1438+
expected = tuple([trie['xy'], trie['xyz']])
1439+
found = tuple(trie.prefixed_by('xy'))
1440+
self.assertEqual(expected, found, msg=f'Expected {expected}, found {found}')
1441+
14231442
@pytest.mark.order(after=['test_create_trie', 'test_add', 'test_trieid_class', 'test_contains_dunder'])
14241443
@pytest.mark.dependency(
14251444
name='test_deeply_nested_keys',

0 commit comments

Comments
 (0)