Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to teach Python with Jupyter Notebook

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Jupyter Notebook to teach Python", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to teach Python with Jupyter Notebook.

First of all, we need some "duct tape". Usually, you will use some nice command-line tester for testing, such as pytest or virtue. Usually, you don't even run it directly. You use a tool like tox or nox to run it. For Jupyter, however, you need to write a small piece of glue code in which you can run tests directly.

Fortunately, the code is short and simple:

Import unittest def run_test (klass): suite = unittest.TestLoader () .loadTestsFromTestCase (klass) unittest.TextTestRunner (verbosity=2) .run (suite) return klass

Now, the equipment is ready for the first practice.

In teaching, it is always a good idea to build confidence, starting with a simple exercise.

So, let's fix a very simple test:

@ run_testclass TestNumbers (unittest.TestCase): def test_equality (self): expected_value = 3 # change only this line 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 (1x1) Expected_value) AssertionError: 2! = 3-Ran 1 test in 0.002s FAILED (failures=1)

"only change this line" is a useful mark for students. It accurately indicates what needs to be modified. Otherwise, students can fix the test by changing the first line to return.

In this case, it is easy to fix:

@ run_testclass TestNumbers (unittest.TestCase): def test_equality (self): expected_value = 2 # repaired line of code self.assertEqual (1x1, expected_value) test_equality (_ _ main__.TestNumbers). Ok-Ran 1 test in 0.002s OK

Soon, however, the native assertions of the unittest library will prove to be insufficient. In pytest, you solve this problem by rewriting the bytecode in assert to make it have magical properties and various heuristics. But this is not easy to achieve in Jupyter notebook. It's time to dig up a good library of assertions: PyHamcrest.

From hamcrest import * @ run_testclass TestList (unittest.TestCase): def test_equality (self): things = [1, 5, # only this line 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 and a sequence containing and a sequence containing) but: a sequence containing was-Ran 1 test in 0.004s FAILED (failures=1)

PyHamcrest is not only good at flexible assertions, it is also good at clear error messages. Because of this, the problem is obvious. [1, 5, 3] does not contain 2 and looks ugly:

Run_testclass TestList (unittest.TestCase): def test_equality (self): things = [1, 2, # modified line 3] assert_that (things, has_items (1, 2, 3)) test_equality (_ _ main__.TestList). Ok-Ran 1 test in 0.001s OK

Using Jupyter, PyHamcrest, and glue code for a little test, you can teach any Python topic suitable for unit testing.

For example, the following can help show the differences between the different ways Python removes whitespace from a string.

Source_string = "hello world" @ run_testclass TestList (unittest.TestCase): # this is a giveaway: it works! 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 # change only this line assert_that (result, all_of (starts_with ("hello")) Ends_with ("world")) def test_end_strip (self): result = source_string # change only this line 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)

Ideally, students will realize that .lstrip () and .rstrip () can meet their needs. But if they try to use .strip () everywhere instead of doing so:

Source_string = "hello world" @ run_testclass TestList (unittest.TestCase): # this is a giveaway: it works! 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 () # modified line assert_that (result, all_of (starts_with ("hello")) Ends_with ("world")) def test_end_strip (self): result = source_string.strip () # modified line 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)

They will get a different error message showing that too much white space has been removed:

Source_string = "hello world" @ run_testclass TestList (unittest.TestCase): # this is a giveaway: it works! 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 so far, I believe you have a deeper understanding of "how to teach Python with Jupyter Notebook". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report