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()