開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の8章(スタティックライブラリとダイナミックライブラリ: ホットスワップ可能なコード)、makeマグネット(p.361)をpythonで考えてみる。
makeマグネット(p.361)
コード(BBEdit, Emacs)
libs/hfcal.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def display_calories(weight, distance, coeff):
print('体重:{0:3.2f}ポンド'.format(weight))
print('距離:{0:3.2f}マイル'.format(distance))
print('消費カロリー:{0:4.2f}カロリー'.format(coeff))
hfsecurity.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import libs.hfcal
libs.hfcal.display_calories(115.2, 11.3, 0.79)
print()
try:
hfcal.display_calories(115.2, 11.3, 0.79)
except Exception as err:
print(type(err), err, err.args)
print()
# libsにパスを通す
import sys
sys.path.append('libs')
import hfcal
hfcal.display_calories(115.2, 11.3, 0.79)
bank_vault.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import hfsecurity
import sys
for path in sys.path:
print(path)
s = 'Speak friend and enter'
b = bytearray(s.encode('utf-8'))
hfsecurity.encrypt(b)
s = bytes(b).decode('utf-8')
print('{0}に暗号化しました。'.format(s))
print('チェックサムは{0}です。'.format(hfsecurity.checksum(s)))
b = bytearray(s.encode('utf-8'))
hfsecurity.encrypt(b)
s = bytes(b).decode('utf-8')
print('{0}に復号化しました。'.format(s))
print('チェックサムは{0}です。'.format(hfsecurity.checksum(s)))
入出力結果(Terminal, IPython)
$ ls -R
./ ../ elliptical.py* libs/
./libs:
./ ../ hfcal.py
$ ./elliptical.py
体重:115.20ポンド
距離:11.30マイル
消費カロリー:0.79カロリー
<class 'NameError'> name 'hfcal' is not defined ("name 'hfcal' is not defined",)
体重:115.20ポンド
距離:11.30マイル
消費カロリー:0.79カロリー
$
0 コメント:
コメントを投稿