開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
- GIMP (ビットマップ画像編集・加工ソフトウェア、PPM形式(portable pixmap)の画像用)
The Ray Tracer Challenge: A Test-Driven Guide to Your First 3D Renderer (Jamis Buck(著)、Pragmatic Bookshelf)、Chapter 15(Triangles)のWavefront OBJFiles、Test #8: OBJ Parser with Gibberish Inputを取り組んでみる。
コード
obj_file_test.py
#!/usr/bin/env python3 from unittest import TestCase, main from obj_file import Parser class ParserTest(TestCase): def setUp(self): pass def tearDown(self): pass def test_ingoring_unrecognized_lines(self): with open('gibberish.obj') as gibberish: parser = Parser(gibberish) self.assertEqual(parser.ignored_lines, 5) if __name__ == '__main__': main()
obj_file.py
class Parser: def __init__(self, fh): self.ignored_lines = 0 for line in fh: cols = line.split() if len(cols) < 4 or cols[0] not in ['v', 'f']: self.ignored_lines += 1 continue for i, col in enumerate(cols[1:], 1): try: cols[i] = float(col) except ValueError as err: self.ignored_lines += 1 break
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
C:\Users\...>py obj_file_test.py . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK C:\Users\...>
0 コメント:
コメントを投稿