用 Python 开发 Emoji 表情查找程序

开发 后端
今天分享一个前几天构建的小应用程序,用来从命令行搜索emoji表情符号。它可以通过OS命令行来完成,而且不必单击任何东西即可获得我的表情符号,更加便捷。

[[398183]]

本文转载自微信公众号「Python中文社区」,作者Python中文社区。转载本文请联系Python中文社区公众号。

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

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

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

  1. $ emo 
  2.  
  3. ------------------------------------------------------------------------------------ 
  4. Type one or more emoji related words ... 
  5. End a word with a . if you want to select an emoji if there are multiple 
  6. matches, otherwise the first match will be picked. Type 'q' to exit. 
  7. > snake beer fire ninja 
  8. Copying 🐍 🍺 🔥 🥷 to clipboard 
  9.  
  10. ------------------------------------------------------------------------------------ 
  11. Type one or more emoji related words ... 
  12. End a word with a . if you want to select an emoji if there are multiple 
  13. matches, otherwise the first match will be picked. Type 'q' to exit. 
  14. > q 
  15. Bye 

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

是不是很酷?

安装并运行程序包

  1. git clone git@github.com:PyBites-Open-Source/emojisearcher.git 
  2. cd emojisearcher 
  3. poetry install 
  4. poetry run emo 

poetry使依赖项管理变得轻而易举,最后一个命令(别名)实际上有效,因为我将其放在pyproject.toml文件中:

  1. [tool.poetry.scripts] 
  2. emo = "emojisearcher.script:main" 

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

  1. $ alias emo 
  2. alias emo='cd YOUR_PATH/emojisearcher && poetry run emo' 

(将YOUR_PATH更改为项目的路径。)

文件夹结构

由于有了poetry new,文件夹结构从一开始就遵循了公认的最佳做法。

我喜欢将测试文件放在专用的tests /文件夹中。

我使用emoji库中的EMOJI_UNICODE常量来查找emoji表情:

  1. ... 
  2. EMOJI_MAPPING = EMOJI_UNICODE[LANGUAGE] 
  3.  
  4. ... 
  5. def get_emojis_for_word( 
  6.     word: str, emoji_mapping: dict[str, str] = EMOJI_MAPPING 
  7. ) -> list[str]: 
  8.     return [emo for name, emo in emoji_mapping.items() if word in name

然后我使用pyperclip复制到操作系统的剪贴板中:

  1. from pyperclip import copy 
  2. ... 
  3. def copy_emojis_to_clipboard(matches: list[str]) -> None: 
  4.     all_matching_emojis = ' '.join(matches) 
  5.     print(f"Copying {all_matching_emojis} to clipboard"
  6.     copy(all_matching_emojis) 

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

如何查找多个表情符号?

在这种情况下,我通过user_select_emoji函数进入交互模式。

我想用一种创新的方式来触发此交互模式,为此选择了信号字符(SIGNAL_CHAR):如果用户的搜索字符串以点(.)结尾,它将进入交互模式。

原因如下:

  1. $ emo 
  2.  
  3. ------------------------------------------------------------------------------------ 
  4. Type one or more emoji related words ... 
  5. End a word with a . if you want to select an emoji if there are multiple 
  6. matches, otherwise the first match will be picked. Type 'q' to exit. 
  7. > snake 
  8. Copying 🐍 to clipboard 
  9.  
  10. ------------------------------------------------------------------------------------ 
  11. Type one or more emoji related words ... 
  12. End a word with a . if you want to select an emoji if there are multiple 
  13. matches, otherwise the first match will be picked. Type 'q' to exit. 
  14. > flag 
  15. Copying 🏴 to clipboard 
  16.  
  17. ------------------------------------------------------------------------------------ 
  18. Type one or more emoji related words ... 
  19. End a word with a . if you want to select an emoji if there are multiple 
  20. matches, otherwise the first match will be picked. Type 'q' to exit. 
  21. > flag. 
  22. 1 🏴 
  23. 2 🏁 
  24. 3 📪 
  25. 4 📫 
  26. 5 🎌 
  27. 6 ⛳ 
  28. 7 📭 
  29. 8 📬 
  30. 9 🏴‍☠️ 
  31. 10 🏳️‍🌈 
  32. 11 🏳️‍⚧️ 
  33. 12 🚩 
  34. 13 🏳 
  35. Select the number of the emoji you want: 12 
  36. Copying 🚩 to clipboard 
  37.  
  38. ------------------------------------------------------------------------------------ 
  39. Type one or more emoji related words ... 
  40. End a word with a . if you want to select an emoji if there are multiple 
  41. matches, otherwise the first match will be picked. Type 'q' to exit. 
  42. > q 
  43. 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之后。):

  1. $ emo 
  2.  
  3. ------------------------------------------------------------------------------------ 
  4. Type one or more emoji related words ... 
  5. End a word with a . if you want to select an emoji if there are multiple 
  6. matches, otherwise the first match will be picked. Type 'q' to exit. 
  7. > tree. 
  8. 1 🎄 
  9. 2 🌳 
  10. 3 🌲 
  11. 4 🌴 
  12. 5 🎋 
  13. Select the number of the emoji you want: a 
  14. is not an integer
  15. 1 🎄 
  16. 2 🌳 
  17. 3 🌲 
  18. 4 🌴 
  19. 5 🎋 
  20. Select the number of the emoji you want: 10 
  21. 10 is not a valid option
  22. 1 🎄 
  23. 2 🌳 
  24. 3 🌲 
  25. 4 🌴 
  26. 5 🎋 
  27. Select the number of the emoji you want: 2 
  28. Copying 🌳 to clipboard 
  29.  
  30. ------------------------------------------------------------------------------------ 
  31. Type one or more emoji related words ... 
  32. End a word with a . if you want to select an emoji if there are multiple 
  33. matches, otherwise the first match will be picked. Type 'q' to exit. 
  34. > q 
  35. Bye 

测试代码时,一种有用的技术是删除所有常见的前导空白。您可以为此使用textwrap.dedent,但是在这里我使用了替代的inspect.cleandoc。

上传到PyPI

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

  1. poetry build 
  2.  
  3. poetry publish 

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

如果您喜欢这个项目,请在Github上给它加星标,很高兴能收到反馈。

https://github.com/PyBites-Open-Source/emojisearcher

 

责任编辑:武晓燕 来源: Python中文社区
相关推荐

2021-09-05 07:55:37

前端Emoji 表情

2017-12-18 11:16:31

iOS苹果Bug

2022-07-27 11:22:44

Emoji图片动图

2015-06-26 11:14:09

Emoji 开发编程语言

2016-06-01 16:03:39

emoji圣经

2022-01-12 10:30:44

Windows 11Windows微软

2021-04-16 11:27:16

Python表情微信

2021-04-06 10:57:15

ChromeEmoji错误

2016-11-01 20:37:31

javascriptnode.jstypescript

2024-03-27 08:41:09

Vue3Web应用emoji表情选择器

2021-07-21 05:23:06

Linkerd Emoji.voto服务网格

2021-07-16 05:31:42

Windows 11操作系统微软

2023-02-17 14:50:40

Windows 11微软开发

2023-02-18 20:00:50

Windows 11Emoji 15

2015-10-09 16:42:16

GDB 排查Python程序故障

2015-06-23 10:07:31

密码表情密码

2011-01-27 17:33:10

DalvikAndroidQNX

2010-02-26 13:17:24

Python开发程序

2010-02-06 14:19:26

ibmdwGoogleMap

2010-11-18 15:52:32

QMLMeeGo
点赞
收藏

51CTO技术栈公众号