今さらPython3 (79) - 第12章一気読み その1

前回までで、この本の手を動かす部分(第11章)までは終了。とはいえ、第12章も気になるところがかなりなあるので、拾い読みしていくつもり。

入門 Python 3

入門 Python 3

pipのバージョン指定

$ pip install flask==0.9.0

とか

$ pip install 'flask>=0.9.0'

みたいなバージョン指定ができる。

ソースからのインストール

コードをダウンロードしたら、解凍してsetup.pyがあるディレクトリを探して、これでいける。

$ python install setup.py

ちなみに、.whl形式のファイルなら、pipも使える。

$ pip install xxxxxx.whl

IDE環境

この本を読み始める前から、PyCharm使ってます。
www.jetbrains.com

IPythonはあらためて別の機会に。

プログラムのお作法

定数は大文字とアンダースコアを使って表現する、あと下のブロックがテストになっているんだね。

pylint

さっき作ったftoc.pyでやってみた。

$ pylint ftoc.py
No config file found, using default configuration
************* Module ftoc
C:  1, 0: Missing module docstring (missing-docstring)
W: 11, 4: Redefining name 'c_temp' from outer scope (line 16) (redefined-outer-name)
W:  9, 9: Redefining name 'f_temp' from outer scope (line 15) (redefined-outer-name)


Report
======
14 statements analysed.

Statistics by type
------------------

+---------+-------+-----------+-----------+------------+---------+
|type     |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module   |1      |NC         |NC         |0.00        |0.00     |
+---------+-------+-----------+-----------+------------+---------+
|class    |0      |NC         |NC         |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|method   |0      |NC         |NC         |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|function |1      |NC         |NC         |100.00      |0.00     |
+---------+-------+-----------+-----------+------------+---------+



Raw metrics
-----------

+----------+-------+------+---------+-----------+
|type      |number |%     |previous |difference |
+==========+=======+======+=========+===========+
|code      |16     |84.21 |NC       |NC         |
+----------+-------+------+---------+-----------+
|docstring |1      |5.26  |NC       |NC         |
+----------+-------+------+---------+-----------+
|comment   |0      |0.00  |NC       |NC         |
+----------+-------+------+---------+-----------+
|empty     |2      |10.53 |NC       |NC         |
+----------+-------+------+---------+-----------+



Duplication
-----------

+-------------------------+------+---------+-----------+
|                         |now   |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines      |0     |NC       |NC         |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |NC       |NC         |
+-------------------------+------+---------+-----------+



Messages by category
--------------------

+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |1      |NC       |NC         |
+-----------+-------+---------+-----------+
|refactor   |0      |NC       |NC         |
+-----------+-------+---------+-----------+
|warning    |2      |NC       |NC         |
+-----------+-------+---------+-----------+
|error      |0      |NC       |NC         |
+-----------+-------+---------+-----------+



Messages
--------

+---------------------+------------+
|message id           |occurrences |
+=====================+============+
|redefined-outer-name |2           |
+---------------------+------------+
|missing-docstring    |1           |
+---------------------+------------+



Global evaluation
-----------------
Your code has been rated at 7.86/10

docstringがないと出るのは納得いかないな。。。

unittest


$ python3 test_cap.py
F.
======================================================================
FAIL: test_multiple_words (__main__.TestCap)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_cap.py", line 22, in test_multiple_words
    self.assertEqual(result, 'A Veritable Flock Of ducks')
AssertionError: 'A veritable flock of ducks' != 'A Veritable Flock Of ducks'
- A veritable flock of ducks
?   ^         ^     ^
+ A Veritable Flock Of ducks
?   ^         ^     ^


----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)


$ python3 test_cap2.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

実際には、本に書いてあるように不完全だけど、こうやってテストを繰り返すという仕組みを理解することが大事だと思うので、このぐらいにしておく。

その他のテストツール

26.3. doctest — 対話的な実行例をテストする — Python 3.5.1 ドキュメント
Note to Users — nose 1.3.7 documentation

一気読みのつもりが、まだ終わらなかったので、もう少し粘ってみる。

(つづく)