Skip to content

Commit

Permalink
Add packaging files
Browse files Browse the repository at this point in the history
  • Loading branch information
ermanh committed Jun 22, 2020
1 parent c5a6934 commit bff871c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 22 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
__pycache__/**
.cache/**
.pytest_cache/**
__pycache__/
.cache/
.pytest_cache/
*.pyc

*.egg-info/
*.egg
dist/
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Herman Leung

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include *.md
include LICENSE.txt
Empty file added README.md
Empty file.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
license_files = LICENSE.txt
23 changes: 18 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@

setup(
name='trieregex',
version='',
description='Compose efficient regexes from a large list of keywords',
version='1.0.0',
description='Compose efficient trie-based regexes from large word lists',
long_description='',
author='Herman Leung',
author_email='',
author_email='[email protected]',
url='https://github.com/ermanh/trieregex',
license='',
packages=find_packages() # exclude=('tests', 'docs'))
packages=find_packages(exclude='tests'),
python_requires='>=3.6',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],
keywords=['python', 'regular expressions', 'regex', 'pattern', 'trie'],
license='MIT',
)
18 changes: 5 additions & 13 deletions trieregex/trieregex.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@ def __init__(self, *words: str) -> None:
self._finals = defaultdict(int)
self.add(*words)

def _adjust_initials_finals(self, word, increase=True):
if increase:
self._initials[word[0]] += 1
self._finals[word[-1]] += 1
else:
self._initials[word[0]] -= 1
self._finals[word[-1]] -= 1

@Memoizer
def add(self, *words: str) -> None:
self.regex.clear_cache()
for word in words:
if word != '' and not self.has(word):
self._adjust_initials_finals(word)
self._initials[word[0]] += 1
self._finals[word[-1]] += 1
trie = self._trie
for char in word:
if char not in trie:
Expand All @@ -44,7 +37,8 @@ def remove(self, *words: str) -> None:
is_end = i == len(word)
if is_end and self.has(word[:i]):
remove_word = True
self._adjust_initials_finals(word, increase=False)
self._initials[word[0]] -= 1
self._finals[word[-1]] -= 1
if remove_word:
node = self._trie
for j in range(i-1):
Expand All @@ -69,9 +63,7 @@ def has(self, word: str) -> bool:
trie = trie[char]
else:
return False
if '**' not in trie:
return False
return True
return True if ('**' in trie) else False

def initials(self) -> List[str]:
result = [key for key in self._initials if self._initials[key] > 0]
Expand Down

0 comments on commit bff871c

Please sign in to comment.