自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用 Python 開發(fā) Emoji 表情查找程序

開發(fā) 后端
今天分享一個前幾天構(gòu)建的小應(yīng)用程序,用來從命令行搜索emoji表情符號。它可以通過OS命令行來完成,而且不必單擊任何東西即可獲得我的表情符號,更加便捷。

[[398183]]

本文轉(zhuǎn)載自微信公眾號「Python中文社區(qū)」,作者Python中文社區(qū)。轉(zhuǎn)載本文請聯(lián)系Python中文社區(qū)公眾號。

今天分享一個前幾天構(gòu)建的小應(yīng)用程序,用來從命令行搜索emoji表情符號。

它可以通過OS命令行來完成,而且不必單擊任何東西即可獲得我的表情符號,更加便捷。

該工具支持一次將多個匹配的表情符號復(fù)制到剪貼板。

  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即可。

是不是很酷?

安裝并運(yùn)行程序包

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

poetry使依賴項(xiàng)管理變得輕而易舉,最后一個命令(別名)實(shí)際上有效,因?yàn)槲覍⑵浞旁趐yproject.toml文件中:

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

您也可以通過添加以下shell別名來使調(diào)用命令更短(就像我在第一個示例中一樣):

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

(將YOUR_PATH更改為項(xiàng)目的路徑。)

文件夾結(jié)構(gòu)

由于有了poetry new,文件夾結(jié)構(gòu)從一開始就遵循了公認(rèn)的最佳做法。

我喜歡將測試文件放在專用的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復(fù)制到操作系統(tǒng)的剪貼板中:

  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函數(shù)進(jìn)入交互模式。

我想用一種創(chuàng)新的方式來觸發(fā)此交互模式,為此選擇了信號字符(SIGNAL_CHAR):如果用戶的搜索字符串以點(diǎn)(.)結(jié)尾,它將進(jìn)入交互模式。

原因如下:

  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(蛇)”后出現(xiàn)的emoji不會出錯,但是對于“flag(旗幟)”,它默認(rèn)選擇12個匹配項(xiàng)中的第一個(對于“heart(心臟)”,我們會得到130個匹配的表情符號!),這里我想手動選擇一個,因此鍵入點(diǎn)".",以做出進(jìn)一步的選擇。

測試

還有幾件事:

@ pytest.mark.parametrize非常好,可以使您的測試代碼更加簡潔。

將代碼分解為更多的功能使其更可重用且更易于測試。

我測試了使用@patch(“ builtins.input”,side_effect = ['a',10,2,'q']的交互模式模擬input的方法。side_effect中的列表包含將“double” input的參數(shù)。這等效于以下內(nèi)容(在鍵入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 

測試代碼時,一種有用的技術(shù)是刪除所有常見的前導(dǎo)空白。您可以為此使用textwrap.dedent,但是在這里我使用了替代的inspect.cleandoc。

上傳到PyPI

感謝toml文件中[tool.poetry]中的一些基本元數(shù)據(jù),發(fā)布到PyP非常簡單:

  1. poetry build 
  2.  
  3. poetry publish 

(首先使用--repository of publish在測試PyPI上嘗試一下,看是否一切正常。)

如果您喜歡這個項(xiàng)目,請?jiān)贕ithub上給它加星標(biāo),很高興能收到反饋。

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

 

責(zé)任編輯:武曉燕 來源: Python中文社區(qū)
相關(guān)推薦

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 開發(fā)編程語言

2016-06-01 16:03:39

emoji圣經(jīng)

2022-01-12 10:30:44

Windows 11Windows微軟

2024-06-17 10:16:37

MySQLutf8mb4字節(jié)

2021-04-16 11:27:16

Python表情微信

2016-11-01 20:37:31

javascriptnode.jstypescript

2021-04-06 10:57:15

ChromeEmoji錯誤

2025-02-06 10:42:20

2021-07-21 05:23:06

Linkerd Emoji.voto服務(wù)網(wǎng)格

2024-03-27 08:41:09

Vue3Web應(yīng)用emoji表情選擇器

2021-07-16 05:31:42

Windows 11操作系統(tǒng)微軟

2024-04-29 08:22:42

2015-06-23 10:07:31

密碼表情密碼

2023-02-17 14:50:40

Windows 11微軟開發(fā)

2023-02-18 20:00:50

Windows 11Emoji 15

2010-02-06 14:19:26

ibmdwGoogleMap

2011-08-31 15:15:20

PhoneGap
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號