Commit 88de4bc3 authored by Jeroen F.J. Laros's avatar Jeroen F.J. Laros

Python 3 support.

parent 343ce8f9
*.pyc
.cache/
.pytest_cache/
.tox/
build/
dist/
......
from itertools import imap
class iMap(imap):
def __next__(cls):
return cls.next()
map = iMap
import itertools
import sys
if sys.version_info.major < 3:
from .compatibility import map
def _add(root, word, count):
......@@ -226,17 +230,17 @@ class Trie(object):
_fill(self.root, alphabet, length)
def all_hamming_(self, word, distance):
return itertools.imap(
return map(
lambda x: (x[0], distance - x[1], x[2]),
_hamming('', self.root, word, distance, ''))
def all_hamming(self, word, distance):
return itertools.imap(
return map(
lambda x: x[0], _hamming('', self.root, word, distance, ''))
def hamming(self, word, distance):
try:
return self.all_hamming(word, distance).next()
return self.all_hamming(word, distance).__next__()
except StopIteration:
return ''
......@@ -259,17 +263,17 @@ class Trie(object):
return ''
def all_levenshtein_(self, word, distance):
return itertools.imap(
return map(
lambda x: (x[0], distance - x[1], x[2]),
_levenshtein('', self.root, word, distance, ''))
def all_levenshtein(self, word, distance):
return itertools.imap(
return map(
lambda x: x[0], _levenshtein('', self.root, word, distance, ''))
def levenshtein(self, word, distance):
try:
return self.all_levenshtein(word, distance).next()
return self.all_levenshtein(word, distance).__next__()
except StopIteration:
return ''
......
......@@ -108,26 +108,26 @@ class TestTrie(object):
assert 'abc' not in self._trie
def test_iter(self):
assert list(self._trie) == ['abc', 'abd', 'te', 'test']
assert set(self._trie) == set(['abc', 'abd', 'te', 'test'])
def test_list(self):
assert list(self._trie.list()) == list(self._trie)
def test_list_non_unique(self):
assert list(self._trie.list(False)) == [
'abc', 'abd', 'abd', 'te', 'test']
assert set(self._trie.list(False)) == set(
['abc', 'abd', 'abd', 'te', 'test'])
def test_fill(self):
trie = Trie()
trie.fill(('a', 'b'), 3)
assert list(trie) == [
'aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bba', 'bbb']
assert set(trie) == set(
['aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bba', 'bbb'])
def test_all_hamming_1_perfect(self):
assert list(self._trie.all_hamming('abc', 1)) == ['abc', 'abd']
assert set(self._trie.all_hamming('abc', 1)) == set(['abc', 'abd'])
def test_all_hamming_1_not_perfect(self):
assert list(self._trie.all_hamming('abx', 1)) == ['abc', 'abd']
assert set(self._trie.all_hamming('abx', 1)) == set(['abc', 'abd'])
def test_all_hamming_1_no_match(self):
assert not list(self._trie.all_hamming('xbx', 1))
......@@ -145,7 +145,7 @@ class TestTrie(object):
assert self._trie.hamming('abcd', 0) == ''
def test_hamming_1_match(self):
assert self._trie.hamming('abc', 1) == 'abc'
assert self._trie.hamming('abc', 1) in ['abc', 'abd']
def test_hamming_1_match_sub(self):
assert self._trie.hamming('te', 1) == 'te'
......@@ -157,10 +157,10 @@ class TestTrie(object):
assert self._trie.hamming('axc', 1) == 'abc'
def test_hamming_1_match_3(self):
assert self._trie.hamming('abx', 1) == 'abc'
assert self._trie.hamming('abx', 1) in ['abc', 'abd']
def test_hamming_1_match_4(self):
assert self._trie.hamming('abd', 1) == 'abc'
assert self._trie.hamming('abd', 1) in ['abc', 'abd']
def test_hamming_1_no_prefix(self):
assert self._trie.hamming('ab', 1) == ''
......@@ -187,7 +187,7 @@ class TestTrie(object):
assert list(self._trie.all_levenshtein('tes', 1)) == ['te', 'test']
def test_levenshtein_0_match_1(self):
assert self._trie.levenshtein('abc', 0) == 'abc'
assert self._trie.levenshtein('abc', 0) in ['abc', 'abd']
def test_levenshtein_0_match_2(self):
assert self._trie.levenshtein('te', 0) == 'te'
......@@ -199,16 +199,16 @@ class TestTrie(object):
assert self._trie.levenshtein('ac', 1) == 'abc'
def test_levenshtein_1_prefix(self):
assert self._trie.levenshtein('ab', 1) == 'abc'
assert self._trie.levenshtein('ab', 1) in ['abc', 'abd']
def test_levenshtein_1_ins(self):
assert self._trie.levenshtein('abbc', 1) == 'abc'
def test_all_hamming__2(self):
assert list(self._trie.all_hamming_('acb', 2)) == [
('abc', 2, '=XX'), ('abd', 2, '=XX')]
assert set(self._trie.all_hamming_('acb', 2)) == set(
[('abc', 2, '=XX'), ('abd', 2, '=XX')])
def test_all_levenshtein__2(self):
assert list(self._trie.all_levenshtein_('acb', 2)) == [
assert set(self._trie.all_levenshtein_('acb', 2)) == set([
('abc', 2, '=D=I'), ('abd', 2, '=D=I'), ('abc', 2, '=XX'),
('abd', 2, '=XX'), ('abc', 2, '=I=D')]
('abd', 2, '=XX'), ('abc', 2, '=I=D')])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment