Learning to program - The Basics - Coding Style

今回は英文読む以外は労力をあまり使わなそうなところ。

これはいいこと学んじゃったね。

Documentation strings

All languages allow you to create comments to document what a function or module does, but a few, such as Python and Smalltalk, go one stage further and allow you to document the function in a way that the language/environment can use to provide interactive help while programming. In Python this is done using the """documentation""" string style:

class Spam:
"""A meat for combining with other foods

It can be used with other foods to make interesting meals.
It comes with lots of nutrients and can be cooked using many
different techniques"""

def __init__(self):
pass # ie. it does nothing!

print Spam.__doc__

Note: We can access the documentation string by printing the special __doc__ variable. Modules, Functions and classes/methods can all have documentation strings. For example try:

import sys
print sys.__doc__

Since Python version 2.2 there is also a help() function within Python that will search for and print out any helpful documentation on a Python symbol. For example to see the help on sys.exit we can do this at the Python prompt:

>>> import sys
>>> help (sys.exit)
Help on built-in function exit:

exit(...)
exit([status])

Exit the interpreter by raising SystemExit(status).
If the status is omitted or None, it defaults to zero (i.e., success).
If the status is numeric, it will be used as the system exit status.
If it is another kind of object, it will be printed and the system
exit status will be one (i.e., failure).
(END)

To get out of help mode hit the letter 'q'(for quit) when you see the (END) marker. If more than one page of help is present you can hit the space bar to page through it. <<<

まぁ、ぶっちゃけAlan Gauld先生のテキスト読んでてつまづいたわけだがw

Note for Unix users

The first line of a Python script file should contain the sequence #! followed by the full path of python on your system. You can find that by typing, at your shell prompt:

$ which python

On my system the line looks like:

#! /usr/local/bin/python

This will allow you to run the file without calling Python at the same time (after you set it to be executable via chmod - but you knew that already I'm sure!):

$ spam.py

You can use an even more convenient trick on most modern Unix systems (including all Linux distros) which replaces the path information with /usr/bin/env/python, like this:

#! /usr/bin/env/python

That will find where Python is in your path automatically. The only snag is where you may have two or more different versions of Python installed and the script will only work with one of them (maybe it uses a brand new language feature, say), in that case you will be better with the full path technique.

とあるが、○○.pyで実行できない。何故だと思っていたところ、
ぐぐってみて質問

unix上でpythonを実行させるときは、普段
python スクリプト名.py でRETURNを押せばできるがこれをpythonを省略して、スクリプト名.pyだけで実行できるようにするにはどうせればいいか教えてください。

こんなのを発見。

その「csv.py」というファイルですが、カレントディレクトリ上ということで直に「csv.py」と入力していませんか?
Windowsの場合は実行ファイルをカレント→パスの順で探してくれますがLinux系で探してくれるのはパス指定のもののみなので、カレントでどうにかしたいなら「./csv.py」とパス指定してやる必要があります。

まさにこれだった。

パーミッションの実行権限をchmodだかで変更するのはまだ学んでいないwのでエクスプローラライクなソフトで変更した。

Windows(DOS)環境の場合は、カレントディレクトリ→pathの順に目的のファイルを探しますが、Unix系だとpathのみが検索対象となってしまいます。
よって、UNIX上では自前のコマンド(プログラム)を実行したいなら、パスをコマンドの前に付けるか、環境変数pathにそのファイルのある場所を明示的に登録しなければいけません。

普通は、自分の作ったコマンドは、自分のホームディレクトリの下に bin という名前のディレクトリを作って、そこに格納します。
おそらく、$PATH に $HOME/bin が含まれていると思いますので、

へー
ここに通したらもう./すら不必要になったわ。これがコマンドとして扱われるということか。TAB押すと補完されるし。

ちなみに#! /usr/bin/env/python ならどのシステムでも通るよってAlan Gauldさんも質問に答えてる人も言ってるけど、通らん。Pythonにパスが通してあればってpythonって打ってインタプレタ?が反応すりゃおkなんでしょ?駄目みたいだ。
あ、パスの確認の仕方のサイト見たら通ってないかもw

パス(PATH)の確認と設定方法は? - Pocketstudio.jp Linux Wikiを参考にして、通してみたけど駄目だったぜ。/usr/bin/env/pythonに通してみても駄目だったし。とりあえず一通りは試してみたけど。まぁ、諦めマフ。

PATHに関してはここも参考になった
ITmedia エンタープライズ : Linux Tips「パス設定を確認したい」

PATHって.bash_profileを書き換えないと次回から自動でパス指定されてないのかぁ。書いてあってへーと思って、今実際に試してみて勉強になった。

chmodについては未だに勉強していないw

chmodについても勉強した。といってもとりあえず実行できればいいから、適当なんだけど。
http://www.k-tanaka.net/unix/chmod.html
http://itpro.nikkeibp.co.jp/article/COLUMN/20060227/230728/
http://www.linux.or.jp/JM/html/gnumaniak/man1/chmod.1.html

chmod a+rwx 対象ファイルで全てのユーザに全ての権限与えるみたいな感じになる。んでとりあえずそうしている。
後、次の章に入ってhomeディレクトリ直下の/binに.pyファイルを作ってファイル名だけでそのまま実行できるかと無茶していたけど、それは出来なかった。構文エラーとか出てきて戸惑いまくった。

python ファイル名にするか、
ファイル名のみにしたい場合はホームディレクトリ直下の/bin(初期設定でパスが通っているところ)に入れる場合でも#! /usr/bin/pythonとかwhich pythonでわかるPythonのある環境に合わせて設定してやらねば、ファイル名のみでは実行不可だった。