1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

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

        共 4698字,需瀏覽 10分鐘

         ·

        2021-05-11 21:45

        今天分享一個(gè)前幾天構(gòu)建的小應(yīng)用程序,用來從命令行搜索emoji表情符號。
        它可以通過OS命令行來完成,而且不必單擊任何東西即可獲得我的表情符號,更加便捷。
        該工具支持一次將多個(gè)匹配的表情符號復(fù)制到剪貼板。
        $ 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個(gè)表情符號都寫好了,在鍵盤輸入Cmd + V即可。
        是不是很酷?
        安裝并運(yùn)行程序包
        git clone [email protected]:PyBites-Open-Source/emojisearcher.git
        cd emojisearcher
        poetry install
        poetry run emo

        poetry使依賴項(xiàng)管理變得輕而易舉,最后一個(gè)命令(別名)實(shí)際上有效,因?yàn)槲覍⑵浞旁?/span>pyproject.toml文件中:
        [tool.poetry.scripts]
        emo = "emojisearcher.script:main"

        您也可以通過添加以下shell別名來使調(diào)用命令更短(就像我在第一個(gè)示例中一樣)
        $ alias emo
        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表情:
        ...
        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復(fù)制到操作系統(tǒng)的剪貼板中:
        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)

        感謝這個(gè)庫的作者AlSweigart,這是一個(gè)很酷的程序包。
        如何查找多個(gè)表情符號?
        在這種情況下,我通過user_select_emoji函數(shù)進(jìn)入交互模式。
        我想用一種創(chuàng)新的方式來觸發(fā)此交互模式,為此選擇了信號字符(SIGNAL_CHAR):如果用戶的搜索字符串以點(diǎn)(.)結(jié)尾,它將進(jìn)入交互模式。
        原因如下:
        $ 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(蛇)”后出現(xiàn)的emoji不會(huì)出錯(cuò),但是對于“flag(旗幟)”,它默認(rèn)選擇12個(gè)匹配項(xiàng)中的第一個(gè)(對于“heart(心臟)”,我們會(huì)得到130個(gè)匹配的表情符號?。?,這里我想手動(dòng)選擇一個(gè),因此鍵入點(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之后。):
        $ 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

        • 測試代碼時(shí),一種有用的技術(shù)是刪除所有常見的前導(dǎo)空白。您可以為此使用textwrap.dedent,但是在這里我使用了替代的inspect.cleandoc
        上傳到PyPI
        感謝toml文件中[tool.poetry]中的一些基本元數(shù)據(jù),發(fā)布到PyP非常簡單:
        poetry build
        poetry publish

        (首先使用--repository of publish在測試PyPI上嘗試一下,看是否一切正常。)
        如果您喜歡這個(gè)項(xiàng)目,請?jiān)贕ithub上給它加星標(biāo),很高興能收到反饋。
        https://github.com/PyBites-Open-Source/emojisearcher


        更多閱讀



        用 Python 進(jìn)行 OCR 圖像識別


        用 XGBoost 進(jìn)行時(shí)間序列預(yù)測


        5 分鐘快速上手 pytest 測試框架

        特別推薦




        點(diǎn)擊下方閱讀原文加入社區(qū)會(huì)員

        瀏覽 49
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            特级黄色片 | 粉嫩被粗大进进出出视频 | free性中国hd露脸 | 6—12呦国产精品 | 日日擼夜夜擼狠狠擼88 | 精品露脸国偷精品产拍 | 懂色AV一区二区三区免费看 | 欧美aaaaaaaa | 波多野结衣无码电影 | 国产美女精品精品喷水免费观看 |