Physical computing ︰ python 《補充》︰ IDE 除錯

根據 WiKi 百科︰

在計算機科學裡,『內省』 Type introspection 是指程式在執行中 Run time 檢查『物件』 Object 類型的一種能力。相對於『內省』,『反射』 Reflection 則更進一步,它在程式在執行中,還可以檢視和修改本身的『狀態』或『行為』。

在《CPU 機器語言的『解譯器』》一文裡,我們提到︰

事實上 Von Neumann 的計算機架構,對於電腦程式語言的發展,有著極為深遠的影響,產生了現在叫做 Von Neumann 程式語言,與Von Neumann 的計算機架構,同形 isomorphism 同構

program variables ↔ computer storage cells
程式變數 對映 計算機的儲存單元

control statements ↔ computer test-and-jump instructions
控制陳述 對映 計算機的『測試.跳至』指令

assignment statements ↔ fetching, storing instructions
賦值陳述 對映 計算機的取得、儲存指令

expressions ↔ memory reference and arithmetic instructions.
表達式 對映 記憶體參照和算術指令

由於機器語言內,並不區分『指令』與『資料』,而且一般多以『二進制』的數值表達,因此都是『反射』的。之後的一些高階語言,比方說 C ,區分了『指令』和『資料』,這個『反射』的能力就逐漸消失了。一九八二年 Brian Cantwell Smith ── 現是加拿大‧多倫多大學教授 ── 在博士論文中介紹了『程式語言』的計算『反射』的這種概念,此後常用在『元編程』 metaprogramming 、『自循環直譯器』 Meta-circular evaluator 以及『軟體測試』 software testing 中。

舉例來說『 PyPy 』是 rPython 寫的 Python 解譯器,可以用來好好的 Hack Python !一年前,由於『樹莓派基金會』的贊助,因而也有了 ARM 的版本。

事實上, Python 語言高度支持『內省』與『反射』,在此僅列出幾個鏈結給有興趣的讀者︰

inspect — Inspect live objects

Dive Into Python

Python Programming/Reflection

雖然目前『 Ninja-IDE 』上沒見到『內省』或『反射』功能,據聞『 NINJA-IDE 3.0 』將包含一些新的特色,就讓我們拭目以待的吧!

也許『 Ninja-IDE 』的想法是︰打造自己的 IDE ,所以詳細說明了如何寫作插件《 Plugin Tutorial 》,這也是了解忍者 IDE 運作方式的文件。此處我們將介紹一個『 ninja-debugger-plugin 』,安裝方法十分簡單,在主選單裡選擇【 Addins 】‧【 Manage Plugins 】,然後點選『 ninja-debugger-plugin 』插件,按 Install 鍵即可。如何操作『除錯器』?就請讀者自行閱讀《 Starting to debug 》一文的了。

Plugins Manager_050

NINJA-IDE - -home-casey-temp-mp3-28-fractal.py_076

 

 

─── 自得者,家有敝帚,享之千金。 ───

 

Physical computing ︰ python 《補充》︰ IDE 用法……

220px-PythonProgLogo 1990s-2005

Python 語言的創造者吉多‧范羅蘇姆 Guido van Rossum 先生非常注重『程式風格』!主導著整體 Python 語言之內蘊。一九九四年元月,那時網際網路還盛行用『地鼠』 Gopher 挖掘資訊的時代, Python 1.0 發布。同年七月 Michael McLay (mclay@eeel.nist.gov)
Wed, 29 Jun 94 10:07:42 EDT 寫了一篇

If Guido was hit by a bus? 》 ── 萬一吉多被巴士撞了? ──

的文章,或許促進了 Python 語言的開始標準化。之後 Python 社群開始稱呼 Guido 『生殺決策之慈悲獨裁者BDFL Benevolent dictator for life︰

BDFL is a title given to a small number of open-source software development leaders, typically project founders who retain the final say in disputes or arguments within the community.

無疑的吉多是第一位擁有這個殊榮稱號的第一人。二零零一年三月六日『 Python軟體基金會』 PSF Python Software Foundation 成立,一個月後 Python 2.1 發行,所有的發行版開始使用 PSF 授權。事實上吉多的熱情與睿智,更多的展現在『 Python 改進提案PEPs Python Enhancement Proposals 的發文裡。有關『可讀性』之『程式風格』的論述主要集中在︰

PEP 7 – Style Guide for C Code

PEP 8 – Style Guide for Python Code

也許吉多的理念是︰

程式雖是一時一人之寫作,確有多時多人的閱讀,因此『可讀性』是十分重要的事情。

這個『 Ninja-IDE 』有一個特色,就是能找出 Python 程式不符合『 PEP8 』建議的『原始碼』,彰顯了『 Pythonic Way 』的重要組成部分。在此讓我們舉個出自美國 Simpson College 的 Paul Vincent Craven 所著的線上書《 Program Arcade Games With Python And Pygame 》第二十章中的『 fractal.py 』為例,比較符不符合『 PEP8 』的異同︰

feature-errors
Highlight Static and PEP8 errors in the document, you can also see that the files containing PEP8 errors are shown with an icon in the tab where the file is opened, and files containing code static errors are shown with a bug icon in that tab.

PEP8 的版本

"""
Sample fractal using recursion.
Sample Python/Pygame Programs
Simpson College Computer Science
http://programarcadegames.com/
http://simpson.edu/computer-science/
"""
import pygame

# Define some colors
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)


def recursive_draw(x, y, width, height, count):
    # Draw the rectangle
    #pygame.draw.rect(screen,black,[x,y,width,height],1)
    pygame.draw.line(screen,
        black,
        [x + width * .25, height // 2 + y],
        [x + width * .75, height // 2 + y],
        3)
    pygame.draw.line(screen,
        black,
        [x + width * .25, (height * .5) // 2 + y],
        [x + width * .25, (height * 1.5) // 2 + y],
        3)
    pygame.draw.line(screen,
        black,
        [x + width * .75, (height * .5) // 2 + y],
        [x + width * .75, (height * 1.5) // 2 + y],
        3)
    if count > 0:
        count -= 1
        # Top left
        recursive_draw(x, y, width // 2, height // 2, count)
        # Top right
        recursive_draw(x + width // 2, y, width // 2, height // 2, count)
        # Bottom left
        recursive_draw(x, y + width // 2, width // 2, height // 2, count)
        # Bottom right
        recursive_draw(x + width // 2, y + width // 2, width // 2,
             height // 2, count)
pygame.init()
# Set the height and width of the screen
size = [700, 700]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop
    # Set the screen background
    screen.fill(white)
    # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
    fractal_level = 3
    recursive_draw(0, 0, 700, 700, fractal_level)
    # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    # Limit to 20 frames per second
    clock.tick(20)
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()

Ninja-IDE顯示之原始版本

NINJA-IDE - -home-casey-temp-mp3-28-fractal.py_072

原始程式

"""
 Sample fractal using recursion.

 Sample Python/Pygame Programs
 Simpson College Computer Science
 http://programarcadegames.com/
 http://simpson.edu/computer-science/
"""

import pygame

# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
green    = (   0, 255,   0)
red      = ( 255,   0,   0)

def recursive_draw(x, y, width, height, count):
    # Draw the rectangle
    #pygame.draw.rect(screen,black,[x,y,width,height],1)
    pygame.draw.line(screen,
                     black,
                     [x + width*.25,height//2+y],
                     [x + width*.75,height//2+y],
                     3)
    pygame.draw.line(screen,
                     black,
                     [x+width*.25,(height*.5)//2+y],
                     [x+width*.25,(height*1.5)//2+y],
                     3)
    pygame.draw.line(screen,
                     black,
                     [x + width*.75,(height*.5)//2+y],
                     [x + width*.75,(height*1.5)//2+y],
                     3)

    if count > 0:
        count -= 1
        # Top left
        recursive_draw(x, y, width // 2, height // 2, count)
        # Top right
        recursive_draw(x + width // 2, y, width // 2, height // 2, count)
        # Bottom left
        recursive_draw(x, y + width // 2, width // 2, height // 2, count)
        # Bottom right
        recursive_draw(x + width // 2, y + width // 2, width // 2, height // 2, count)


pygame.init()

# Set the height and width of the screen
size = [700, 700]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

#Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop

    # Set the screen background
    screen.fill(white)

    # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
    fractal_level = 3
    recursive_draw(0, 0, 700, 700, fractal_level)
    # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT闡釋

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # Limit to 20 frames per second
    clock.tick(20)

# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()

 

 

 

 

Physical computing ︰ python 《補充》︰ IDE 用法…

蕭規曹隨典故引文︰

蕭何與曹參兩人自少年微賤時便已是好友,後來西漢建國,蕭何身為宰相,曹參身為大將,地位不凡的兩人卻反而有了嫌隙。蕭何擔任相國時,參考前朝文獻制訂典章及制度。蕭死前,推薦曹參繼任。曹參上任後,認為蕭何訂下的法令已很完備,所以繼續沿用而不作改動。

曹參就任漢相國期間,整日飲酒食肉,政治上清靜無為,繼續執行蕭何留下的政策,不予變動。漢惠帝認為自己被曹參輕視,於是命其子御史大夫曹窋勸諫,曹參把曹窋鞭笞了兩百下並趕出門外,漢惠帝於是親自責問曹參。

曹參摘帽,向皇帝俯首謝罪:「陛下您認為,您與先帝相比,誰較為英明神武?」皇帝回道:「我怎敢與先帝比?」曹參又問說:「我跟蕭何比,誰較賢能?」皇帝說道:「你好像不太比得上他。」曹參接著說:「陛下說得對,且高祖跟蕭何平定了天下,法令都健全具備。陛下只要垂拱而治,我們這些官吏堅守崗位,遵守他們的法令而不犯過失,不是很適合嗎?」

時人歌頌:「蕭何制定法律,調和整齊如一;曹參繼任相國,遵法而不犯過失。施載清淨無為的政策,人民因而安寧統一。」史稱「蕭規曹隨」。

在《『騛罿』── 非同的禪!!》一文裡,我們談過︰

吉多的 Python 『非同』於其他程式語言,居然把『空白』符號寫進了它的『文法』裡,竟然用『對齊的空白』表示程式區塊。如是種種『見地』,匯聚成一條稱作『非同的』pythonic Way 大道。吉多他的『中心思想』,集中的表現在由 Tim Peters 先生所寫的『 Python 的禪』,收錄在『 this 』模組 module 裡︰

Readability counts.
可讀性能加分

或許因為太多人問︰是否有一天,將有一版 Python 可以使用『“{ ” 與 “} ”』來表達『程式區塊』??以至於有了這個特別『復活蛋』之幽默回答!!

pi@raspberrypi ~ $ python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> from __future__ import braces
File “<stdin>”, line 1
SyntaxError: not a chance
>>>

看來這是『沒機會not a chance 的了…,甚至有…

>>> import __hello__
Hello world…
>>>

其實這個『空白』就是源自《始中終!!》所說的『ABC』語言,在今天都還有個名稱叫做『越位規則』,正宛如不同的程式語言有不同的『聖歌mantra 一樣, Python 的禪是︰

There should be one– and preferably only one –obvious way to do it.
總有一個,最好是唯一的一個,明白的作法

。而 Perl 與 Ruby 的咒語說︰

There’s more than one way to do it.
凡事都有多種作法

事實上也沒有太多好爭論的。

2015-02-05-152803_700x500_scrot

也就像『 ninja-ide 』的『 Welcome to NINJA-IDE Documentation ! 』是個沒有使用說明的手冊!或許說作者們認為它太直覺,很容易,所以不需要講解?也許講希望使用者從『』與『』中學習??

 

 

 

 

Physical computing ︰ python 《補充》︰ IDE ︰安裝

言 

兼

《說文解字》︰,并也。从又持秝。兼持二禾,持一禾。

欠

易經》第十五卦‧地山謙

謙:亨,君子有終。

彖曰謙,亨,天道下濟而光明,地道卑而上行。天道虧盈而益謙,地道變盈而流謙,鬼神害盈而福謙,人道惡盈而好謙。謙尊而光,卑而不可踰,君子之終也。

象曰地中有山,謙﹔君子以裒多益寡,稱物平施。

初六:謙謙君子,用涉大川,吉。
象曰:謙謙君子,卑以自牧也。

六二:鳴謙,貞吉。
象曰:鳴謙貞吉,中心得也。

九三:勞謙君子,有終吉。
象曰:勞謙君子,萬民服也。

六四:無不利,撝謙。
象曰:無不利,撝謙﹔不違則也。

六五不富,以其鄰,利用侵伐,無不利。
象曰利用侵伐,征不服也。

上六:鳴謙,利用行師,征邑國。
象曰:鳴謙,志未得也。 可用行師,征邑國也。

』卦,是易經裡唯一六爻皆『』的卦,既將迎接『羊頭』,告別『馬尾』,祈願立春後諸事『吉祥』,風調雨順

近來有人談起如何學習 python 語言的事,雖想在『部落格』中不便大談特談,之前既然說了《 Thue 》,《 λ 運算》與《圖靈機》,也許說幾件一般 python 書籍中比較少講的□○,或許是寥勝於無的吧!!

對於初學 python 語言的人,通常有一個『整合發展環境』 IDE integrated development environment 是很有幫助的。由於 python 內建的『 IDLE 』太過陽春,所以常被『閒置』!那麼在『樹莓派』上可有合適的 IDE 呢?於此我們將介紹一個也是用『 python 』寫的『別樣』 IDE ── 忍者 ninja ── ,WiKi 上說,它的命名來自︰
the recursive acronym: “Ninja-IDE Is Not Just Another IDE”

雖然目前 raspbian 中已有『 ninja-ide v2.0 』,簡單使用

sudo apt-get install ninja-ide

就能安裝。由於各種可應用『插件』 plugins 上的考慮,此處我們將說明如何安裝 v2.3 的最新發行版︰

 

# 檢查目前 raspbian ninja-ide 的版本
apt-cache show ninja-ide
Package: ninja-ide
Version: 2.0~b-2
Installed-Size: 1069
Maintainer: David Paleino <dapal@debian.org>
Architecture: all
Depends: python (>= 2.6.6-7~), python-qt4, libjs-jquery, pyflakes
Size: 456358
SHA256: 1833c6eb4a70d1e8ec1812b55a66650478f49855e48a7e6a64d3678869c85243
SHA1: b57d0a13f64da2b53236a5e7bf5e5669394a348e
MD5sum: 285af7d1de191d35d137760cb839bca3
Description: integrated development environment (IDE) for Python
Homepage: http://www.ninja-ide.org/
Description-md5: 3ebb9c5854de561b006e05916e99fead
Tag: devel::ide, devel::lang:python, role::program
Section: devel
Priority: extra
Filename: pool/main/n/ninja-ide/ninja-ide_2.0~b-2_all.deb

# 安裝 ninja-ide 相依的套件
sudo apt-get install python-qt4 libjs-jquery pyflakes python-qscintilla2

# 取得與執行
wget https://github.com/ninja-ide/ninja-ide/archive/v2.3.zip
unzip v2.3.zip
cd ninja-ide-2.3/
python ninja-ide.py

# 建立桌面快取圖案
cd ~/Desktop/
nano ninja.desktop

[Desktop Entry]
Name=Ninja IDE
Comment=Integrated DeveLopment Environment for Python
Exec=/home/pi/ninja-ide-2.3/ninja-ide.py
Icon=/home/pi/ninja-ide-2.3/icon.png
Terminal=false
Type=Application
Categories=Application;Development;
StartupNotify=true

2015-02-04-151601_700x500_scrot

 

 

 

立春!!

OLYMPUS DIGITAL CAMERA

春笋

杜甫‧立春

春日春盤細生菜,忽憶兩京梅發時。
盤出高門行白玉,菜傳纖手送青絲。
巫峽寒江那對眼,杜陵遠客不勝悲。
此身未知歸定處,呼兒覓紙一題詩。

 

南朝梁‧昭明太子‧蕭統纂要》:

一年之計在於春,一日之計在於晨。

 

古訓《增廣賢文
昔時賢文,誨汝諄諄,集韻增文,多見多聞。
觀今宜鑒古,無古不成今。

一年之計在於春,

一日之計在於寅,

一家之計在於和,

一生之計在於勤。