溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用Jupyter Notebook教Python

發布時間:2021-10-27 10:40:58 來源:億速云 閱讀:199 作者:iii 欄目:編程語言

本篇內容主要講解“怎么用Jupyter Notebook教Python”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么用Jupyter Notebook教Python”吧!

首先,需要一些“膠布”。通常,你會使用一些漂亮的命令行測試器來做測試,比如 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_testclass 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_testclass 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_testclass 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_testclass 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_testclass 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_testclass 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_testclass 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”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女