開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
入門 Python 3 (Bill Lubanovic (著)、 斎藤 康毅(監修)、 長尾 高弘 (翻訳)、オライリージャパン)の10章(システム)、10-5(復習問題)を取り組んでみる。
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
print('10-1')
import datetime
today = datetime.date.today()
filename = 'today.txt'
with open(filename, 'w') as f:
print(today.isoformat(), file=f, end='')
print('10-2')
with open(filename) as f:
today_string = f.read()
print(today_string)
print('10-3')
fmt = '%Y-%m-%d'
date = datetime.datetime.strptime(today_string, fmt)
print(date)
print('10-4')
import os
files = os.listdir(os.path.curdir)
print(files)
print('10-5')
files = os.listdir(os.path.pardir)
print(files)
print('10-6')
import multiprocessing
import time
import random
def done(n, t):
time.sleep(t)
print('done {0} ({1}秒)'.format(n, t))
for n in range(3):
p = multiprocessing.Process(target=done,
args=(n, random.randrange(1, 6), ))
p.start()
print('10-7')
birth_date = datetime.date(2000, 1, 2)
print(birth_date)
print('10-8')
print(birth_date.weekday())
print(birth_date.isoweekday())
print(datetime.datetime.strftime(birth_date, '%A %a'))
ten_thousand_days = datetime.timedelta(days=10000)
print((birth_date + ten_thousand_days).isoformat())
入出力結果(Terminal, IPython)
$ ./sample.py 10-1 10-2 2016-08-25 10-3 2016-08-25 00:00:00 10-4 ['draft.html', 'draft.html~', 'sample.py', 'sample.py~', 'today.txt'] 10-5 ['ch10', 'ch4', 'ch5', 'ch6', 'ch7', 'ch8', 'ch9'] 10-6 10-7 2000-01-02 10-8 6 7 Sunday Sun 2027-05-20 done 1 (2秒) done 0 (4秒) done 2 (5秒) $
0 コメント:
コメントを投稿