Skip to content

Commit 0d56331

Browse files
committed
Allow Counter as all_words type
1 parent 9881590 commit 0d56331

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

tests/test_wrdcld.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import Counter
12
from string import ascii_letters, digits, punctuation
23
from unittest import TestCase
34

@@ -56,8 +57,9 @@ def test_make_word_cloud(self, words: list[str]):
5657
@settings(deadline=None)
5758
@given(words=words_with_repeats_strategy(), seed=st.integers())
5859
def test_word_cloud_reproducibility(self, words: list[str], seed: int):
60+
counter = Counter(words)
5961

6062
word_cloud_1 = make_word_cloud(words, seed=seed)
61-
word_cloud_2 = make_word_cloud(words, seed=seed)
63+
word_cloud_2 = make_word_cloud(counter, seed=seed)
6264

6365
self.assertTrue(_two_images_are_equal(word_cloud_1, word_cloud_2))

wrdcld/__init__.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# pylint: disable=unused-argument
1818
def make_word_cloud(
19-
all_words: list[str],
19+
all_words: list[str] | Counter,
2020
width: int = 500,
2121
height: int = 500,
2222
font_path: Path | None = None,
@@ -32,6 +32,7 @@ def make_word_cloud(
3232
):
3333
if seed is not None:
3434
random.seed(seed)
35+
3536
# Asserts
3637
assert len(all_words) > 0, "No words in list"
3738
assert width > 0, "Width must be a positive number (in pixels)"
@@ -42,9 +43,13 @@ def make_word_cloud(
4243
assert (
4344
font_color is not None or font_color_func is not None
4445
), "Must specify a fixed font color or function"
46+
assert isinstance(all_words, (list, Counter)), "Word supply must be list or Counter"
4547

4648
# Count the words
47-
word_counts = Counter(all_words)
49+
if isinstance(all_words, list):
50+
word_counts = Counter(all_words)
51+
else:
52+
word_counts = all_words
4853
first_word, first_count = word_counts.most_common(1)[0]
4954

5055
# Create a new image and font

0 commit comments

Comments
 (0)