今さらPython3 (71) - 第10章復習課題
第10章の復習課題。
- 作者: Bill Lubanovic,斎藤康毅,長尾高弘
- 出版社/メーカー: オライリージャパン
- 発売日: 2015/12/01
- メディア: 単行本(ソフトカバー)
- この商品を含むブログを見る
10.1
>>> from datetime import date >>> fout = open('today.txt', 'wt') >>> print(date.today(), file=fout) >>> fout.close()
これでファイルには、2015-12-27となっていたので正解だと思うけど、本に書いてあるみたいにisoフォーマットにした方がいいのかも。
>>> from datetime import date >>> with open("today.txt", "wt") as fout: ... print(date.today().isoformat(), file=fout) ...
10.2
>>> with open('today.txt', 'rt') as fin: ... today_string = fin.read() ... >>> today_string '2015-12-27\n'
*** 10.3
>>> import time >>> fmt = '%Y-%m-%d' >>> time.strptime(today_string, fmt) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_strptime.py", line 494, in _strptime_time tt = _strptime(data_string, format)[0] File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_strptime.py", line 340, in _strptime data_string[found.end():]) ValueError: unconverted data remains: >>> fmt = '%Y-%m-%d\n' >>> time.strptime(today_string, fmt) time.struct_time(tm_year=2015, tm_mon=12, tm_mday=27, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=361, tm_isdst=-1)
間違えた部分も載せておく。最後に改行が入っているので、fmtの中には\nまで含める必要がある。
10.4
>>> import os >>> os.getcwd() '/Users/ken/PycharmProjects/IntroducingPython3'
まずカレントディレクトリを取得する方法を思い出す。
>>> os.listdir(os.getcwd()) ['.idea', '__pycache__', 'bfile', 'books.csv', 'books.db', 'books2.csv', 'books3.csv', 'bottle1.py', 'bottle2.py', 'bottle3.py', 'bottle_test.py', 'country_data.xml', 'enterprise.db', 'flask1.py', 'flask2.py', 'flask3.py', 'flask3a.py', 'flask3b.py', 'flask3c.py', 'flask9-2.py', 'flask9-3.py', 'flask9-5.py', 'home.html', 'index.html', 'jeepers.txt', 'links.py', 'links2.py', 'mcintyre.yaml', 'menu.xml', 'mp.py', 'ohmygod2.txt', 'ohwell.txt', 'output.xml', 'poems', 'relativity', 'settings.cfg', 'templates', 'terminate.py', 'test1.py', 'test2.py', 'today.txt', 'villains', 'yikes.txt', 'zoo.py']
一気に出したけど、変数に入れてからos.listdir()でも良いはず。
<10.5終わってからやり直し>
もっと簡単な方法があった。
>>> os.listdir('.') ['.idea', '__pycache__', 'bfile', 'books.csv', 'books.db', 'books2.csv', 'books3.csv', 'bottle1.py', 'bottle2.py', 'bottle3.py', 'bottle_test.py', 'country_data.xml', 'enterprise.db', 'flask1.py', 'flask2.py', 'flask3.py', 'flask3a.py', 'flask3b.py', 'flask3c.py', 'flask9-2.py', 'flask9-3.py', 'flask9-5.py', 'home.html', 'index.html', 'jeepers.txt', 'links.py', 'links2.py', 'mcintyre.yaml', 'menu.xml', 'mp.py', 'ohmygod2.txt', 'ohwell.txt', 'output.xml', 'poems', 'relativity', 'settings.cfg', 'templates', 'terminate.py', 'test1.py', 'test2.py', 'today.txt', 'villains', 'yikes.txt', 'zoo.py'] >>>
10.5
親ディレクトリは..で表現するからこういう感じだね。
>>> os.listdir('..') ['.DS_Store', 'IntroducingPython3']
ということは、10.4の答えももっとシンプルになるはず。ということで10.4に追記した。
10.6
ちとプログラム書こうか。
これをmp10-6.pyという名前で保存して実行。
$ python3 mp10-6.py Waited 1 secs. It is 2015-12-27 12:42:14.857463 now. Waited 1 secs. It is 2015-12-27 12:42:14.857478 now. Waited 4 secs. It is 2015-12-27 12:42:17.858021 now. $ python3 mp10-6.py Waited 1 secs. It is 2015-12-27 12:45:04.728357 now. Waited 4 secs. It is 2015-12-27 12:45:07.730087 now. Waited 5 secs. It is 2015-12-27 12:45:08.728227 now.
意味もなく2回実行してみた。
問題文には、1秒から5秒までのランダムな秒数だけ眠らせようという割には、サンプルソースコードは、random.random()で拾っているから、これじゃ0〜1秒の時間しか拾わないよね。5倍かけ算して丸めるとか、random.choice()にするべきだと思う。
10.7
作者みたいに誕生日偽装しようかな。
>>> from datetime import date >>> mybd = date(1980, 1, 30) >>> mybd datetime.date(1980, 1, 30)
10.8
>>> mybd.weekday() 2 >>> mybd.isoweekday() 3
こういう時、自分の誕生日を使わないと結果が正しいかどうかわからないね。weekdayは月曜日が0、isoweekdayは月曜日が1という数え方らしいので、正解は水曜日ですね。
10.9
>>> from datetime import timedelta >>> htdays = timedelta(days=10000) >>> mybd + htdays datetime.date(2007, 6, 17) >>>
どうりでオッサンになる訳だw。
(つづく)