Practical Programming
An Introduction to Computer Science
Using Python 3
(Pragmatic Programmers)
(Pragmatic Bookshelf)
Paul Gries (著) Jennifer Campbell (著)
Jason Montojo (著) Lynn Beighley (編集)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 10(Reading and Writing Files)、10.10(Exercises) 8.を解いてみる。
10.10(Exercises) 8.
コード(BBEdit)
sample8.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- def readMolecule(reader): line = reader.readline() if not line: return None if not line.isspace() and not line.startswith('CMNT'): key, name = line.split() molecule = [name] else: return None serial_number = 1 while True: line = reader.readline() if line.startswith('END'): break if not line.isspace() and not line.startswith('CMNT'): key, num, atom_type, x, y, z = line.split() if serial_number != int(num): print('{0}: シリアル番号が{1}ではない。{2}'.format( num, serial_number, name)) molecule.append([atom_type, x, y, z]) serial_number += 1 return molecule def readAllMolecules(reader): result = [] reading = True while reading: molecule = readMolecule(reader) if molecule: result.append(molecule) else: reading = False return result if __name__ == '__main__': import sys with open(sys.argv[1]) as f: molecules = readAllMolecules(f) for molecule in molecules: print(molecule)
入出力結果(Terminal, IPython)
$ cat multimol.pdb COMPND AMMONIA ATOM 1 N 0.257 -0.363 0.000 ATOM 2 H 0.257 0.727 0.000 ATOM 3 H 0.771 -0.727 0.890 ATOM 4 H 0.771 -0.727 -0.890 END COMPND METHANOL ATOM 1 C -0.748 -0.015 0.024 ATOM 2 O 0.558 0.420 -0.278 ATOM 3 H -1.293 -0.202 -0.901 ATOM 4 H -1.263 0.754 0.600 ATOM 5 H -0.699 -0.934 0.609 ATOM 6 H 0.716 1.404 0.137 END $ cat multimol1.pdb COMPND AMMONIA ATOM 1 N 0.257 -0.363 0.000 ATOM 2 H 0.257 0.727 0.000 ATOM 2 H 0.771 -0.727 0.890 ATOM 4 H 0.771 -0.727 -0.890 END COMPND METHANOL ATOM 1 C -0.748 -0.015 0.024 ATOM 2 O 0.558 0.420 -0.278 ATOM 2 H -1.293 -0.202 -0.901 ATOM 4 H -1.263 0.754 0.600 ATOM 4 H -0.699 -0.934 0.609 ATOM 6 H 0.716 1.404 0.137 END $ ./sample8.py multimol.pdb ['AMMONIA', ['N', '0.257', '-0.363', '0.000'], ['H', '0.257', '0.727', '0.000'], ['H', '0.771', '-0.727', '0.890'], ['H', '0.771', '-0.727', '-0.890']] ['METHANOL', ['C', '-0.748', '-0.015', '0.024'], ['O', '0.558', '0.420', '-0.278'], ['H', '-1.293', '-0.202', '-0.901'], ['H', '-1.263', '0.754', '0.600'], ['H', '-0.699', '-0.934', '0.609'], ['H', '0.716', '1.404', '0.137']] $ ./sample8.py multimol1.pdb 2: シリアル番号が3ではない。AMMONIA 2: シリアル番号が3ではない。METHANOL 4: シリアル番号が5ではない。METHANOL ['AMMONIA', ['N', '0.257', '-0.363', '0.000'], ['H', '0.257', '0.727', '0.000'], ['H', '0.771', '-0.727', '0.890'], ['H', '0.771', '-0.727', '-0.890']] ['METHANOL', ['C', '-0.748', '-0.015', '0.024'], ['O', '0.558', '0.420', '-0.278'], ['H', '-1.293', '-0.202', '-0.901'], ['H', '-1.263', '0.754', '0.600'], ['H', '-0.699', '-0.934', '0.609'], ['H', '0.716', '1.404', '0.137']] $
0 コメント:
コメントを投稿