Skip to content

Commit

Permalink
Word lists can be combined using the plus operator (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaher86 authored Aug 21, 2022
1 parent b97ed45 commit 1434d8c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
9 changes: 6 additions & 3 deletions blacksquare/word_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
from collections import defaultdict
from functools import lru_cache
from pathlib import Path
from typing import (TYPE_CHECKING, Callable, Dict, List, NamedTuple, Optional,
Union)
from xml.dom import INVALID_ACCESS_ERR
from typing import TYPE_CHECKING, Callable, Dict, List, NamedTuple, Optional, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -237,6 +235,11 @@ def __next__(self):
else:
raise StopIteration

def __add__(self, other):
return WordList(
{w: s for w, s in zip(self.words + other.words, self.scores + other.scores)}
)


class MatchWordList(WordList):
"""An object representing a WordList matching an open word. This class should not be
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = blacksquare
version = 0.3.1
version = 0.4.0
author = Patrick Maher
author_email = [email protected]
description = "A package for creating crossword puzzles"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_word_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def test_get_score(self, word_list):
assert word_list.get_score("BBB") == 0.1
assert word_list.get_score("XYZ") is None

def test_add_word_list(self):
word_list_1 = {"ZZZ": 0.1, "AAA": 1.0}
word_list_2 = {"ABC": 0.9, "XYZ": 1.0}
new_word_list = WordList(word_list_1) + WordList(word_list_2)
assert new_word_list.words == ["AAA", "XYZ", "ABC", "ZZZ"]
assert new_word_list.scores == [1.0, 1.0, 0.9, 0.1]


class TestMatchWordList:
@pytest.fixture
Expand Down

0 comments on commit 1434d8c

Please sign in to comment.