-
Notifications
You must be signed in to change notification settings - Fork 132
Implement discard and remove function #19
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
I signed it! |
We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s). If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. |
a638b6a
to
fd72748
Compare
CLAs look good, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also needs tests and documentation. And some good documentation because semantics of removing elements from a prefix set aren’t obvious. For example, what should be the result of the following:
ps = pygtrie.PrefixSet(factory=pygtrie.StringTrie)
ps.add('foo/bar/baz')
assert 'foo/bar/baz/qux' in ps
ps.remove('foo/bar/baz/qux') # Should it throw?
'foo/bar/baz/qux' in ps # Is this True?
try: | ||
node, _ = self._get_node(key) | ||
except: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearly, this pass
should be return
, no?
try: | ||
node, _ = self._get_node(key) | ||
except KeyError: | ||
return 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh? Why are you catching the exception? It should be propagated.
More importantly, why are you duplicating the implementation? It’s much better to implement discard
in terms of remove
, i.e.:
def remove(self, key):
…
def discard(self, key):
try:
self.remove(key)
except KeyError:
pass
except: | ||
pass | ||
|
||
if node and node.children: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t understand why you have this special case. node.children = {}
is enough to remove all the children, you don’t have to iterate over them.
self.discard(child) | ||
else: | ||
node.children = {} | ||
node.value = _SENTINEL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This leaves out empty nodes which don’t lead to anything. For example, if you first add foo/bar/baz
you get a prefix set with three nodes:
foo → bar → baz
Only baz
’s value is set. If you now remove foo/bar
you end up with an empty set which nonetheless has two nodes:
foo → bar
No description provided.