commit eb60a63edc215538a1c1043fbdcac59f8718ee20
parent 72a9c67c17da70ce7e15c3f1e75b6753bddad512
Author: amin <dev@aminmesbah.com>
Date: Mon, 9 Jan 2017 20:34:33 +0000
Add logging.
FossilOrigin-Name: 70104619c331952502f7791b8fe968abc5c8e9fbe41ed8f1a88b35dc7658bb90
Diffstat:
3 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/Makefile b/Makefile
@@ -9,7 +9,7 @@ test:
py.test tests.py
lint:
- flake8 *.py
+ flake8 *.py --max-line-length 90
watch-log:
tail -f debug.log
diff --git a/elemental_speller.py b/elemental_speller.py
@@ -1,8 +1,15 @@
-# TODO: add logging
-
-from collections import namedtuple
from itertools import chain, product
+import logging
+
+# TODO(amin): Profile and optimize
+# TODO(amin): Add performance reporting to log
+# TODO(amin): Use recursion to save time with long words that can't be spelled.
+# TODO(amin): Convert symbol tuple to element name or atomic number tuple
+
+log = logging.getLogger(__name__)
+log.addHandler(logging.NullHandler())
+# TODO(amin): Ensure elements are up to date (Fl, Lv)
ELEMENTS = (
'Ac', 'Ag', 'Al', 'Am', 'Ar', 'As', 'At', 'Au', 'B', 'Ba', 'Be', 'Bh',
'Bi', 'Bk', 'Br', 'C', 'Ca', 'Cd', 'Ce', 'Cf', 'Cl', 'Cm', 'Co', 'Cr',
@@ -17,6 +24,9 @@ ELEMENTS = (
)
+# TODO(amin): Use optional caching/memoization to improve performance
+# TODO(amin): Support appostrophies
+# TODO(amin): Add option to require no repeated symbols
def elemental_spelling(word, symbols=ELEMENTS):
"""Given a word and a sequence of symbols (tokens),
return a list of any possible ways to spell that word
@@ -24,8 +34,9 @@ def elemental_spelling(word, symbols=ELEMENTS):
Example:
>>> elemental_spelling('amputation')
- [(('Am', 'Pu', 'Ta', 'Ti', 'O', 'N'), ('Am', 'P', 'U', 'Ta', 'Ti', 'O', 'N')]
+ [('Am', 'Pu', 'Ta', 'Ti', 'O', 'N'), ('Am', 'P', 'U', 'Ta', 'Ti', 'O', 'N')]
"""
+ log.info('Word: {}'.format(word))
letter_groupings = _groupings(len(word))
spellings = [_map_word(word, grouping) for grouping in letter_groupings]
@@ -37,10 +48,12 @@ def elemental_spelling(word, symbols=ELEMENTS):
if set(s.lower() for s in spelling) <= set(s.lower() for s in symbols)
]
+ log.info('Spellings: {}'.format(elemental_spellings))
+
return elemental_spellings
-def _groupings(word_length, token_sizes=(1, 2, 3)):
+def _groupings(word_length, token_sizes=(1, 2)):
"""Return a tuple of all character groupings for a word
of a given length.
@@ -58,6 +71,7 @@ def _groupings(word_length, token_sizes=(1, 2, 3)):
((2, 2), (1, 1, 2), (1, 2, 1), (2, 1, 1), (1, 1, 1, 1))
"""
+ # TODO(amin): Why do I need to add 1 to word_length?
cartesian_products = (
product(token_sizes, repeat=r)
for r in range(1, word_length + 1)
@@ -70,9 +84,12 @@ def _groupings(word_length, token_sizes=(1, 2, 3)):
if sum(grouping) == word_length
)
+ log.debug('Groupings: {}'.format(groupings))
+
return groupings
+# TODO(amin): Handle failure cases (grouping doesn't add up to word length)
def _map_word(word, grouping):
"""Return a tuple of tokens: word mapped to a grouping.
@@ -90,6 +107,8 @@ def _map_word(word, grouping):
char_group += next(word_chars)
mapped.append(char_group)
+ log.debug('Grouping: {}. Mapped word: {}'.format(grouping, mapped))
+
return tuple(mapped)
diff --git a/tests.py b/tests.py
@@ -1,5 +1,4 @@
import elemental_speller as es
-import pytest
ELEMENTS = (
'Ac', 'Ag', 'Al', 'Am', 'Ar', 'As', 'At', 'Au', 'B', 'Ba', 'Be', 'Bh',