開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Introducing Python: Modern Computing in Simple Packages (Bill Lubanovic (著)、 O'Reilly Media)のChapter 10(Systems)、Things to Do 10.1-9.を取り組んでみる。
Things to Do 10.1-9.
コード(Emacs)
#!/usr/bin/env python3
# -*-c doing: utf-8 -*_
import datetime
print('10.1')
filename = 'today.txt'
with open(filename, 'w') as f:
print(datetime.date.today().isoformat(), file=f, end='')
print('10.2')
with open(filename) as f:
today_string = f.read()
print(today_string)
print('10.3')
today = datetime.datetime.strptime(today_string, '%Y-%m-%d')
print(today)
print('10.4')
import os
print(os.listdir('.'))
print('10.5')
print(os.listdir('..'))
print('10.6')
import multiprocessing
import random
import time
def target(seconds):
time.sleep(seconds)
print('Process {0}: {1}'.format(os.getpid(), time.ctime()))
print('start: Process {0}: {1}'.format(os.getpid(), time.ctime()))
for _ in range(10):
seconds = random.randrange(1, 5)
p = multiprocessing.Process(target=target, args=(seconds,))
p.start()
f.close()
print('10.7')
date = datetime.date(2000, 1, 2)
print('10.8')
print(date.isoweekday())
print('10.9')
old = date + datetime.timedelta(days=10000)
print(old.isoformat())
入出力結果(Terminal, IPython)
$ ./sample1.py 10.1 10.2 2016-12-12 10.3 2016-12-12 00:00:00 10.4 ['draft.html', 'draft.html~', 'draft1.html', 'draft1.html~', 'draft2.html', 'draft3.html', 'draft4.html', 'draft5.html', 'draft6.html', 'draft7.html', 'draft8.html', 'draft9.html', 'multiprocessing.txt', 'output.txt', 'output.txt~', 'sample1.py', 'sample1.py~', 'sample2.py', 'sample2.py~', 'sample3.py', 'sample3.py~', 'sample4.py', 'sample4.py~', 'sample5.py', 'sample5.py~', 'sample6.py', 'sample6.py~', 'sample7.py', 'sample7.py~', 'sample8.py', 'sample8.py~', 'sample9.py', 'sample9.py~', 'today.txt'] 10.5 ['.DS_Store', 'ch1', 'ch10', 'ch11', 'ch12', 'ch2', 'ch3', 'ch4', 'ch5', 'ch6', 'ch7', 'ch8', 'ch9', 'introducing-python'] 10.6 start: Process 6171: Mon Dec 12 14:23:32 2016 10.7 10.8 7 10.9 2027-05-20 Process 6172: Mon Dec 12 14:23:33 2016 Process 6173: Mon Dec 12 14:23:33 2016 Process 6176: Mon Dec 12 14:23:33 2016 Process 6178: Mon Dec 12 14:23:33 2016 Process 6180: Mon Dec 12 14:23:33 2016 Process 6181: Mon Dec 12 14:23:34 2016 Process 6174: Mon Dec 12 14:23:35 2016 Process 6175: Mon Dec 12 14:23:35 2016 Process 6177: Mon Dec 12 14:23:35 2016 Process 6179: Mon Dec 12 14:23:36 2016 $
0 コメント:
コメントを投稿