老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

使用JupyterNotebook學習Python

有了 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 "", 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 "", 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 "", 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 "", 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 "", 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 "", 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視頻教程


名稱欄目:使用JupyterNotebook學習Python
網頁URL:http://www.xueling.net.cn/article/cjjegd.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 综合在线一区 | 欧美曰逼视频 | 九九热线视频只有这里最精品 | 人人爽亚洲AV人人爽AV人人片 | 亚洲精品TV久久久久久久久J | 日日摸夜夜添夜夜爽免费视频 | 国产精品日日做人人爱 | 亚洲国产精品VA在线观看黑人 | 成人你懂的 | 亚洲夜夜综合 | 性少妇tubevⅰdeos高清 | 日韩精品人妻中文字幕有码 | 欧美性猛交xxxx乱大交少妇 | 久久精品国产99精品国产2021 | 国产成人精品久久免费动漫 | 国产成人小视频在线 | 中文乱码人妻系列一区 | 一本久久a久久精品综合 | 国产一精品一AV一免费爽爽 | 久久久国产精品萌白酱免费 | 少妇真实被内射视频三四区 | 国产性网| 国产精品9区 | 日本丰满老妇BBW | 欧美GV肉片视频免费观看 | 色婷婷久久 | 深夜A级毛片免费视频 | 亚洲自拍一区在线观看 | 国产免费av片在线观看麻豆 | 亚洲3级 | 亚洲AV午夜精品一区二区三区 | 欧美熟妇的性裸交 | 国产精品一区二区av麻豆 | 555www色欧美视频 | 妺妺窝人体色WWW看美女 | 在线免费h视频 | 成人免费毛片内射美女-百度 | 国产一级义婬片AAA毛片久久 | 91影院高清 | a级在线播放| 亚洲日韩高清aⅴ在线观看 四区在线观看 |