💡
この記事は Middleman 時代に書いた古いものです。記録のため、astro-notion-blog に移行していますが、あまり参考にしないでください。
正解リスト作成ヘルパーメソッドの作成
-
テストの正解リストを簡単に作成するために,create_correct_answers というヘルパメソッドを追加する.すでに作成した generate_with_array を使うことで簡単に作成できる.
with such.A('MonoImage class') as it: (中略) def create_correct_answers(it, array_tupple): return [mi.generate_with_array(a) for (mi, a) in zip(it.marray, array_tupple)]
full と fill メソッドのテスト
-
NumPy には full, full_like という同じ値で埋めた新規オブジェクト作成メソッドと,fill という既存のオブジェクトを全て同じ値で埋めるインスタンスメソッドがある. 単語は異なるが内容的にはほぼ同じメソッドである.比較する正解リストは同じなので,同じメソッド内でテストしておく. 正解リストは先ほどの create_correct_answers で作成した. テストしたいメソッドは以下の三つである.fill_self だけが破壊的メソッドである.Ruby であれば ! を付けるのだが,Python では記号が使えないので,「_self」を付けることにした.
- MonoImage.create_with_full(shape, v, dtype, bits, signed) → share, dtype, bits, signed の v で埋めた MonoImage を作成する.bits と signed は省略可とする.
- obj.generate_with_full(v) → obj と同じ bits, dtype, signed の v で埋めた MonoImage を作成する
- obj.fill_self(v) → obj の値を全て v で埋める
with such.A('MonoImage class') as it: @it.should('have some full(fill) methods.') def test(): correct = create_correct_answers(it, ([[2, 2, 2], [2, 2, 2]], [[2, 2], [2, 2], [2, 2]], [2, 2, 2, 2, 2, 2])) compare_two_mono_image_list([MonoImage.create_with_full(*x) for x in (((2, 3), 2, np.uint8), ((3, 2), 2, np.int8, 16, True), (6, 2.0, np.double, 0))], correct) compare_two_mono_image_list([x.generate_with_full(2) for x in it.marray], correct) [x.fill_self(2) for x in it.marray] compare_two_mono_image_list(it.marray, correct)
full と fill メソッドの実装
-
create_with_full は Numpy.full で作成したオブジェクトで MonoImage を作るだけである.
@classmethod def create_with_full(self, shape, v, dtype, bits=8, signed=False): return MonoImage(np.full(shape, v, dtype), bits, signed)
-
generate_with_full は今作成した create_with_full をプロパティを使って呼び出せばよい.
def generate_with_full(self, v): return MonoImage.create_with_full(self.shape, v, self.dtype, self.bits, self.signed)
-
fill_self は NumPy.ndarray.fill を呼び出す.また,メソッドチェーンすることも考え,self を return しておく.
def fill_self(self, v): self._array.fill(v) return self
関連メソッドのテスト
full や fill の特殊版である zeros, ones に関するメソッドも追加しておく.
-
zeros に関連するテストは以下のようになる(ほぼ full, fill と同じである).
@it.should('have some zeros methods.') def test(): correct = [x.generate_with_full(0) for x in it.marray] compare_two_mono_image_list([MonoImage.create_with_zeros(*x) for x in (((2, 3), np.uint8), ((3, 2), np.int8, 16, True), (6, np.double, 0))], correct) compare_two_mono_image_list([x.generate_with_zeros() for x in it.marray], correct) [x.zeros_self() for x in it.marray] compare_two_mono_image_list(it.marray, correct)
-
同様に ones に関連するテストは以下のようになる.
@it.should('have some ones methods.') def test(): correct = [x.generate_with_full(1) for x in it.marray] compare_two_mono_image_list([MonoImage.create_with_ones(*x) for x in (((2, 3), np.uint8), ((3, 2), np.int8, 16, True), (6, np.double, 0))], correct) compare_two_mono_image_list([x.generate_with_ones() for x in it.marray], correct) [x.ones_self() for x in it.marray] compare_two_mono_image_list(it.marray, correct)
- 実装は簡単なので省略する
長くなったので今日はここまで.ただし,似たようなメソッドがあったらテストを追加する.