189 8069 5689

怎么用Python开发Emoji表情查找程序

本篇内容介绍了“怎么用Python开发Emoji表情查找程序”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都做网站、望奎网络推广、小程序开发、望奎网络营销、望奎企业策划、望奎品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供望奎建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

今天分享一个前几天构建的小应用程序,用来从命令行搜索emoji表情符号。

它可以通过OS命令行来完成,而且不必单击任何东西即可获得我的表情符号,更加便捷。

该工具支持一次将多个匹配的表情符号复制到剪贴板。

$ 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

至此,我的剪贴板上所有4个表情符号都写好了,在键盘输入Cmd + V即可。

是不是很酷?

安装并运行程序包

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"

您也可以通过添加以下shell别名来使调用命令更短(就像我在第一个示例中一样):

$ 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)

感谢这个库的作者AlSweigart,这是一个很酷的程序包。

如何查找多个表情符号?

在这种情况下,我通过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

键入“snake(蛇)”后出现的emoji不会出错,但是对于“flag(旗帜)”,它默认选择12个匹配项中的第一个(对于“heart(心脏)”,我们会得到130个匹配的表情符号!),这里我想手动选择一个,因此键入点".",以做出进一步的选择。

测试

还有几件事:

@ 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。

上传到PyPI

感谢toml文件中[tool.poetry]中的一些基本元数据,发布到PyP非常简单:

poetry build  poetry publish

(首先使用--repository of publish在测试PyPI上尝试一下,看是否一切正常。)

“怎么用Python开发Emoji表情查找程序”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网站栏目:怎么用Python开发Emoji表情查找程序
转载来源:http://cdxtjz.cn/article/gcodci.html

其他资讯