]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/mw/util/tests/test_autovivifying.py
Initial commit
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / mw / util / tests / test_autovivifying.py
1 from nose.tools import eq_
2
3 from .. import autovivifying
4
5
6 def test_word_count():
7     words = """
8     I am a little teapot short and stout.  Here is my handle and here is my
9     spout.  The red fox jumps over the lazy brown dog.  She sells sea shells
10     by the sea shore.
11     """.replace(".", " ").split()
12
13     # Lame way
14     lame_counts = {}
15     for word in words:
16         if word not in lame_counts:
17             lame_counts[word] = 0
18
19         lame_counts[word] += 1
20
21     # Awesome way
22     awesome_counts = autovivifying.Dict(  # Autovivifies entries with zero.
23                                           vivifier=lambda k: 0  # Useful for counting.
24     )
25     for word in words:
26         awesome_counts[word] += 1
27
28     eq_(lame_counts, awesome_counts)

Community Data Science Collective || Want to submit a patch?