使用Jupyter Notebook 学习 Python-Python教程

资源魔 59 0

有了 Jupyter、PyHamcrest,用一点测试的代码把它们连正在一同,你就能够教任何实用于单位测试的 Python 内容。

Python视频教程栏目为各人具体引见~

对于 Ruby 社区的一些事件不断让我印象粗浅,此中两个例子是对测试的承诺以及对易于上手的强调。这两方面最佳的例子是 Ruby Koans,正在这里你能够经过修复测试来学习 Ruby。

要是咱们能把这些神秘的对象也用于 Python,咱们应该能够做患上更好。是的,应用 Jupyter Notebook、PyHamcrest,再加之一点相似于胶带的粘合代码,咱们能够做出一个包罗教授教养、可工作的代码以及需求修复的代码的教程。

起首,需求一些“胶布”。通常,你会应用一些美丽的饬令行测试器来做测试,比方 pytest 或 virtue。通常,你乃至没有会间接运转它。你应用像 tox 或 nox 这样的对象来运转它。但是,关于 Jupyter 来讲,你需求写一小段粘合代码,能够间接正在此中运转测试。

侥幸的是,这个代码又短又简略:

import unittest

def run_test(klass):
    suite = unittest.TestLoader().loadTestsFromTestCase(klass)
    unittest.TextTestRunner(verbosity=2).run(suite)
    return klass复制代码

如今,配备曾经就绪,能够进行第一次操练了。

正在教授教养中,从一个简略的操练开端,建设信念老是一个好主见。

那末,让咱们来修复一个十分简略的测试:

@run_test
class TestNumbers(unittest.TestCase):
   
    def test_equality(self):
        expected_value = 3 # 只改这一行
        self.assertEqual(1+1, expected_value)复制代码
    test_equality (__main__.TestNumbers) ... FAIL
   
    ======================================================================
    FAIL: test_equality (__main__.TestNumbers)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "<ipython-input-7-5ebe25bc00f3>", line 6, in test_equality
        self.assertEqual(1+1, expected_value)
    AssertionError: 2 != 3
   
    ----------------------------------------------------------------------
    Ran 1 test in 0.002s
   
    FAILED (failures=1)复制代码

“只改这一行” 对先生来讲是一个有用的标志。它精确地标明了需求修正的内容。不然,先生能够经过将第一行改成 return 来修复测试。

正在这类状况下,修复很容易:

@run_test
class TestNumbers(unittest.TestCase):
   
    def test_equality(self):
        expected_value = 2 # 修复后的代码行
        self.assertEqual(1+1, expected_value)复制代码
    test_equality (__main__.TestNumbers) ... ok
   
    ----------------------------------------------------------------------
    Ran 1 test in 0.002s
   
    OK复制代码

但是,很快,unittest 库的原生断言将被证实是不敷的。正在 pytest 中,经过重写 assert 中的字节码来处理这个成绩,使其具备神秘的属性以及各类启示式办法。但这正在 Jupyter notebook 中就不易完成了。是时分挖出一个好的断言库了:PyHamcrest。

from hamcrest import *
@run_test
class TestList(unittest.TestCase):
   
    def test_equality(self):
        things = [1,
                  5, # 只改这一行
                  3]
        assert_that(things, has_items(1, 2, 3))复制代码
    test_equality (__main__.TestList) ... FAIL
   
    ======================================================================
    FAIL: test_equality (__main__.TestList)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "<ipython-input-11-96c91225ee7d>", line 8, in test_equality
        assert_that(things, has_items(1, 2, 3))
    AssertionError:
    Expected: (a sequence containing <1> and a sequence containing <2> and a sequence containing <3>)
         but: a sequence containing <2> was <[1, 5, 3]>
   
   
    ----------------------------------------------------------------------
    Ran 1 test in 0.004s
   
    FAILED (failures=1)复制代码

PyHamcrest 不只善于灵敏的断言,它还善于明晰的谬误信息。正由于如斯,成绩就不言而喻了。[1, 5, 3] 没有蕴含 2,并且看起来很丑:

@run_test
class TestList(unittest.TestCase):
   
    def test_equality(self):
        things = [1,
                  2, # 改完的行
                  3]
        assert_that(things, has_items(1, 2, 3))复制代码
    test_equality (__main__.TestList) ... ok
   
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
   
    OK复制代码

应用 Jupyter、PyHamcrest 以及一点测试的粘合代码,你能够传授任何实用于单位测试的 Python 主题。

例如,上面能够协助展现 Python 从字符串中去掉空缺的没有同办法之间的差别。

source_string = "  hello world  "

@run_test
class TestList(unittest.TestCase):
   
    # 这是个赠品:它能够工作!
    def test_complete_strip(self):
        result = source_string.strip()
        assert_that(result,
                   all_of(starts_with("hello"), ends_with("world")))

    def test_start_strip(self):
        result = source_string # 只改这一行
        assert_that(result,
                   all_of(starts_with("hello"), ends_with("world  ")))

    def test_end_strip(self):
        result = source_string # 只改这一行
        assert_that(result,
                   all_of(starts_with("  hello"), ends_with("world")))复制代码
    test_complete_strip (__main__.TestList) ... ok
    test_end_strip (__main__.TestList) ... FAIL
    test_start_strip (__main__.TestList) ... FAIL
   
    ======================================================================
    FAIL: test_end_strip (__main__.TestList)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "<ipython-input-16-3db7465bd5bf>", line 19, in test_end_strip
        assert_that(result,
    AssertionError:
    Expected: (a string starting with '  hello' and a string ending with 'world')
         but: a string ending with 'world' was '  hello world  '
   
   
    ======================================================================
    FAIL: test_start_strip (__main__.TestList)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "<ipython-input-16-3db7465bd5bf>", line 14, in test_start_strip
        assert_that(result,
    AssertionError:
    Expected: (a string starting with 'hello' and a string ending with 'world  ')
         but: a string starting with 'hello' was '  hello world  '
   
   
    ----------------------------------------------------------------------
    Ran 3 tests in 0.006s
   
    FAILED (failures=2)复制代码

理想状况下,先生们会心识到 .lstrip() 以及 .rstrip() 这两个办法能够餍足他们的需求。但若他们没有这样做,而是试图四处应用 .strip() 的话:

source_string = "  hello world  "

@run_test
class TestList(unittest.TestCase):
   
    # 这是个赠品:它能够工作!
    def test_complete_strip(self):
        result = source_string.strip()
        assert_that(result,
                   all_of(starts_with("hello"), ends_with("world")))

    def test_start_strip(self):
        result = source_string.strip() # 改完的行
        assert_that(result,
                   all_of(starts_with("hello"), ends_with("world  ")))

    def test_end_strip(self):
        result = source_string.strip() # 改完的行
        assert_that(result,
                   all_of(starts_with("  hello"), ends_with("world")))复制代码
    test_complete_strip (__main__.TestList) ... ok
    test_end_strip (__main__.TestList) ... FAIL
    test_start_strip (__main__.TestList) ... FAIL
   
    ======================================================================
    FAIL: test_end_strip (__main__.TestList)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "<ipython-input-17-6f9cfa1a997f>", line 19, in test_end_strip
        assert_that(result,
    AssertionError:
    Expected: (a string starting with '  hello' and a string ending with 'world')
         but: a string starting with '  hello' was 'hello world'
   
   
    ======================================================================
    FAIL: test_start_strip (__main__.TestList)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "<ipython-input-17-6f9cfa1a997f>", line 14, in test_start_strip
        assert_that(result,
    AssertionError:
    Expected: (a string starting with 'hello' and a string ending with 'world  ')
         but: a string ending with 'world  ' was 'hello world'
   
   
    ----------------------------------------------------------------------
    Ran 3 tests in 0.007s
   
    FAILED (failures=2)复制代码

他们会失去一个没有同的谬误信息,显示去除了了过多的空缺:

source_string = "  hello world  "

@run_test
class TestList(unittest.TestCase):
   
    # 这是个赠品:它能够工作!
    def test_complete_strip(self):
        result = source_string.strip()
        assert_that(result,
                   all_of(starts_with("hello"), ends_with("world")))

    def test_start_strip(self):
        result = source_string.lstrip() # Fixed this line
        assert_that(result,
                   all_of(starts_with("hello"), ends_with("world  ")))

    def test_end_strip(self):
        result = source_string.rstrip() # Fixed this line
        assert_that(result,
                   all_of(starts_with("  hello"), ends_with("world")))复制代码
    test_complete_strip (__main__.TestList) ... ok
    test_end_strip (__main__.TestList) ... ok
    test_start_strip (__main__.TestList) ... ok
   
    ----------------------------------------------------------------------
    Ran 3 tests in 0.005s
   
    OK复制代码

正在一个比拟实在的教程中,会有更多的例子以及更多的诠释。这类应用 Jupyter Notebook 的技术,有的例子能够用,有的例子需求修改,能够用于及时教授教养,能够用于视频课,乃至,能够用更多的其它零星用处,让先生本人实现一个教程。

如今就去分享你的常识吧!

更多相干收费学习保举:python视频教程

以上就是应用Jupyter Notebook 学习 Python的具体内容,更多请存眷资源魔其它相干文章!

标签: python教程 python编程 python使用问题 学习Python

抱歉,评论功能暂时关闭!