PythonでHello Worldの先へ進む
Google App EngineのHello Worldを試した人はたくさんいると思いますが、その先に進めていない方のためにチュートリアルを試したログを晒すと見せかけてただのアプリログだったりする。
環境:GAE+NetBeans6.9.1
準備:NetBeans6.9.1でプラグイン「python」の追加
エラーが起きたよ
File "/home/ubuntu-user/NetBeansProjects/HockeyRoster/src/HockeyRoster.py", line 4
SyntaxError: Non-ASCII character '\xe3' in file /home/ubuntu-user/NetBeansProjects/HockeyRoster/src/HockeyRoster.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
文字コードのエラーのようだ。
Python でUTF-8, shift_jis, euc_jpなど日本語を使う方法
ここにあるようにヘッダに下記2行を追加したらOKでした。
#!/usr/bin/env python # -*- coding: utf-8 -*-
無事作成成功
Hockey Roster Application
Please choose an option
Press 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit:
1がデータ追加、2がデータ閲覧、3がデータ検索、4が終了の機能ですね。
Press 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit: 1
Add a player to the roster by providing the following informationFirst Name: 麻生
Last Name: 太郎
Position: 元総理大臣
Player successfully added to the team rosterAdd another? (Y or N)n
閲覧してみると?
Please choose an option
Press 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit: 2
====================Complete Team Roster
======================
麻生 太郎 - 元総理大臣
=== End of Roster ===
検索してみよう
Please choose an option
Press 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit: 3
Enter a player name below to search the teamFirst Name: 麻生
Last Name: 太郎
麻生 太郎 is in the roster as 元総理大臣
1件じゃものたりないので、
Please choose an option
Press 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit: 1
Add a player to the roster by providing the following informationFirst Name: 菅
Last Name: 直人
Position: 総理大臣
Player successfully added to the team rosterAdd another? (Y or N)n
2件目入れて閲覧してみよう
Please choose an option
Press 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit: 2
====================Complete Team Roster
======================
麻生 太郎 - 元総理大臣
菅 直人 - 総理大臣
=== End of Roster ===
見れるね。次は検索。
Please choose an option
Press 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit: 3
Enter a player name below to search the teamFirst Name: 菅
Last Name: 直人
菅 直人 is in the roster as 総理大臣
Please choose an optionPress 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit:
OK!チュートリアル完了!
作成したソースは2つだけ
# HockeyRoster.py # # HockeyRoster アプリケーションの実装ロジック # Player モジュールから Player クラスをインポート from Player import Player # 各 Player オブジェクトを入れるリストを定義 playerList = [] # addPlayer() # # キーボード入力を受け取り、選手オブジェクトを名簿に追加。この関数は、 # 呼び出されるたびに新しい選手オブジェクトを作成し、リストに付加。 def addPlayer(): addNew = 'Y' print "Add a player to the roster by providing the following information\n" while addNew.upper() == 'Y': first = raw_input("First Name: ") last = raw_input("Last Name: ") position = raw_input("Position: ") id = len(playerList) player = Player() player.create(id, first, last, position) playerList.append(player) print "Player successfully added to the team roster\n" addNew = raw_input("Add another? (Y or N)") makeSelection() # makeSelection() # # アプリケーションのセレクタを作成。関数は、出力をコマンド行に # 表示します。次にコマンド行でパラメータをキーボード入力として取得して、 # アプリケーションのオプションを選択。 def makeSelection(): validOptions = ['1', '2', '3', '4'] print "Please choose an option\n" selection = raw_input("Press 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit: ") if selection not in validOptions: print "Not a valid option, please try again\n" makeSelection() else: if selection == '1': addPlayer() elif selection == '2': printRoster() elif selection == '3': searchRoster() else: print "Thanks for using the HockeyRoster application." # printRoster() # # リストの内容をレポートとしてコマンド行に出力 def printRoster(): print "====================\n" print "Complete Team Roster\n" print "======================\n\n" for player in playerList: print "%s %s - %s" % (player.first, player.last, player.position) print "\n" print "=== End of Roster ===\n" makeSelection() # searchRoster() # # コマンド行から選手名の入力を受け取り、 # 名簿内を検索。選手が名簿内で見つかった場合は、肯定のメッセージを # 出力。見つからなかった場合は、否定のメッセージを出力。 def searchRoster(): index = 0 found = False print "Enter a player name below to search the team\n" first = raw_input("First Name: ") last = raw_input("Last Name: ") position = None while index < len(playerList): player = playerList[index] if player.first.upper() == first.upper() or player.last.upper() == last.upper(): found = True position = player.position index = index + 1 if found: print '%s %s is in the roster as %s' % (first, last, position) else: print '%s %s is not in the roster.' % (first, last) makeSelection() # main # # アプリケーションのエントリポイント。アプリケーションのタイトルをコマンド行に # 出力してから、makeSelection() 関数を呼び出す。 if __name__ == "__main__": print "Hockey Roster Application\n\n" makeSelection()
# Player.py # # 選手オブジェクトを入れるコンテナ class Player: # 選手の属性 id = 0 first = None last = None position = None # 選手オブジェクトを作成する関数 def create(self, id, first, last, position): self.id = id self.first = first self.last = last self.position = position
注意!上記ソースはわたしの環境ではエラーとなりました。