用 Python 开发 emoji 表情查找程序
Python中文社区
共 4698字,需浏览 10分钟
·
2021-05-11 21:45
$ emo
------------------------------------------------------------------------------------
Type one or more emoji related words ...
End a word with a . if you want to select an emoji if there are multiple
matches, otherwise the first match will be picked. Type 'q' to exit.
> snake beer fire ninja
Copying 🐍 🍺 🔥 🥷 to clipboard
------------------------------------------------------------------------------------
Type one or more emoji related words ...
End a word with a . if you want to select an emoji if there are multiple
matches, otherwise the first match will be picked. Type 'q' to exit.
> q
Bye
git clone git@github.com:PyBites-Open-Source/emojisearcher.git
cd emojisearcher
poetry install
poetry run emo
poetry
使依赖项管理变得轻而易举,最后一个命令(别名)实际上有效,因为我将其放在pyproject.toml
文件中:[tool.poetry.scripts]
emo = "emojisearcher.script:main"
$ alias emo
alias emo='cd YOUR_PATH/emojisearcher && poetry run emo'
YOUR_PATH
更改为项目的路径。)poetry new
,文件夹结构从一开始就遵循了公认的最佳做法。tests /
文件夹中。emoji
库中的EMOJI_UNICODE
常量来查找emoji表情:...
EMOJI_MAPPING = EMOJI_UNICODE[LANGUAGE]
...
def get_emojis_for_word(
word: str, emoji_mapping: dict[str, str] = EMOJI_MAPPING
) -> list[str]:
return [emo for name, emo in emoji_mapping.items() if word in name]
pyperclip
复制到操作系统的剪贴板中:from pyperclip import copy
...
def copy_emojis_to_clipboard(matches: list[str]) -> None:
all_matching_emojis = ' '.join(matches)
print(f"Copying {all_matching_emojis} to clipboard")
copy(all_matching_emojis)
user_select_emoji
函数进入交互模式。SIGNAL_CHAR
):如果用户的搜索字符串以点(.
)结尾,它将进入交互模式。$ emo
------------------------------------------------------------------------------------
Type one or more emoji related words ...
End a word with a . if you want to select an emoji if there are multiple
matches, otherwise the first match will be picked. Type 'q' to exit.
> snake
Copying 🐍 to clipboard
------------------------------------------------------------------------------------
Type one or more emoji related words ...
End a word with a . if you want to select an emoji if there are multiple
matches, otherwise the first match will be picked. Type 'q' to exit.
> flag
Copying 🏴 to clipboard
------------------------------------------------------------------------------------
Type one or more emoji related words ...
End a word with a . if you want to select an emoji if there are multiple
matches, otherwise the first match will be picked. Type 'q' to exit.
> flag.
1 🏴
2 🏁
3 📪
4 📫
5 🎌
6 ⛳
7 📭
8 📬
9 🏴☠️
10 🏳️🌈
11 🏳️⚧️
12 🚩
13 🏳
Select the number of the emoji you want: 12
Copying 🚩 to clipboard
------------------------------------------------------------------------------------
Type one or more emoji related words ...
End a word with a . if you want to select an emoji if there are multiple
matches, otherwise the first match will be picked. Type 'q' to exit.
> q
Bye
@ pytest.mark.parametrize
非常好,可以使您的测试代码更加简洁。将代码分解为更多的功能使其更可重用且更易于测试。 我测试了使用 @patch(“ builtins.input”,side_effect = ['a',10,2,'q']
的交互模式模拟input
的方法。side_effect
中的列表包含将“double”input
的参数。这等效于以下内容(在键入tree
之后。):
$ emo
------------------------------------------------------------------------------------
Type one or more emoji related words ...
End a word with a . if you want to select an emoji if there are multiple
matches, otherwise the first match will be picked. Type 'q' to exit.
> tree.
1 🎄
2 🌳
3 🌲
4 🌴
5 🎋
Select the number of the emoji you want: a
a is not an integer.
1 🎄
2 🌳
3 🌲
4 🌴
5 🎋
Select the number of the emoji you want: 10
10 is not a valid option.
1 🎄
2 🌳
3 🌲
4 🌴
5 🎋
Select the number of the emoji you want: 2
Copying 🌳 to clipboard
------------------------------------------------------------------------------------
Type one or more emoji related words ...
End a word with a . if you want to select an emoji if there are multiple
matches, otherwise the first match will be picked. Type 'q' to exit.
> q
Bye
测试代码时,一种有用的技术是删除所有常见的前导空白。您可以为此使用 textwrap.dedent
,但是在这里我使用了替代的inspect.cleandoc
。
toml
文件中[tool.poetry]
中的一些基本元数据,发布到PyP非常简单:poetry build
poetry publish
--repository of publish
在测试PyPI上尝试一下,看是否一切正常。)https://github.com/PyBites-Open-Source/emojisearcher
更多阅读
特别推荐
点击下方阅读原文加入社区会员
评论