Skip to content

Commit 2ddf97f

Browse files
edit testing and add numerizer
1 parent 6d8a379 commit 2ddf97f

File tree

10 files changed

+1173
-196
lines changed

10 files changed

+1173
-196
lines changed

Chapter5/natural_language_processing.ipynb

Lines changed: 462 additions & 49 deletions
Large diffs are not rendered by default.

Chapter5/test_freezegun.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from freezegun import freeze_time
2+
import datetime
3+
4+
def get_day_of_week():
5+
return datetime.datetime.now().weekday()
6+
7+
@freeze_time("2024-10-13")
8+
def test_get_day_of_week():
9+
assert get_day_of_week() == 6

Chapter5/testing.ipynb

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@
23082308
"id": "915e9960",
23092309
"metadata": {},
23102310
"source": [
2311-
"### FreezeGun: Freeze Dynamic Time in Unit Testing "
2311+
"### FreezeGun: Freezing Time for Reliable Python Testing"
23122312
]
23132313
},
23142314
{
@@ -2325,21 +2325,37 @@
23252325
"!pip install freezegun"
23262326
]
23272327
},
2328+
{
2329+
"cell_type": "markdown",
2330+
"id": "d979b680-a898-402b-b92d-412cfbb3572c",
2331+
"metadata": {},
2332+
"source": [
2333+
"Testing time-dependent functions can be challenging and unreliable as the results may vary based on when the test is executed. This results in flaky tests that pass or fail inconsistently."
2334+
]
2335+
},
23282336
{
23292337
"attachments": {},
23302338
"cell_type": "markdown",
23312339
"id": "85ef39bd",
23322340
"metadata": {},
23332341
"source": [
2334-
"Unit tests require static input, but time is dynamic and constantly changing. With FreezeGun, you can freeze time to a specific point, ensuring accurate verification of the tested features."
2342+
"With FreezeGun, you can freeze time at a particular point, ensuring your tests always run with the same date context."
23352343
]
23362344
},
23372345
{
23382346
"cell_type": "code",
2339-
"execution_count": null,
2347+
"execution_count": 6,
23402348
"id": "62213cd9",
23412349
"metadata": {},
2342-
"outputs": [],
2350+
"outputs": [
2351+
{
2352+
"name": "stdout",
2353+
"output_type": "stream",
2354+
"text": [
2355+
"Overwriting test_freezegun.py\n"
2356+
]
2357+
}
2358+
],
23432359
"source": [
23442360
"%%writefile test_freezegun.py\n",
23452361
"from freezegun import freeze_time\n",
@@ -2348,9 +2364,9 @@
23482364
"def get_day_of_week():\n",
23492365
" return datetime.datetime.now().weekday()\n",
23502366
"\n",
2351-
"@freeze_time(\"2023-06-13\")\n",
2367+
"@freeze_time(\"2024-10-13\")\n",
23522368
"def test_get_day_of_week():\n",
2353-
" assert get_day_of_week() == 1\n"
2369+
" assert get_day_of_week() == 6\n"
23542370
]
23552371
},
23562372
{
@@ -2366,7 +2382,7 @@
23662382
},
23672383
{
23682384
"cell_type": "code",
2369-
"execution_count": 16,
2385+
"execution_count": 7,
23702386
"id": "376870d5",
23712387
"metadata": {
23722388
"tags": [
@@ -2379,21 +2395,32 @@
23792395
"output_type": "stream",
23802396
"text": [
23812397
"\u001b[1m============================= test session starts ==============================\u001b[0m\n",
2382-
"platform darwin -- Python 3.9.6, pytest-7.2.1, pluggy-1.0.0\n",
2398+
"platform darwin -- Python 3.11.6, pytest-8.2.0, pluggy-1.5.0\n",
2399+
"Fugue tests will be initialized with options:\n",
23832400
"rootdir: /Users/khuyentran/book/Efficient_Python_tricks_and_tools_for_data_scientists/Chapter5\n",
2384-
"plugins: anyio-3.6.2\n",
2401+
"plugins: anyio-4.0.0, dash-2.17.1, fugue-0.9.1, Faker-19.13.0, returns-0.22.0\n",
23852402
"collected 1 item \u001b[0m\n",
23862403
"\n",
23872404
"test_freezegun.py \u001b[32m.\u001b[0m\u001b[32m [100%]\u001b[0m\n",
23882405
"\n",
2389-
"\u001b[32m============================== \u001b[32m\u001b[1m1 passed\u001b[0m\u001b[32m in 0.03s\u001b[0m\u001b[32m ===============================\u001b[0m\n"
2406+
"\u001b[32m============================== \u001b[32m\u001b[1m1 passed\u001b[0m\u001b[32m in 0.07s\u001b[0m\u001b[32m ===============================\u001b[0m\n"
23902407
]
23912408
}
23922409
],
23932410
"source": [
23942411
"!pytest test_freezegun.py"
23952412
]
23962413
},
2414+
{
2415+
"cell_type": "markdown",
2416+
"id": "9274e666-b811-4997-9764-117ec5257f90",
2417+
"metadata": {},
2418+
"source": [
2419+
"This code uses `get_day_of_week()` to return the current weekday (0-6). The `@freeze_time(\"2024-10-13\")` decorator sets a fixed date (Sunday, October 13, 2024).\n",
2420+
"\n",
2421+
"The test calls `get_day_of_week()` and checks if it returns 6 (Sunday). This test will consistently pass because FreezeGun ensures `datetime.datetime.now()` always returns the specified frozen date."
2422+
]
2423+
},
23972424
{
23982425
"attachments": {},
23992426
"cell_type": "markdown",

docs/Chapter5/natural_language_processing.html

Lines changed: 136 additions & 60 deletions
Large diffs are not rendered by default.

docs/Chapter5/testing.html

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ <h2> Contents </h2>
531531
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#pytest-steps-share-data-between-tests">6.13.16. pytest-steps: Share Data Between Tests</a></li>
532532
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#pytest-picked-run-the-tests-related-to-the-unstaged-files-in-git">6.13.17. pytest-picked: Run the Tests Related to the Unstaged Files in Git</a></li>
533533
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#efficient-testing-of-python-class-with-setup-method">6.13.18. Efficient Testing of Python Class with setUp Method</a></li>
534-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#freezegun-freeze-dynamic-time-in-unit-testing">6.13.19. FreezeGun: Freeze Dynamic Time in Unit Testing</a></li>
534+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#freezegun-freezing-time-for-reliable-python-testing">6.13.19. FreezeGun: Freezing Time for Reliable Python Testing</a></li>
535535
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simulate-external-services-in-testing-with-mock-objects">6.13.20. Simulate External Services in Testing with Mock Objects</a></li>
536536
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#pytest-mock-vs-unittest-mock-simplifying-mocking-in-python-tests">6.13.21. pytest-mock vs unittest.mock: Simplifying Mocking in Python Tests</a></li>
537537
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#tmp-path-create-a-temporary-directory-for-testing">6.13.22. tmp_path: Create a Temporary Directory for Testing</a></li>
@@ -1758,8 +1758,8 @@ <h2><span class="section-number">6.13.18. </span>Efficient Testing of Python Cla
17581758
</div>
17591759
</div>
17601760
</section>
1761-
<section id="freezegun-freeze-dynamic-time-in-unit-testing">
1762-
<h2><span class="section-number">6.13.19. </span>FreezeGun: Freeze Dynamic Time in Unit Testing<a class="headerlink" href="#freezegun-freeze-dynamic-time-in-unit-testing" title="Permalink to this heading">#</a></h2>
1761+
<section id="freezegun-freezing-time-for-reliable-python-testing">
1762+
<h2><span class="section-number">6.13.19. </span>FreezeGun: Freezing Time for Reliable Python Testing<a class="headerlink" href="#freezegun-freezing-time-for-reliable-python-testing" title="Permalink to this heading">#</a></h2>
17631763
<div class="cell tag_hide-cell docutils container">
17641764
<details class="hide above-input">
17651765
<summary aria-label="Toggle hidden content">
@@ -1773,7 +1773,8 @@ <h2><span class="section-number">6.13.19. </span>FreezeGun: Freeze Dynamic Time
17731773
</div>
17741774
</details>
17751775
</div>
1776-
<p>Unit tests require static input, but time is dynamic and constantly changing. With FreezeGun, you can freeze time to a specific point, ensuring accurate verification of the tested features.</p>
1776+
<p>Testing time-dependent functions can be challenging and unreliable as the results may vary based on when the test is executed. This results in flaky tests that pass or fail inconsistently.</p>
1777+
<p>With FreezeGun, you can freeze time at a particular point, ensuring your tests always run with the same date context.</p>
17771778
<div class="cell docutils container">
17781779
<div class="cell_input docutils container">
17791780
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">%%writefile</span> test_freezegun.py
@@ -1783,9 +1784,14 @@ <h2><span class="section-number">6.13.19. </span>FreezeGun: Freeze Dynamic Time
17831784
<span class="k">def</span> <span class="nf">get_day_of_week</span><span class="p">():</span>
17841785
<span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span>
17851786

1786-
<span class="nd">@freeze_time</span><span class="p">(</span><span class="s2">&quot;2023-06-13&quot;</span><span class="p">)</span>
1787+
<span class="nd">@freeze_time</span><span class="p">(</span><span class="s2">&quot;2024-10-13&quot;</span><span class="p">)</span>
17871788
<span class="k">def</span> <span class="nf">test_get_day_of_week</span><span class="p">():</span>
1788-
<span class="k">assert</span> <span class="n">get_day_of_week</span><span class="p">()</span> <span class="o">==</span> <span class="mi">1</span>
1789+
<span class="k">assert</span> <span class="n">get_day_of_week</span><span class="p">()</span> <span class="o">==</span> <span class="mi">6</span>
1790+
</pre></div>
1791+
</div>
1792+
</div>
1793+
<div class="cell_output docutils container">
1794+
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Overwriting test_freezegun.py
17891795
</pre></div>
17901796
</div>
17911797
</div>
@@ -1796,18 +1802,21 @@ <h2><span class="section-number">6.13.19. </span>FreezeGun: Freeze Dynamic Time
17961802
<div class="cell tag_remove-input docutils container">
17971803
<div class="cell_output docutils container">
17981804
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span><span class=" -Color -Color-Bold">============================= test session starts ==============================</span>
1799-
platform darwin -- Python 3.9.6, pytest-7.2.1, pluggy-1.0.0
1805+
platform darwin -- Python 3.11.6, pytest-8.2.0, pluggy-1.5.0
1806+
Fugue tests will be initialized with options:
18001807
rootdir: /Users/khuyentran/book/Efficient_Python_tricks_and_tools_for_data_scientists/Chapter5
1801-
plugins: anyio-3.6.2
1808+
plugins: anyio-4.0.0, dash-2.17.1, fugue-0.9.1, Faker-19.13.0, returns-0.22.0
18021809
collected 1 item
18031810

18041811
test_freezegun.py <span class=" -Color -Color-Green">. [100%]</span>
18051812

1806-
<span class=" -Color -Color-Green">============================== </span><span class=" -Color -Color-Bold -Color-Bold-Green">1 passed</span><span class=" -Color -Color-Green"> in 0.03s ===============================</span>
1813+
<span class=" -Color -Color-Green">============================== </span><span class=" -Color -Color-Bold -Color-Bold-Green">1 passed</span><span class=" -Color -Color-Green"> in 0.07s ===============================</span>
18071814
</pre></div>
18081815
</div>
18091816
</div>
18101817
</div>
1818+
<p>This code uses <code class="docutils literal notranslate"><span class="pre">get_day_of_week()</span></code> to return the current weekday (0-6). The <code class="docutils literal notranslate"><span class="pre">&#64;freeze_time(&quot;2024-10-13&quot;)</span></code> decorator sets a fixed date (Sunday, October 13, 2024).</p>
1819+
<p>The test calls <code class="docutils literal notranslate"><span class="pre">get_day_of_week()</span></code> and checks if it returns 6 (Sunday). This test will consistently pass because FreezeGun ensures <code class="docutils literal notranslate"><span class="pre">datetime.datetime.now()</span></code> always returns the specified frozen date.</p>
18111820
<p><a class="reference external" href="https://github.com/spulec/freezegun">Link to FreezeGun</a>.</p>
18121821
</section>
18131822
<section id="simulate-external-services-in-testing-with-mock-objects">
@@ -3235,7 +3244,7 @@ <h2><span class="section-number">6.13.32. </span>DeepEval: Unit Testing for Your
32353244
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#pytest-steps-share-data-between-tests">6.13.16. pytest-steps: Share Data Between Tests</a></li>
32363245
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#pytest-picked-run-the-tests-related-to-the-unstaged-files-in-git">6.13.17. pytest-picked: Run the Tests Related to the Unstaged Files in Git</a></li>
32373246
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#efficient-testing-of-python-class-with-setup-method">6.13.18. Efficient Testing of Python Class with setUp Method</a></li>
3238-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#freezegun-freeze-dynamic-time-in-unit-testing">6.13.19. FreezeGun: Freeze Dynamic Time in Unit Testing</a></li>
3247+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#freezegun-freezing-time-for-reliable-python-testing">6.13.19. FreezeGun: Freezing Time for Reliable Python Testing</a></li>
32393248
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simulate-external-services-in-testing-with-mock-objects">6.13.20. Simulate External Services in Testing with Mock Objects</a></li>
32403249
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#pytest-mock-vs-unittest-mock-simplifying-mocking-in-python-tests">6.13.21. pytest-mock vs unittest.mock: Simplifying Mocking in Python Tests</a></li>
32413250
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#tmp-path-create-a-temporary-directory-for-testing">6.13.22. tmp_path: Create a Temporary Directory for Testing</a></li>

docs/README.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ <h2> Contents </h2>
482482
<nav aria-label="Page">
483483
<ul class="visible nav section-nav flex-column">
484484
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#">What Should You Expect From This Book?</a></li>
485-
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#about-this-book">About This Book</a></li>
485+
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#printable-pdf-guide">Printable PDF Guide</a></li>
486486
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#about-the-author">About The Author</a></li>
487487
</ul>
488488

@@ -519,9 +519,9 @@ <h1>What Should You Expect From This Book?<a class="headerlink" href="#what-shou
519519
<p>This book expects you to have some basic knowledge of Python and data science.</p>
520520
<p>You should also expect bite-size code snippets for each section. This will allow you to obtain multiple pieces of knowledge in fewer than one minute. I included the link to the resources for every tools introduced in case you want to explore them further.</p>
521521
</section>
522-
<section id="about-this-book">
523-
<h1>About This Book<a class="headerlink" href="#about-this-book" title="Permalink to this heading">#</a></h1>
524-
<p>This book includes more than 800 tips and tools I have shared daily on my website, <a class="reference external" href="https://codecut.ai/?utm_source=github&amp;utm_medium=efficient_python_tricks&amp;utm_campaign=about_this_book">CodeCut</a>. If you want to get the updated of new tips on your mailbox, you can subscribe to <a class="reference external" href="https://codecut.ai/daily-tips-form/?utm_source=github&amp;utm_medium=efficient_python_tricks&amp;utm_campaign=subscribe_to_newsletter">my newsletter</a>.</p>
522+
<section id="printable-pdf-guide">
523+
<h1>Printable PDF Guide<a class="headerlink" href="#printable-pdf-guide" title="Permalink to this heading">#</a></h1>
524+
<p>For a printer-friendly version of the tools mentioned in this book, sign up for <a class="reference external" href="https://codecut.ai/data-scientist-toolkit/?utm_source=github&amp;utm_medium=defficient_python_tricks&amp;utm_campaign=free_pdf">CodeCut’s free PDF guide</a>. This comprehensive 264-page document covers over 100 essential data science tools, providing you with a valuable reference that you can print and keep at your desk.</p>
525525
</section>
526526
<section id="about-the-author">
527527
<h1>About The Author<a class="headerlink" href="#about-the-author" title="Permalink to this heading">#</a></h1>
@@ -586,7 +586,7 @@ <h1>About The Author<a class="headerlink" href="#about-the-author" title="Permal
586586
<nav class="bd-toc-nav page-toc">
587587
<ul class="visible nav section-nav flex-column">
588588
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#">What Should You Expect From This Book?</a></li>
589-
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#about-this-book">About This Book</a></li>
589+
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#printable-pdf-guide">Printable PDF Guide</a></li>
590590
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#about-the-author">About The Author</a></li>
591591
</ul>
592592

0 commit comments

Comments
 (0)