찾다
백엔드 개발파이썬 튜토리얼Jupyter Notebook을 사용하여 Python 배우기

Jupyter Notebook을 사용하여 Python 배우기

Sep 28, 2020 pm 04:26 PM
파이썬을 배우다

Jupyter, PyHamcrest 및 이들을 함께 묶는 작은 테스트 코드를 사용하면 단위 테스트에 적합한 Python 콘텐츠를 가르칠 수 있습니다.

Python 비디오 튜토리얼 칼럼에서 자세히 소개합니다~

Jupyter Notebook을 사용하여 Python 배우기

Ruby 커뮤니티에 관한 몇 가지 예는 항상 저에게 깊은 인상을 주었습니다. 두 가지 예는 테스트에 대한 헌신과 시작의 용이성에 대한 강조입니다. . 둘 다의 가장 좋은 예는 테스트를 수정하여 Ruby를 배울 수 있는 Ruby Koans입니다.

이러한 놀라운 도구를 Python에 도입할 수만 있다면 더 나은 결과를 얻을 수 있을 것입니다. 예, Jupyter Notebook, PyHamcrest 및 약간의 테이프 같은 접착 코드를 사용하여 지침, 작업 코드 및 수정이 필요한 코드가 포함된 튜토리얼을 만들 수 있습니다.

먼저 "테이프"가 필요합니다. 일반적으로 pytest나 Virity와 같은 멋진 명령줄 테스터를 사용하여 테스트를 수행합니다. 종종 직접 실행하지도 않습니다. 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 "<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)复制代码

"Change just this line"은 학생들에게 유용한 표기법입니다. 수정해야 할 내용을 정확하게 나타냅니다. 그렇지 않으면 학생들은 첫 번째 줄을 return으로 변경하여 테스트를 수정할 수 있습니다. 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 "<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_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 "<ipython-input-16-3db7465bd5bf>", line 19, in test_end_strip
        assert_that(result,
    AssertionError:
    Expected: (a string starting with &#39;  hello&#39; and a string ending with &#39;world&#39;)
         but: a string ending with &#39;world&#39; was &#39;  hello world  &#39;
   
   
    ======================================================================
    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 &#39;hello&#39; and a string ending with &#39;world  &#39;)
         but: a string starting with &#39;hello&#39; was &#39;  hello world  &#39;
   
   
    ----------------------------------------------------------------------
    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 "<ipython-input-17-6f9cfa1a997f>", line 19, in test_end_strip
        assert_that(result,
    AssertionError:
    Expected: (a string starting with &#39;  hello&#39; and a string ending with &#39;world&#39;)
         but: a string starting with &#39;  hello&#39; was &#39;hello world&#39;
   
   
    ======================================================================
    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 &#39;hello&#39; and a string ending with &#39;world  &#39;)
         but: a string ending with &#39;world  &#39; was &#39;hello world&#39;
   
   
    ----------------------------------------------------------------------
    Ran 3 tests in 0.007s
   
    FAILED (failures=2)复制代码

그러나 곧 unittest 라이브러리의 기본 어설션이 불충분하다는 것이 증명될 것입니다. pytest에서는 마법 속성과 다양한 경험적 방법을 갖도록 assert의 바이트코드를 다시 작성하여 이 문제를 해결합니다. 하지만 Jupyter Notebook에서는 이를 달성하기가 쉽지 않습니다. 이제 좋은 주장 라이브러리인 PyHamcrest를 알아볼 시간입니다.

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复制代码

PyHamcrest는 유연한 주장뿐 아니라 명확한 오류 메시지에도 능숙합니다. 이 때문에 문제는 명백합니다. [1, 5, 3]에는 2가 포함되어 있지 않으며 보기에도 좋지 않습니다.

rrreeerrreee

Jupyter, PyHamcrest를 사용한 글루 코드와 약간의 테스트를 사용하면 모든 Python 테마를 가르칠 수 있습니다. 단위 테스트용.

예를 들어, 다음은 문자열에서 공백을 제거하는 Python의 다양한 방법 간의 차이점을 보여주는 데 도움이 됩니다. rrreeerrreee이상적으로는 학생들이 .lstrip().rstrip()이 자신의 요구 사항을 충족한다는 것을 깨닫게 됩니다. 하지만 그렇게 하지 않고 대신 .strip()을 모든 곳에서 사용하려고 하면 rrreeerrreee 너무 많은 공백이 제거되었음을 보여주는 다른 오류 메시지가 표시됩니다. In a에서 rrreeerrreee 더 현실적인 튜토리얼이 있으면 더 많은 예시와 설명이 나올 것입니다. Jupyter Notebook을 사용하는 이 기술은 일부 예에 사용될 수 있으며 일부 예는 수정이 필요합니다. 실시간 교육, 비디오 수업 및 기타 분산된 목적으로 사용할 수 있으므로 학생들이 튜토리얼을 완료할 수 있습니다. 그들 자신.

지금 지식을 공유하세요! 🎜🎜🎜🎜 관련 무료 학습 추천: 🎜🎜🎜python 비디오 튜토리얼🎜🎜🎜🎜

위 내용은 Jupyter Notebook을 사용하여 Python 배우기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
이 기사는 juejin에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제
요소를 파이썬 어레이에 어떻게 추가합니까?요소를 파이썬 어레이에 어떻게 추가합니까?Apr 30, 2025 am 12:19 AM

inpython, youappendElementStoalistUsingTheAppend () 메소드 1) useappend () forsinglelements : my_list.append (4) .2) useextend () 또는 = formultiplementements : my_list.extend (other_list) 또는 my_list = [4,5,6] .3) useinsert () forspecificpositions : my_list.insert (1,5) .Bearware

Shebang 관련 문제를 어떻게 디버그합니까?Shebang 관련 문제를 어떻게 디버그합니까?Apr 30, 2025 am 12:17 AM

Shebang 문제를 디버깅하는 방법에는 다음이 포함됩니다. 1. Shebang 라인을 확인하여 스크립트의 첫 번째 줄인지 확인하고 접두사 공간이 없는지 확인하십시오. 2. 통역 경로가 올바른지 확인하십시오. 3. 통역사에게 직접 전화하여 스크립트를 실행하여 Shebang 문제를 분리하십시오. 4. Strace 또는 Trusts를 사용하여 시스템 호출을 추적합니다. 5. Shebang에 대한 환경 변수의 영향을 확인하십시오.

파이썬 어레이에서 요소를 어떻게 제거합니까?파이썬 어레이에서 요소를 어떻게 제거합니까?Apr 30, 2025 am 12:16 AM

pythonlistscanbemanipatedusingseveralmethodstoremoveElements : 1) geremove () methodremove () methodeMovestHefirstoccurrence.2) thePop () methodRemovesAndReTurnSanElementatAgivenIndex.3) THEDELSTATEMENTCANREMORENDEX.4) LESTCORHENSCREC

파이썬 목록에 어떤 데이터 유형을 저장할 수 있습니까?파이썬 목록에 어떤 데이터 유형을 저장할 수 있습니까?Apr 30, 2025 am 12:07 AM

PythonlistscanstoreAnydatataTATY, 문자열, 부유물, 부울, 기타 목록 및 디터 시어

Python 목록에서 수행 할 수있는 일반적인 작업은 무엇입니까?Python 목록에서 수행 할 수있는 일반적인 작업은 무엇입니까?Apr 30, 2025 am 12:01 AM

pythonlistssupportnumouseOperations : 1) addingElementSwitHappend (), extend (), andinsert ()

Numpy를 사용하여 다차원 배열을 어떻게 생성합니까?Numpy를 사용하여 다차원 배열을 어떻게 생성합니까?Apr 29, 2025 am 12:27 AM

다음 단계를 통해 Numpy를 사용하여 다차원 배열을 만들 수 있습니다. 1) Numpy.array () 함수를 사용하여 NP.Array ([[1,2,3], [4,5,6]]과 같은 배열을 생성하여 2D 배열을 만듭니다. 2) np.zeros (), np.ones (), np.random.random () 및 기타 함수를 사용하여 특정 값으로 채워진 배열을 만듭니다. 3) 서브 어레이의 길이가 일관되고 오류를 피하기 위해 배열의 모양과 크기 특성을 이해하십시오. 4) NP.Reshape () 함수를 사용하여 배열의 모양을 변경하십시오. 5) 코드가 명확하고 효율적인지 확인하기 위해 메모리 사용에주의를 기울이십시오.

Numpy 어레이에서 '방송'의 개념을 설명하십시오.Numpy 어레이에서 '방송'의 개념을 설명하십시오.Apr 29, 2025 am 12:23 AM

BroadcastingInnumpyIsamethodtoperformoperationsonArraysoffferentShapesByAutomicallyAligningThem.itsimplifiesCode, enourseadability, andboostsperformance.here'showitworks : 1) smalraysarepaddedwithonestomatchdimenseare

데이터 저장을 위해 목록, Array.Array 및 Numpy Array 중에서 선택하는 방법을 설명하십시오.데이터 저장을 위해 목록, Array.Array 및 Numpy Array 중에서 선택하는 방법을 설명하십시오.Apr 29, 2025 am 12:20 AM

forpythondatastorage, chooselistsforflexibilitywithmixeddatatypes, array.arrayformemory-effic homogeneousnumericaldata, andnumpyarraysforadvancednumericalcomputing.listsareversatilebutlessefficipforlargenumericaldatasets.arrayoffersamiddlegro

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.