Skip to content

Commit

Permalink
Merge 2392f87 into cc9ea61
Browse files Browse the repository at this point in the history
  • Loading branch information
tomislater committed Aug 27, 2022
2 parents cc9ea61 + 2392f87 commit a86eb47
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 42 deletions.
23 changes: 10 additions & 13 deletions .travis.yml
@@ -1,23 +1,20 @@
language: python
dist: focal
python:
- '2.7'
- '3.4'
- '3.5'
- '3.6'
- '3.7'
- '3.8'
- '3.7'
- '3.8'
- '3.9'
install:
- python setup.py install
- pip install coverage
- pip install coveralls
- python setup.py install
- pip install coverage
- pip install coveralls
script:
- coverage run --source=random_words setup.py test
- coverage run --source=random_words setup.py test
after_success:
- coveralls
sudo: false
- coveralls
deploy:
provider: pypi
user: swiety
username: swiety
password:
secure: O1/w8g7QwrUPeMy0l5mnFjs2fgUU5XNyVKTWZywZ2t0X8rDACpkJGpj10jK86zi0EcI0cB/NnOSbyQ1ZvVS9clxIjb082kiunvVRFP4iznjeBg3HZ2mA0etU+bobYj4PvGbmJ4bE5fi5/W6hT0zzW++iHDNbZdstjAjHhjmJHa4=
on:
Expand Down
1 change: 1 addition & 0 deletions CHANGES.txt
@@ -1,3 +1,4 @@
v0.4.0, 2022-08-27 -- Add Min Letter Count Feature
v0.3.0, 2020-06-20 -- Fix the mess with versions
v0.2.3, 2020-06-20 -- Use dirname instead of split
v0.2.2, 2020-06-20 -- Add python 3.7 and 3.8 to travis (remove 3.3)
Expand Down
39 changes: 20 additions & 19 deletions random_words/random_words.py
Expand Up @@ -55,17 +55,17 @@ def load_nicknames(self, file):
self.nicknames = json.load(f)

@staticmethod
def check_count(count):
def check_int(a, count):
"""
Checks count
:param int count: count number ;)
:raises: ValueError
"""
if type(count) is not int:
raise ValueError('Param "count" must be int.')
raise ValueError('Param "' + a + '" must be int.')
if count < 1:
raise ValueError('Param "count" must be greater than 0.')
raise ValueError('Param "' + a + '" must be greater than 0.')


class RandomWords(Random):
Expand All @@ -75,56 +75,57 @@ def __init__(self):

super(RandomWords, self).__init__('nouns')

def random_word(self, letter=None):
def random_word(self, letter=None, min_letter_count=1):
"""
Return random word.
:param str letter: letter
:param int min_letter_count: minimum letter count
:rtype: str
:returns: random word
"""
return self.random_words(letter)[0]
return self.random_words(letter, min_letter_count)[0]

def random_words(self, letter=None, count=1):
def random_words(self, letter=None, min_letter_count=1, count=1):
"""
Returns list of random words.
:param str letter: letter
:param int min_letter_count: minimum letter count
:param int count: how much words
:rtype: list
:returns: list of random words
:raises: ValueError
"""
self.check_count(count)
self.check_int("count", count)
self.check_int("min_letter_count", min_letter_count)

words = []

if letter is None:
all_words = list(
chain.from_iterable(self.nouns.values()))
all_words = [w for w in chain.from_iterable(self.nouns.values()) if len(w) >= min_letter_count]

try:
words = sample(all_words, count)
except ValueError:
len_sample = len(all_words)
raise ValueError('Param "count" must be less than {0}. \
(It is only {0} words)'.format(len_sample + 1, letter))
raise ValueError('Param "count" must be less than {0}. (There are only {0} words)'.format(len_sample, letter))

elif type(letter) is not str:
raise ValueError('Param "letter" must be string.')

elif letter not in self.available_letters:
raise ValueError(
'Param "letter" must be in {0}.'.format(
self.available_letters))
'Param "letter" must be in {0}.'.format(self.available_letters))

elif letter in self.available_letters:
all_nouns_letter = [w for w in self.nouns[letter] if len(w) >= min_letter_count]

try:
words = sample(self.nouns[letter], count)
words = sample(all_nouns_letter, count)
except ValueError:
len_sample = len(self.nouns[letter])
raise ValueError('Param "count" must be less than {0}. \
(It is only {0} words for letter "{1}")'.format(len_sample + 1, letter))
len_sample = len(all_nouns_letter)
raise ValueError('Param "count" must be less than {0}. (There are only {0} words for letter "{1}")'.format(len_sample, letter))

return words

Expand Down Expand Up @@ -161,7 +162,7 @@ def random_nicks(self, letter=None, gender='u', count=1):
:returns: list of random nicks
:raises: ValueError
"""
self.check_count(count)
self.check_int("count", count)

nicks = []

Expand Down Expand Up @@ -222,7 +223,7 @@ def randomMails(self, count=1):
:rtype: list
:returns: list of random e-mails
"""
self.check_count(count)
self.check_int("count", count)

random_nicks = self.rn.random_nicks(count=count)
random_domains = sample(list(self.dmails), count)
Expand Down
56 changes: 51 additions & 5 deletions random_words/test/test_random_words.py
Expand Up @@ -22,34 +22,80 @@ def test_random_word(self):
word = self.rw.random_word(letter)
assert word[0] == letter

def test_random_word_with_min_letter_count(self):
for letter in self.letters:
word = self.rw.random_word(letter, min_letter_count=5)
assert word[0] == letter

def test_random_word_large_min_letter_count(self):
"""
min_letter_count is too large.
"""
for letter in self.letters:
pytest.raises(ValueError, self.rw.random_word, letter, min_letter_count=3443)

def test_random_word_value_error(self):
pytest.raises(ValueError, self.rw.random_word, 'x')
pytest.raises(ValueError, self.rw.random_word, 0)
pytest.raises(ValueError, self.rw.random_word, -1)
pytest.raises(ValueError, self.rw.random_word, 9)
pytest.raises(ValueError, self.rw.random_word, ['u'])
pytest.raises(ValueError, self.rw.random_word, 'fs')

pytest.raises(ValueError, self.rw.random_word, min_letter_count=-1)
pytest.raises(ValueError, self.rw.random_word, min_letter_count=None)
pytest.raises(ValueError, self.rw.random_word, min_letter_count="fds")

def test_random_words(self):
for letter in self.letters:
words = self.rw.random_words(letter)
for word in words:
assert word[0] == letter

def test_random_words_value_error(self):
def test_random_words_value_error_letter(self):
pytest.raises(ValueError, self.rw.random_words, 'fa')
pytest.raises(ValueError, self.rw.random_words, ['fha'])
pytest.raises(ValueError, self.rw.random_words, 0)
pytest.raises(ValueError, self.rw.random_words, -1)

pytest.raises(ValueError, self.rw.random_words, letter=None,
count=1000000)

pytest.raises(ValueError, self.rw.random_words, letter=None, count=1000000)

def test_random_words_value_error_count(self):
pytest.raises(ValueError, self.rw.random_words, count=0)
pytest.raises(ValueError, self.rw.random_words, count=None)
pytest.raises(ValueError, self.rw.random_words, count=[8])
pytest.raises(ValueError, self.rw.random_words, count=-5)

def test_random_words_value_error_min_letter_count(self):
pytest.raises(ValueError, self.rw.random_words, min_letter_count=0)
pytest.raises(ValueError, self.rw.random_words, min_letter_count=None)
pytest.raises(ValueError, self.rw.random_words, min_letter_count=[8])
pytest.raises(ValueError, self.rw.random_words, min_letter_count=-5)

def test_random_words_large_min_letter_count(self):
min_letter_count = 15
words = self.rw.random_words(count=10, min_letter_count=min_letter_count)
for w in words:
assert len(w) >= min_letter_count

def test_random_words_small_min_letter_count(self):
min_letter_count = 3
words = self.rw.random_words(count=1000, min_letter_count=min_letter_count)
for w in words:
assert len(w) >= min_letter_count

def test_random_words_z_letter_min_letter_count(self):
"""
There are not enough words for such query.
"""
pytest.raises(ValueError, self.rw.random_words, "z", count=5, min_letter_count=15)

def test_random_words_min_letter_count(self):
for letter in self.letters:
min_letter_count = random.randint(1, 5)
words = self.rw.random_words(letter, count=2, min_letter_count=min_letter_count)
for w in words:
assert len(w) >= min_letter_count

def test_random_words_length_list(self):
len_words = len(self.rw.random_words())
assert len_words == 1
Expand Down
7 changes: 2 additions & 5 deletions setup.py
Expand Up @@ -20,7 +20,7 @@ def run_tests(self):

setup(
name='RandomWords',
version='0.3.0',
version='0.4.0',
author='Tomek Święcicki',
author_email='tomislater@gmail.com',
packages=['random_words'],
Expand All @@ -34,12 +34,9 @@ def run_tests(self):
'Programming Language :: Python',
'Natural Language :: English',
'Development Status :: 5 - Production/Stable',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Topic :: Software Development :: Libraries :: Python Modules',
],
include_package_data=True,
Expand Down

0 comments on commit a86eb47

Please sign in to comment.