广告
返回顶部
首页 > 资讯 > 后端开发 > Python >怎么用Jupyter Notebook教Python
  • 503
分享到

怎么用Jupyter Notebook教Python

2023-06-16 03:06:16 503人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

本篇内容主要讲解“怎么用Jupyter Notebook教python”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Jupyter Notebook教Python”吧!首先,需要一些“胶布

本篇内容主要讲解“怎么用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”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: 怎么用Jupyter Notebook教Python

本文链接: https://www.lsjlt.com/news/282313.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么用Jupyter Notebook教Python
    本篇内容主要讲解“怎么用Jupyter Notebook教Python”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Jupyter Notebook教Python”吧!首先,需要一些“胶布...
    99+
    2023-06-16
  • Jupyter Notebook使用教程
    Jupyter Notebook使用教程 一、什么是Jupyter Notebook二、jupyter notebook的简单使用三、jupyter运行环境的配置——一python运行环境为例...
    99+
    2023-09-10
    jupyter python ide
  • Jupyter Notebook 安装与使用教程
    一、什么是Jupyter Notebook? 1. 简介        Jupyter Notebook是基于网页的用于交互计算的应用程序。其可被应用于全过程计算:开发、文档编写、运行代码和展示结果。——Jupyter Notebook官方...
    99+
    2023-09-20
    jupyter python ide
  • Jupyter Notebook怎么安装
    今天小编给大家分享一下Jupyter Notebook怎么安装的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。安装 VS Co...
    99+
    2023-06-19
  • 怎么运行Python的神器Jupyter Notebook
    这篇文章主要介绍了怎么运行Python的神器Jupyter Notebook,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Jupyter NotebookJupyter项目是...
    99+
    2023-06-15
  • 怎么远程使用jupyter notebook
    本篇内容介绍了“怎么远程使用jupyter notebook”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!...
    99+
    2022-10-19
  • 怎么在终端运行 Jupyter Notebook
    本篇内容介绍了“怎么在终端运行 Jupyter Notebook ”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有...
    99+
    2022-10-19
  • Jupyter Notebook内使用argparse报错怎么办
    小编给大家分享一下Jupyter Notebook内使用argparse报错怎么办,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!Jupyter Notebook内使用argparse报错在github上下载了代码来学习时,...
    99+
    2023-06-15
  • Anaconda与Jupyter Notebook入门级详细使用教程
    Anaconda 简介 我们用Anaconda发行版作为Python的使用环境。Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。 注...
    99+
    2023-09-02
    jupyter python ide anaconda 入门教程
  • 怎么安装运行和连接Jupyter Notebook
    这篇文章主要介绍了怎么安装运行和连接Jupyter Notebook,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Jupyter Notebook是一个开源的交互式Web应用...
    99+
    2023-06-04
  • 怎么远程使用服务器上的Jupyter notebook
    本文小编为大家详细介绍“怎么远程使用服务器上的Jupyter notebook”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么远程使用服务器上的Jupyter notebook”文章能帮助大家解决疑惑,下面跟着小编的...
    99+
    2023-07-05
  • Python中Jupyter notebook的常用快捷键有哪些
    这篇文章主要介绍了Python中Jupyter notebook的常用快捷键有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。python是什么意思Python是一种跨平台...
    99+
    2023-06-14
  • Jupyter notebook中怎么添加Pytorch运行环境
    这篇文章主要介绍“Jupyter notebook中怎么添加Pytorch运行环境”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Jupyter notebook中怎么添加Pyto...
    99+
    2023-07-05
  • 在VSCode里怎么使用Jupyter Notebook调试Java代码
    本篇内容介绍了“在VSCode里怎么使用Jupyter Notebook调试Java代码”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所...
    99+
    2023-07-02
  • jupyter notebook中图片显示不出来怎么办
    这篇文章主要介绍jupyter notebook中图片显示不出来怎么办,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!报错:D:\Program Files\Anaconda3\lib\site-packages\ma...
    99+
    2023-06-14
  • Jupyter notebook出现不自动弹出网页怎么办
    这篇文章主要介绍了Jupyter notebook出现不自动弹出网页怎么办,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Jupyter notebook 不自动弹出网页 第一...
    99+
    2023-06-15
  • 怎么解决Jupyter Notebook中文不能显示的问题
    小编给大家分享一下怎么解决Jupyter Notebook中文不能显示的问题,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!1、为什么不显示中文:Matplotlib默认不支持中文字符,因为默认的英文字体无法显示汉字。图片显...
    99+
    2023-06-14
  • 怎么在启动Jupyter Notebook时自动执行一段代码
    本篇内容主要讲解“怎么在启动Jupyter Notebook时自动执行一段代码”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么在启动Jupyter Notebook时自动执行一段代码”吧!在我...
    99+
    2023-06-16
  • Jupyter Notebook怎么修改字体和大小以及更改字体样式
    这篇文章给大家分享的是有关Jupyter Notebook怎么修改字体和大小以及更改字体样式的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Notebook 修改字体和大小原理很简单,就是更改CSS文件原本的字体很...
    99+
    2023-06-15
  • 使用Atom支持基于Jupyter的Python开教程详解
    有关于使用Atom进行Python开发的网上资料比较少,最近发现使用Atom结合Hydrogen插件进行Python开发,尤其是数据挖掘相关的工作,整体体验要好于Vscode,Vsc...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作