這篇文章主要介紹了使用Jupyter Notebook學(xué)習(xí)Python的方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司擁有網(wǎng)站維護(hù)技術(shù)和項(xiàng)目管理團(tuán)隊(duì),建立的售前、實(shí)施和售后服務(wù)體系,為客戶提供定制化的成都做網(wǎng)站、網(wǎng)站制作、網(wǎng)站維護(hù)、珉田數(shù)據(jù)中心解決方案。為客戶網(wǎng)站安全和日常運(yùn)維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護(hù)服務(wù)覆蓋集團(tuán)企業(yè)、上市公司、外企網(wǎng)站、商城網(wǎng)站開(kāi)發(fā)、政府網(wǎng)站等各類型客戶群體,為全球近千家企業(yè)提供全方位網(wǎng)站維護(hù)、服務(wù)器維護(hù)解決方案。
有了 Jupyter、PyHamcrest,用一點(diǎn)測(cè)試的代碼把它們連在一起,你就可以教任何適用于單元測(cè)試的 Python 內(nèi)容。
關(guān)于 Ruby 社區(qū)的一些事情一直讓我印象深刻,其中兩個(gè)例子是對(duì)測(cè)試的承諾和對(duì)易于上手的強(qiáng)調(diào)。這兩方面最好的例子是 Ruby Koans,在這里你可以通過(guò)修復(fù)測(cè)試來(lái)學(xué)習(xí) Ruby。
要是我們能把這些神奇的工具也用于 Python,我們應(yīng)該可以做得更好。是的,使用 Jupyter Notebook、PyHamcrest,再加上一點(diǎn)類似于膠帶的粘合代碼,我們可以做出一個(gè)包括教學(xué)、可工作的代碼和需要修復(fù)的代碼的教程。
首先,需要一些“膠布”。通常,你會(huì)使用一些漂亮的命令行測(cè)試器來(lái)做測(cè)試,比如 pytest 或 virtue。通常,你甚至不會(huì)直接運(yùn)行它。你使用像 tox 或 nox 這樣的工具來(lái)運(yùn)行它。然而,對(duì)于 Jupyter 來(lái)說(shuō),你需要寫一小段粘合代碼,可以直接在其中運(yùn)行測(cè)試。
幸運(yùn)的是,這個(gè)代碼又短又簡(jiǎn)單:
import unittest def run_test(klass): suite = unittest.TestLoader().loadTestsFromTestCase(klass) unittest.TextTestRunner(verbosity=2).run(suite) return klass復(fù)制代碼
現(xiàn)在,裝備已經(jīng)就緒,可以進(jìn)行第一次練習(xí)了。
在教學(xué)中,從一個(gè)簡(jiǎn)單的練習(xí)開(kāi)始,建立信心總是一個(gè)好主意。
那么,讓我們來(lái)修復(fù)一個(gè)非常簡(jiǎn)單的測(cè)試:
@run_test class TestNumbers(unittest.TestCase): def test_equality(self): expected_value = 3 # 只改這一行 self.assertEqual(1+1, expected_value)復(fù)制代碼
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)復(fù)制代碼
“只改這一行” 對(duì)學(xué)生來(lái)說(shuō)是一個(gè)有用的標(biāo)記。它準(zhǔn)確地表明了需要修改的內(nèi)容。否則,學(xué)生可以通過(guò)將第一行改為 return
來(lái)修復(fù)測(cè)試。
在這種情況下,修復(fù)很容易:
@run_test class TestNumbers(unittest.TestCase): def test_equality(self): expected_value = 2 # 修復(fù)后的代碼行 self.assertEqual(1+1, expected_value)復(fù)制代碼
test_equality (__main__.TestNumbers) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.002s OK復(fù)制代碼
然而,很快,unittest
庫(kù)的原生斷言將被證明是不夠的。在 pytest
中,通過(guò)重寫 assert
中的字節(jié)碼來(lái)解決這個(gè)問(wèn)題,使其具有神奇的屬性和各種啟發(fā)式方法。但這在 Jupyter notebook 中就不容易實(shí)現(xiàn)了。是時(shí)候挖出一個(gè)好的斷言庫(kù)了: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))復(fù)制代碼
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)復(fù)制代碼
PyHamcrest 不僅擅長(zhǎng)靈活的斷言,它還擅長(zhǎng)清晰的錯(cuò)誤信息。正因?yàn)槿绱耍瑔?wèn)題就顯而易見(jiàn)了。[1, 5, 3]
不包含 2
,而且看起來(lái)很丑:
@run_test class TestList(unittest.TestCase): def test_equality(self): things = [1, 2, # 改完的行 3] assert_that(things, has_items(1, 2, 3))復(fù)制代碼
test_equality (__main__.TestList) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.001s OK復(fù)制代碼
使用 Jupyter、PyHamcrest 和一點(diǎn)測(cè)試的粘合代碼,你可以教授任何適用于單元測(cè)試的 Python 主題。
例如,下面可以幫助展示 Python 從字符串中去掉空白的不同方法之間的差異。
source_string = " hello world " @run_test class TestList(unittest.TestCase): # 這是個(gè)贈(zèng)品:它可以工作! 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")))復(fù)制代碼
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)復(fù)制代碼
理想情況下,學(xué)生們會(huì)意識(shí)到 .lstrip()
和 .rstrip()
這兩個(gè)方法可以滿足他們的需要。但如果他們不這樣做,而是試圖到處使用 .strip()
的話:
source_string = " hello world " @run_test class TestList(unittest.TestCase): # 這是個(gè)贈(zèng)品:它可以工作! 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")))復(fù)制代碼
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)復(fù)制代碼
他們會(huì)得到一個(gè)不同的錯(cuò)誤信息,顯示去除了過(guò)多的空白:
source_string = " hello world " @run_test class TestList(unittest.TestCase): # 這是個(gè)贈(zèng)品:它可以工作! 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")))復(fù)制代碼
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復(fù)制代碼
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享使用Jupyter Notebook學(xué)習(xí)Python的方法內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問(wèn)題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!