2019年4月24日水曜日

開発環境

Programming Bitcoin: Learn How to Program Bitcoin from Scratch (Jimmy Song(著)、O'Reilly Media)のChapter 5(Transactions)、Version、Exercises 1(90)の解答を求めてみる。

コード

Python 3

#!/usr/bin/env python3
from unittest import TestCase, main


class TransactionTest(TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_parse(self):
        import io
        b = io.BytesIO(b'\x01\x00\x00\x00\x01\x81')
        self.assertEqual(1, Transaction.parse(b))


def little_endian_to_int(b: bytes) -> int:
    return int.from_bytes(b, 'little')


class Transaction:
    def __init__(self, version, tx_ins, tx_outs, locktime, testnet=False):
        pass

    @staticmethod
    def parse(stream):
        serialized_version = stream.read(4)
        return little_endian_to_int(serialized_version)


if __name__ == '__main__':
    main()

入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))

C:\Users\...py sample1.py
E
======================================================================
ERROR: test_parse (__main__.TransactionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "sample1.py", line 15, in test_parse
    self.assertEqual(1, Transaction.parse(b))
AttributeError: type object 'Transaction' has no attribute 'parse'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

C:\Users\...py sample1.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

C:\Users\...py sample1.py -v
test_parse (__main__.TransactionTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

C:\Users\...>

0 コメント:

コメントを投稿