Skip to content

Commit

Permalink
property setter getter
Browse files Browse the repository at this point in the history
  • Loading branch information
SoluMilken committed Mar 13, 2019
1 parent 467cd2e commit 01d1797
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions uttut/pipeline/ops/utils/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ def get_child(self, key):
def insert_child(self, key):
self._children[key] = TrieNode()

def set_word(self, word):
@property
def word(self):
return self._word

@word.setter
def word(self, word):
if not self._word:
self._word = word
else:
raise ValueError('Word exists')

def get_word(self):
return self._word
raise AttributeError('Word can only be set once')


class Trie:
Expand All @@ -37,7 +39,7 @@ def exactly_search(self, word: str):
if not current.has_child(char):
return False
current = current.get_child(char)
if current.get_word():
if current.word:
return True
return False

Expand All @@ -47,7 +49,7 @@ def insert(self, word: str):
if not current.has_child(char):
current.insert_child(char)
current = current.get_child(char)
current.set_word(word)
current.word = word

def match_prefix(self, word: str, shortest: bool = False):
current = self.root
Expand All @@ -56,8 +58,8 @@ def match_prefix(self, word: str, shortest: bool = False):
if not current.has_child(char):
return longest_word
current = current.get_child(char)
if current.get_word():
longest_word = current.get_word()
if current.word:
longest_word = current.word
if shortest:
return longest_word
return longest_word

0 comments on commit 01d1797

Please sign in to comment.