<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 1.3 配置 Eclipse 和 PyDev 為了快速的的開發調試 Python 程序,就必須要使用一個穩定的 IDE 平臺。這里作者推 薦的時候 Eclipse(跨平臺的 IDE)和 PyDev。Eclipse 以其強大的可定制性而出名。下面讓我們 看看和安裝和配置它們: 1. 從 [http://www.eclipse.org/downloads/](http://www.eclipse.org/downloads/)下載壓縮包 2. 解壓到 C:\Eclipse 3. 運行 C:\Eclipse\eclipse.exe 4. 第一次運行,會詢問在哪里設置工作區的主目錄;使用默認的就行,將 Use this as default and do not ask again 勾上,點擊 OK。 5. Eclipse 安裝好以后,選擇 Help Software Updates Find and Install 6. 選擇 Search for new features to install 然后點擊 Next。 7. 點擊 New Remote Site。 8. 在 Name 后面填上 PyDev Update,在 URl 后面填上 [http://pydev.sourceforge.net/updates/](http://pydev.sourceforge.net/updates/), 點擊 OK 確認,接著點擊 Finish,Eclipse 會自動升級 PyDev。 9. 過一會兒,更新窗口就會出現,找到頂端的 PyDev Update,選上 PyDev,單擊 Next 繼 續下一步。 1. 閱讀 PyDev 協議,如果同意,在 I accept the terms in the licens agreement 選上。 2. 單擊 Next,和 Finish。Eclipse 開始安裝 PyDe 擴展,全部完成后,單擊 Install All。 12 最后一步,在 PyDev 安裝好之后,單擊 Yes,Eclipse 會重新啟動并加載 PyDev。 使用如下步驟配置 Eclipse,以確保 PyDev 能正確的調用 Python 解釋器執行腳本。 1. Eclipese 驅動后,選擇 Window Preferences 2. 擴展 PyDev,選擇 Interpreter – Python。 3. 在對話框頂端的 Python Interpreters 中點擊 New。 4. 瀏覽到 C:\Python25\python.exe,然后點擊 Open。 5. 下一個對話框將會列出 Python 中已經安裝了的庫。 6. 再次點擊 OK 完成安裝。 在開始編碼前,需要創建一個 PyDev 工程。本書的所有代碼都可以在這個工程中打開。 1. 依次選擇 File--&gt;New--&gt;Project。 2. 展開 PyDev 選擇 PyDev Project,點擊 Next 繼續。 3. 將工程命名為 Gray Hat Python. 點擊 Finish。 Eclipse 窗口自動更新之后,會看到 Gray Hat Python 工程出現在屏幕左上角。現在右擊 sec 文件夾,選擇 New--&gt;PyDev Module。在 Name 字段輸入 chapter1-test,點擊 Finish。就會 看到,工程面板被更新了,chapter1-test.py 被加到列表中。 在 Eclipse 中運行 Python 腳本,重要單擊工具欄上的 Run As(由綠圈包圍的白色箭頭)按 鈕就行了。要運行以前的腳本,可以使用快捷鍵 CTRL-F11。腳本的輸出會顯示在 Eclipse 底端的 Console 面板。現在萬事俱備只欠代碼。 ### 1.3.1 hacker 們的朋友:ctypes ctypes 是強大的,強大到本書以后介紹的幾乎所有庫都要基于此。使用它我們就能夠調 用動態鏈接庫中函數,同時創建各種復雜的 C 數據類型和底層操作函數。毫無疑問,ctypes 就是本書的基礎。 ### 1.3.2 使用動態鏈接庫 使用 ctypes 的第一步就是明白如何解析和訪問動態鏈接庫中的函數。一個 dynamically linked library(被動態連接的庫)其實就是一個二進制文件,不過一般自己不運行,而是由別 的程序調用執行。在 Windows 上叫做 dynamic link libraries (DLL)動態鏈接庫,在 Linux 上叫 做 shared objects (SO)共享庫。無論什么平臺,這些庫中的函數都必須通過導出的名字調用, 之后再在內存中找出真正的地址。所以正常情況下,要調用函數,都必須先解析出函數地址, 不過 ctypes 替我們完成了這一步。 ctypes 提供了三種方法調用動態鏈接庫:cdll(), windll(), 和 oledll()。它們的不同之處就在 于,函數的調用方法和返回值。cdll() 加載的庫,其導出的函數必須使用標準的 cdecl 調用約定。windll()方法加載的庫,其導出的函數必須使用 stdcall 調用約定(Win32 API 的原生約 定)。oledll()方法和 windll()類似,不過如果函數返回一個 HRESULT 錯誤代碼,可以使用 COM 函數得到具體的錯誤信息。 調用約定 調用約定專指函數的調用方法。其中包括,函數參數的傳遞方法,順序(壓入棧或 者傳給寄存器),以及函數返回時,棧的平衡處理。下面這兩種約定是我們最常用到的: cdecl and stdcall。cdecl 調用約定,函數的參數從右往左依次壓入棧內,函數的調用者, 在函數執行完成后,負責函數的平衡。這種約定常用于 x86 架構的 C 語言里。 In C ``` int python_rocks(reason_one, reason_two, reason_three); ``` In x86 Assembly ``` push reason_three push reason_two push reason_one call python_rocks add esp, 12 ``` 從上面的匯編代碼中,可以清晰的看出參數的傳遞順序,最后一行,棧指針增加了 12 個字節(三個參數傳遞個函數,每個被壓入棧的指針都占 4 個字節,共 12 個), 使得 函數調用之后的棧指針恢復到調用前的位置。 下面是個 stdcall 調用約定的了例子,用于 Win32 API。 In C ``` int my_socks(color_one color_two, color_three); ``` In x86 Assembly ``` push color_three push color_two push color_one call my_socks ``` 這個例子里,參數傳遞的順序也是從右到左,不過棧的平衡處理由函數 my_socks 自己完成,而不是調用者。 最后一點,這兩種調用方式的返回值都存儲在 EAX 中。 下面做一個簡單的試驗,直接從 C 庫中調用 printf()函數打印一條消息,Windows 中的 C 庫 位于 C:\WINDOWS\system32\msvcrt.dll,Linux 中的 C 庫位于/lib/libc.so.6。 chapter1-printf.py Code on Windows ``` from ctypes import * msvcrt = cdll.msvcrt message_string = "Hello world!\n" msvcrt.printf("Testing: %s", message_string) ``` 輸出結果見如下: ``` C:\Python25> python chapter1-printf.py Testing: Hello world! C:\Python25> ``` Linux 下會有略微不同: chapter1-printf.py Code on Linux ``` from ctypes import * libc = CDLL("libc.so.6") message_string = "Hello world!\n" libc.printf("Testing: %s", message_string) ``` 輸出結果如下: ``` # python /root/chapter1-printf.py Testing: Hello world! # ``` 可以看到 ctypes 調用動態鏈接庫中的函數有多簡單。 ### 1.3.3 構造 C 數據類型 使用 Python 創建一個 C 數據類型很簡單,你可以很容易的使用由 C 或者 C++些的組件。 Listing 1-1 顯示三者之間的對于關系。 | C Type | Python Type | ctypes Type | | --- | --- | --- | | char | 1-character string | c_char | | wchar_t | 1-character Unicode string | c_wchar | | char | int/long | c_byte | | char | int/long | c_ubyte | | short | int/long | c_short | | unsigned short | int/long | c_ushort | | int | int/long | C_int | | unsigned int | int/long | c_uint | | long | int/long | c_long | | unsigned long | int/long | c_ulong | | long long | int/long | c_longlong | | unsigned long long | int/long | c_ulonglong | | float | float | c_float | | double | float | c_double | | char * (NULL terminated) | string or none | c_char_p | | wchar_t * (NULL terminated) | unicode or none | c_wchar_p | | void * | int/long or none | c_void_p | Listing 1-1:Python 與 C 數據類型映射 請把這章表放到隨時很拿到的地方。ctypes 類型初始化的值,大小和類型必須符合定義 的要求。看下面的例子。 ``` C:\Python25> python.exe Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from ctypes import * >>> c_int() c_long(0) >>> c_char_p("Hello world!") c_char_p('Hello world!') >>> c_ushort(-5) c_ushort(65531) >>> c_short(-5) c_short(-5) >>> seitz = c_char_p("loves the python") >>> print seitz c_char_p('loves the python') >>> print seitz.value loves the python >>> exit() ``` 最 后 一 個 例 子 將 包 含 了 "loves the python" 的 字 符 串 指 針 賦 值 給 變 量 seitz , 并 通 過 seitz.value 方法間接引用了指針的內容, ### 1.3.5 定義結構和聯合 結構和聯合是非常重要的數據類型,被大量的適用于 WIN32 的 API 和 Linux 的 libc 中。 一個結構變量就是一組簡單變量的集合 (所有變量都占用空間)些結構內的變量在類型上沒 有限制,可以通過點加變量名來訪問。比如 beer_recipe.amt_barley,就是訪問 beer_recipe 結 構中的 amt_barley 變量。 In C ``` struct beer_recipe { int amt_barley; int amt_water; }; ``` In Python ``` class beer_recipe(Structure): _fields_ = [ ("amt_barley", c_int), ("amt_water", c_int), ] ``` 如你所見,ctypes 很簡單的就創建了一個 C 兼容的結構。 聯合和結構很像。但是聯合中所有變量同處一個內存地址,只占用一個變量的內存空間,這個空間的大小就是最大的那個變量的大小。這樣就能夠將聯合作為不同類型的變量操作訪 問了。 In C ``` union { long barley_long; int barley_int; char barley_char[8]; }barley_amount; ``` In Python ``` class barley_amount(Union): _fields_ = [ ("barley_long", c_long), ("barley_int", c_int), ("barley_char", c_char * 8), ] ``` 如果我們將一個整數賦值給聯合中的 barley_int,接著我們就能夠調用 barley_char,用 字符的形式顯示剛才輸入的 66。 chapter1-unions.py ``` from ctypes import * class barley_amount(Union): _fields_ = [ ("barley_long", c_long), ("barley_int", c_int), ("barley_char", c_char * 8), ] value = raw_input("Enter the amount of barley to put into the beer vat: my_barley = barley_amount(int(value)) print "Barley amount as a long: %ld" % my_barley.barley_long print "Barley amount as an int: %d" % my_barley.barley_long print "Barley amount as a char: %s" % my_barley.barley_char ``` 輸出如下: ``` C:\Python25> python chapter1-unions.py Enter the amount of barley to put into the beer vat: 66 Barley amount as a long: 66 Barley amount as an int: 66 Barley amount as a char: B C:\Python25> ``` 給聯合賦一個值就能得到三種不同的表現方式。最后一個 barley_char 輸出的結果是 B, 因為 66 剛好是 B 的 ASCII 碼。 barley_char 成員同時也是個數組,一個八個字符大小的數組。在 ctypes 中申請一個數組, 只要簡單的將變量類型乘以想要申請的數量就可以了。 一切就緒,開始我們的旅程吧!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看