Tag: python

  • 如何 (濫用) prompt_toolkit 的自動補齊功能做到互動式命令列工具

    Figure 1. 使用 prompt_toolkit multi column autocompletion 功能做出 interactive command line tool. 目標與結論 本文的目標是想要在 command line 做到可以互動式選擇、展開有階層關係的文字列表工具。 使用 prompt_toolkit 的 autocompletion 功能,以及 prompt_toolkit.shortcuts.prompt 的 pre_run hook 達到互動式列表選擇的功能。 動機 寫好 awesome-cli 之後,覺得全部一次顯示的話會花很多時間在滾動捲軸上面 (因為有太多 list item),用 grep 沒有辦法展開一個 section 裡面的東西 (-A -B 的數量很不一致)、然後用 pipe head/less 會出現 BrokenPipelineError。 覺得 tui 太 heavy,因此想要做一個可以簡單選單互動、展開內容的 interactive command line tool. 實做選擇 PyInquirer…

  • IOTA-Python – A Pure-Python implementation of IOTA node

    IOTA-Python – A Pure-Python implementation of IOTA node

    IOTA-Python – A Pure-Python implementation of IOTA node The target of IOTA-Python is to create a pure Python implementation of IOTA node, and to learn about how IRI works. Currently, it can read out the data from IRI rocksdb without IRI RESTful API. Why? It gave you the full access power of the IRI database! GitHub:…

  • random —  你所不知道的 Python 標準函式庫用法 02

    random — 你所不知道的 Python 標準函式庫用法 02

    random – 產生偽隨機亂數 This module implements pseudo-random number generators for various distributions. 官方介紹文件:9.6. random — Generate pseudo-random numbers 如果要產生亂數密碼或是 token,請使用 secrets 模組,絕對不要使用 random 模組來產生密碼。 亂數狀態相關 01. random.seed(a=None, version=2) random.seed() 初始化亂數種子。a 如果是 None 則使用目前的亂數種子。a 可以是 str, bytes, bytearray, 都會被轉型成 int。 02. random.getstate() random.getstate() 取得亂數器內部狀態。 03. random.setstate(state) random.setstate() 設定亂數器內部狀態,state需為 random.getstate() 獲得之 state。 使用範例: >>> import random >>> random.seed(‘foobar’) #…

  • sys — 你所不知道的 Python 標準函式庫用法 01

    sys — 你所不知道的 Python 標準函式庫用法 01

    sys — 系統相關的參數以及函式 This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. 官方介紹文件:29.1 sys – System-specific parameters and functions sys 是一個極其強大的 Python 標準函式庫,大家常常忽略他的存在,但是當要做到一些奇淫技巧的時候,就會需要使用 sys 來達成。 01. sys.argv – dealing with command line arguments sys.argv 用來表示從 command line 傳入的參數,sys.argv[0] 代表著程式的名稱。如果你對 C 熟悉的話,就會想起這個東西 int main(int…

  • Diagnosing and Fixing Reference Leaks in CPython

    Diagnosing and Fixing Reference Leaks in CPython

    CPython’s garbage collection relies on each object’s reference count. Each object has their own reference count, when the object is referenced by others, then we will need to increase object’s reference count by the Py_INCREF macro. In another way, when the referencer don’t need the object anymore, it will need to decrease object’s reference count by the Py_DECREF macro. When object’s…

  • 深入 GIL: 如何寫出快速且 thread-safe 的 Python – Grok the GIL: How to write fast and thread-safe Python

    深入 GIL: 如何寫出快速且 thread-safe 的 Python – Grok the GIL: How to write fast and thread-safe Python

    本文將會探討 Python 內部的 Global Interpreter Lock,以及學習其如何影響 multi-threaded 程式。  原作者:A. Jesse,Twitter: @jessejiryudavis 原文:Grok the GIL: How to write fast and thread-safe Python Louie Lu 經作者同意[1][2]翻譯為正體中文。 當我6歲時,我有一個音樂盒。我將他上緊發條,在上面的芭蕾舞者開始繞圈,而盒子內的機關開始敲打,發出「一閃一閃亮晶晶」的聲音。雖然這東西肯定很廉價,不過我喜歡這個音樂盒,而我想要知道他是怎麼運作的。總之,我打開了這個音樂盒,看到了裏面的裝置 ── 一個我拇指大小的金屬圓筒,安裝得當的讓他可以旋轉,透過凸起的牙齒與鋼梳撞擊後發出音符。 在程式設計師的特點中,關於事情如何運作的好奇心是必不可缺的。當我打開我的音樂盒觀看內部時,我展現我可能是一個 ── 如果不是一個頂尖程式設計師 ── 起碼也會是好奇的一個。 這很奇怪,我在對 Global Interpreter Lock (GIL) 誤解的情況下寫了 Python 程式這麼久,因為我還沒有足夠的好奇心來了解他是如何運作的。我遇到很多人跟我有著同樣的猶豫,以及無知。 該是時候把這個黑盒子翹開了。讓我們閱讀 CPython 原始碼來了解什麼是 GIL,為什麼 Python 會有,以及他是如何影響我們撰寫 multi-threaded 程式。我會給出一些範例來讓你了解 GIL。你會學到如何快速寫出 thread-safe 的 Python,以及如何在 threads 與 processes 之間選擇。…

  • Python C Extension package version 設定

    不論是 setuptools 或是 distutils 都無法直接設定 C Extension package 的 version,必須採用紆迴的方式進行。 總而言之,Python 3.6 的年代,我們直接使用 setuptools。 0. 預期結果 >>> import jchash >>> jchash.__version__ ‘1.0’ 1.原始狀況 原始 Init function: PyMODINIT_FUNC PyInit_jchash(void) { return PyModule_Create(&module); } 原始 setup.py PACKAGE_VERSION = ‘1.0’ jchash = Extension(PACKAGE_NAME, define_macros=[ (‘PACKAGE_VERSION’, PACKAGE_VERSION)], sources=[‘src/jchash.c’]) 只有這樣是不夠的,Extension 的 define_macros 是在編譯時會加上 -DPACKAGE_VERSION=1.0 這樣的 flag,可是不會幫你轉換成為 package 的 version。 2.改用 PyModule_AddStringConstant 參考資料:PyModule_AddStringConstant…

  • 使用 C 來編寫 Python 模組 – 以 jump consistent hash 為例

    使用 C 來編寫 Python 模組 – 以 jump consistent hash 為例

    Extending Python with C – Jump Consistent Hash 本文的目標是以 C 完成 Jump Consistent Hash 的主體,接著透過 Python-C-API 轉為 Python 可以使用的套件。也就是說,模組的本體將會以 C 語言寫成,接著透過 setuptools 編譯為 so 檔,如此編成的模組將可以讓 Python 無縫接軌使用。 參考文章與程式原始碼: A Fast, Minimal Memory, Consistent Hash Algorithm: https://arxiv.org/pdf/1406.2294v1.pdf Python c-api: https://docs.python.org/3/c-api/intro.html Extending Python with C or C++: https://docs.python.org/3/extending/extending.html jchash – Jump Consistent Hash for Python: https://github.com/grapherd/jchash   1.我們的目標 閱讀完 <A Fast, Minimal Memory, Consistent…