Learn Python The Hard Way - Exercise 17: More Files~

Exercise 17: More Filesの続きからというのが正確か。

2. This script is really annoying. There’s no need to ask you before doing the copy, it prints too much out to the screen. Try to make it more friendly to use by removing features.

ここからはじめる。

3. See how short you can make the script. I could make this 1 line long.

ちょっとどうすればいいのかわからなくて、かなり迷ったが。こうやったら出来てしまった。後悔はしていない。ただやっぱりメソッドだとかオブジェクトだとか、ファイルオブジェクトの操作の方法だとかそれの代入だとかの性質や動作や挙動なんかには気を配らないといけないなという感じをすごくもった。

from sys import argv
from os.path import exists

script, from_file, to_file = argv

indata = open(from_file, 'r').read()
output = open(to_file, 'w')
output.write(indata)

4. Notice at the end of the WYSS I used something called cat? It’s an old comman that “con*cat*enates” files
together, but mostly it’s just an easy way to print a file to the screen. Type man cat to read about it.

WYSSって何だよと思ったら段落名What You Should Seeの頭文字かよ。
関係ないけどinfo catってやってnとか押すとcatの次の項目のinfoのページなるのか。infoは勉強が必要だな。なんてったってGNU形式のHelpだし。何度言うか

Exercise 18: Names, Variables, Code, Functions
そろそろ書いておくか。Zed A Shaw先生、面白い、ぶんなげ感あり、実践的、役立つ、例に自分を採用しすぎ。Alan.Gauld先生、理論的、役立つ、本質。
こんな感じ。二つやった方がいい。

function checklist
1. Did you start your function definition with def?
2. Does your function name have only characters and _ (underscore) characters?
3. Did you put an open parenthesis right after the function name?
4. Did you put your arguments after the parenthesis separated by commas?
5. Did you make each argument unique (meaning no duplicated names).
6. Did you indent all lines of code you want in the function 4 spaces? No more, no less.
7. Did you “end” your function by going back to writing with no indent (dedenting we call it)?
And when you run (aka “use”, “call”) a function check these things:
1. Did you call/use/run this function by typing its name?
2. Did you put ( character after the name to run it?
3. Did you put the values you want into the parenthesis separated by commas?
4. Did you end the function call with a ) character.
“To ‘run’, ‘call’, or ‘use’ a function all mean the same thing.”

Exercise 19: Functions And Variables

The variables in your function are not connected to the variables in your script. Here’s an exercise to get you thinking about this:

3. Write at least one more function of your own design, and run it 10 different ways.

def blackbox(blackbox):
    blackbox = blackbox * 3
    print "blackbox: %r" % blackbox

blackbox(3)
blackbox(4)
blackbox(1)
number = 1000
blackbox(number)
number = 1000 + 11
blackbox(number)
blackbox(4 * 20)
blackbox(4 + number + 1)
blackbox('Apple!')
blackbox('Apple!' * 2)
blackbox('Apple' + 'Soda!')

Exercise 20: Functions And Files

2. Research online what the seek function for file does. Try pydoc file and see if you can figure it out from there.

Alan.Gauld先生のところでSeekやら.readlineやらされてたから理解できたんだけど、あそこで.readlineをfor文でまわして何もなくなったらそこでfor文抜け出してファイルの全てを読み出したことになるみたいなことをやってたわけだけど、それが何故実行可能かなのかその時はわからなかった。というか、今までわからなかった。
今、理解したかもしれない。fileオブジェクトにはポインタみたいに今ファイル中のどこを指し示してますよみたいなのがあって、rewindのファンクションでseekを使って0(ファイルの先頭のバイトの数字?)まで戻してるが、それでファイルを読みきった後に先頭までそのポインタを戻すことでまた最初の一行目から読み込んで、表示できている。
一行ずつ読み込んで、順々に表示できているのは一行そこまで表示したらポインタみたいなのもそこまで自動的にずれているから可能になるらしい。Alan.Gauld先生の練習問題をやっていた時に、for文の中でreadline使ってどうやって自動的に次の行表示させてるんだよと思ってたけど、なるほどなぁ。一行読み込めば自動的に次の行にポインタ?が進んでいる仕組みになっていたわけだ。これであの一行ずつ読み込みながらfor文を回しているだけでファイルの全部の内容を書き出すプログラムの意味がわかった。むちゃくちゃ進歩してるなぁ自分。てか、もうほとんどあのあたりの基本的なことが頭に入った。理解できている。
pydoc fileをやってreadlineを読むとより動きがわかる、EOF(End Of File)にempty stringを返すからそれでfor文の条件のところでEmptyつまりFalseになってFor文終わるわけだ。
pydocでfileの項目見てて思ったけど、たぶんこれはfileのモジュールの中にあるtellのファンクションを使えば一行読み込んだ時点でのファイルポジションがどこに移っているかの動作がよくわかるだろう。というか、Alan.Gauld先生のところでもtellは使っていた覚えがあるからこういうのを教えるために使ってくれてたのかなぁ。マジあそこだけは理解できなかったけどファイルオブジェクトとそれ関連がものすごいクリアになりました。ファイルオブジェクトのメソッド周りも理解したしなー。ここまでわかると清清しい。Pythonやれるわーって気分になってきた。

3. Research the shorthand notation += and rewrite the script to use that.

http://www.alan-g.me.uk/tutor/tutdata.htm ※フレーム内ページのフレーム抽出です
ここのShortcut operatorsを再勉強ってほどでもないが再度読んだ。

Exercise 21: Functions Can Return