Skip to content

Commit

Permalink
Started work on Census class for #4
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsv committed Jun 14, 2018
1 parent dff93c4 commit ad67390
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ety/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from . import data
from .tree import EtyTree
from .census import Census
from .word import Word, Language # noqa: F401


Expand Down Expand Up @@ -62,3 +63,7 @@ def random_word(lang='eng'):
row = list(filter(lambda entry: entry['a_lang'] == lang, data.etyms))
word = choice(row)['a_word']
return Word(word, lang)


def census(words):
return Census(words)
30 changes: 30 additions & 0 deletions ety/census.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import re

from .word import Word

string_types = ("".__class__, u"".__class__)


class Census(object):
def __init__(self, words, lang='eng'):
if isinstance(words, ("".__class__, u"".__class__)):
words = re.split('\s+', words)
elif isinstance(words, (list, tuple)):
words = list(words)
else:
raise ValueError('words argument must be either string or list')

assert type(words) is list

self.words = []
for word in list(words):
if isinstance(word, string_types):
self.words.append(Word(word, lang))
elif isinstance(word, Word):
self.words.append(word)
else:
raise ValueError("Invalid word type: '%s'.\ Words must\
be ety.Word objects or strings" % str(type(word)))

def __eq__(self, other):
return self.words == other.words
8 changes: 8 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def test_tree(self):
self.assertGreaterEqual(len(str(
ety.tree('fabric')).split('\n')), 4)

def test_census_words(self):
a = ety.census(['alphabet', 'avocado', 'guitar'])
b = ety.census('alphabet avocado guitar')
self.assertEqual(a, b)
self.assertTrue(ety.Word('avocado') in a.words)
with self.assertRaises(ValueError):
ety.census(['valid', ety.Word('stillvalid'), 12345])


if __name__ == '__main__':
unittest.main()

0 comments on commit ad67390

Please sign in to comment.