-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from hmasdev/add-printer-selection
Update printer functions and game configuration
- Loading branch information
Showing
4 changed files
with
67 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
from werewolf.utils.printer import ( | ||
_print_dict, | ||
create_print_func, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'key,expected', | ||
list(_print_dict.items()), | ||
) | ||
def test_create_print_func(key, expected): | ||
assert create_print_func(key) is expected | ||
|
||
|
||
def test_create_print_func_invalid_key(): | ||
with pytest.raises(ValueError): | ||
create_print_func('invalid_key') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from functools import partial | ||
import logging | ||
from typing import Callable | ||
import click | ||
|
||
|
||
_print_dict: dict[str, Callable[..., None]] = { | ||
'print': print, | ||
'click.echo': click.echo, | ||
'logging.info': logging.info, | ||
} | ||
|
||
KEYS_FOR_PRINTER: tuple[str, ...] = tuple(_print_dict.keys()) | ||
|
||
|
||
def create_print_func(key: str, **kwargs) -> Callable[..., None]: | ||
"""Create a print function. | ||
Args: | ||
key (str): key of the print function | ||
**kwargs: keyword arguments to pass to the print function | ||
Returns: | ||
Callable[..., None]: print function | ||
""" | ||
try: | ||
if kwargs: | ||
return partial(_print_dict[key], **kwargs) | ||
return _print_dict[key] | ||
except KeyError: | ||
raise ValueError(f'Invalid key: {key}. Valid keys are {list(KEYS_FOR_PRINTER)}') # noqa |