Skip to content

Commit 7b9948d

Browse files
add pipe
1 parent 4d27fa4 commit 7b9948d

File tree

5 files changed

+1150
-605
lines changed

5 files changed

+1150
-605
lines changed

Chapter6/alternative_approach.ipynb

Lines changed: 155 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@
478478
"id": "dba1f40e",
479479
"metadata": {},
480480
"source": [
481-
"### Pipe: Use Inflix Notation in Python"
481+
"### Pipe: A Elegant Alternative to Nested map and filter Calls in Python"
482482
]
483483
},
484484
{
@@ -500,87 +500,60 @@
500500
]
501501
},
502502
{
503-
"attachments": {},
504503
"cell_type": "markdown",
505-
"id": "d23f3a80",
504+
"id": "568929e4",
506505
"metadata": {},
507506
"source": [
508-
"Normally, you might use nested parentheses like below to combine multiple functions. "
507+
"Pipe is a Python library that enables infix notation (pipes), offering a cleaner alternative to nested function calls. Here are some of the most useful methods from the Pipe library:\n",
508+
"\n",
509+
"1. `select` and `where` (aliases for `map` and `filter`):\n",
510+
"\n",
511+
"Python's built-in `map` and `filter` functions are powerful tools for working with iterables, allowing for efficient data transformation and filtering. However, when used together, they can lead to code that's difficult to read due to nested function calls. For example:"
509512
]
510513
},
511514
{
512515
"cell_type": "code",
513-
"execution_count": 16,
514-
"id": "e3bcf8c1",
515-
"metadata": {
516-
"ExecuteTime": {
517-
"end_time": "2021-10-15T13:35:39.261323Z",
518-
"start_time": "2021-10-15T13:35:39.236544Z"
519-
}
520-
},
516+
"execution_count": 20,
517+
"id": "f3a26660",
518+
"metadata": {},
521519
"outputs": [
522520
{
523521
"data": {
524522
"text/plain": [
525523
"[4, 16, 36]"
526524
]
527525
},
528-
"execution_count": 16,
526+
"execution_count": 20,
529527
"metadata": {},
530528
"output_type": "execute_result"
531-
},
532-
{
533-
"data": {
534-
"application/javascript": "\n setTimeout(function() {\n var nbb_cell_id = 16;\n var nbb_unformatted_code = \"nums = [1, 2, 3, 4, 5, 6]\\nlist(\\n filter(lambda x: x % 2 == 0, \\n map(lambda x: x ** 2, nums)\\n )\\n)\";\n var nbb_formatted_code = \"nums = [1, 2, 3, 4, 5, 6]\\nlist(filter(lambda x: x % 2 == 0, map(lambda x: x ** 2, nums)))\";\n var nbb_cells = Jupyter.notebook.get_cells();\n for (var i = 0; i < nbb_cells.length; ++i) {\n if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n if (nbb_cells[i].get_text() == nbb_unformatted_code) {\n nbb_cells[i].set_text(nbb_formatted_code);\n }\n break;\n }\n }\n }, 500);\n ",
535-
"text/plain": [
536-
"<IPython.core.display.Javascript object>"
537-
]
538-
},
539-
"metadata": {},
540-
"output_type": "display_data"
541529
}
542530
],
543531
"source": [
544532
"nums = [1, 2, 3, 4, 5, 6]\n",
533+
"\n",
545534
"list(\n",
546535
" filter(lambda x: x % 2 == 0, \n",
547-
" map(lambda x: x ** 2, nums)\n",
548-
" )\n",
536+
" map(lambda x: x ** 2, nums)\n",
537+
" )\n",
549538
")"
550539
]
551540
},
552541
{
553-
"attachments": {},
554542
"cell_type": "markdown",
555-
"id": "f2183c8f",
543+
"id": "2a5fd571",
556544
"metadata": {},
557545
"source": [
558-
"If you want to increase the readability of your code by using pipes, try the library pipe. Below is an example using this library. "
559-
]
560-
},
561-
{
562-
"cell_type": "code",
563-
"execution_count": 1,
564-
"id": "100c6a98",
565-
"metadata": {
566-
"ExecuteTime": {
567-
"end_time": "2021-10-15T13:27:14.009183Z",
568-
"start_time": "2021-10-15T13:27:13.991954Z"
569-
}
570-
},
571-
"outputs": [],
572-
"source": [
573-
"from pipe import select, where"
546+
"Pipe allows for a more intuitive and readable way of chaining operations:"
574547
]
575548
},
576549
{
577550
"cell_type": "code",
578-
"execution_count": 15,
579-
"id": "87225efd",
551+
"execution_count": 21,
552+
"id": "e3bcf8c1",
580553
"metadata": {
581554
"ExecuteTime": {
582-
"end_time": "2021-10-15T13:35:18.480770Z",
583-
"start_time": "2021-10-15T13:35:18.463033Z"
555+
"end_time": "2021-10-15T13:35:39.261323Z",
556+
"start_time": "2021-10-15T13:35:39.236544Z"
584557
}
585558
},
586559
"outputs": [
@@ -590,37 +563,159 @@
590563
"[4, 16, 36]"
591564
]
592565
},
593-
"execution_count": 15,
566+
"execution_count": 21,
594567
"metadata": {},
595568
"output_type": "execute_result"
596-
},
597-
{
598-
"data": {
599-
"application/javascript": "\n setTimeout(function() {\n var nbb_cell_id = 15;\n var nbb_unformatted_code = \"list(\\n nums\\n | select(lambda x: x ** 2)\\n | where(lambda x: x % 2 == 0)\\n)\";\n var nbb_formatted_code = \"list(nums | select(lambda x: x ** 2) | where(lambda x: x % 2 == 0))\";\n var nbb_cells = Jupyter.notebook.get_cells();\n for (var i = 0; i < nbb_cells.length; ++i) {\n if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n if (nbb_cells[i].get_text() == nbb_unformatted_code) {\n nbb_cells[i].set_text(nbb_formatted_code);\n }\n break;\n }\n }\n }, 500);\n ",
600-
"text/plain": [
601-
"<IPython.core.display.Javascript object>"
602-
]
603-
},
604-
"metadata": {},
605-
"output_type": "display_data"
606569
}
607570
],
608571
"source": [
572+
"from pipe import select, where\n",
573+
"\n",
609574
"list(\n",
610575
" nums\n",
611576
" | select(lambda x: x ** 2)\n",
612577
" | where(lambda x: x % 2 == 0)\n",
613578
")"
614579
]
615580
},
581+
{
582+
"cell_type": "markdown",
583+
"id": "93caacc6",
584+
"metadata": {},
585+
"source": [
586+
"In this version, the operations are read from left to right, mirroring the order in which they're applied. The `select` method corresponds to `map`, while `where` corresponds to `filter`. This syntax not only improves readability but also makes it easier to add, remove, or reorder operations in your data processing pipeline."
587+
]
588+
},
589+
{
590+
"cell_type": "markdown",
591+
"id": "f86d3565",
592+
"metadata": {},
593+
"source": [
594+
"2. `traverse`:\n",
595+
"\n",
596+
"The `traverse` method recursively unfolds nested iterables, which is useful for flattening deeply nested lists:"
597+
]
598+
},
599+
{
600+
"cell_type": "code",
601+
"execution_count": 8,
602+
"id": "04039ee7",
603+
"metadata": {},
604+
"outputs": [],
605+
"source": [
606+
"from pipe import traverse\n"
607+
]
608+
},
609+
{
610+
"cell_type": "code",
611+
"execution_count": 14,
612+
"id": "48e0e73d",
613+
"metadata": {},
614+
"outputs": [
615+
{
616+
"name": "stdout",
617+
"output_type": "stream",
618+
"text": [
619+
"[1, 2, 3, 4, 5]\n"
620+
]
621+
}
622+
],
623+
"source": [
624+
"from pipe import traverse\n",
625+
"\n",
626+
"nested = [[1, 2, [3]], [4, 5]]\n",
627+
"flattened = list(nested | traverse)\n",
628+
"print(flattened) "
629+
]
630+
},
631+
{
632+
"cell_type": "markdown",
633+
"id": "71884dd2",
634+
"metadata": {},
635+
"source": [
636+
"3. `chain`:\n",
637+
"\n",
638+
"The `chain` method combines multiple iterables:"
639+
]
640+
},
641+
{
642+
"cell_type": "code",
643+
"execution_count": 15,
644+
"id": "40c1768d",
645+
"metadata": {},
646+
"outputs": [
647+
{
648+
"name": "stdout",
649+
"output_type": "stream",
650+
"text": [
651+
"[1, 2, 3, 4, 5]\n"
652+
]
653+
}
654+
],
655+
"source": [
656+
"from pipe import chain\n",
657+
"\n",
658+
"result = list([[1, 2], [3, 4], [5]] | chain)\n",
659+
"print(result)"
660+
]
661+
},
662+
{
663+
"cell_type": "markdown",
664+
"id": "5272cc8f",
665+
"metadata": {},
666+
"source": [
667+
"4. `take` and `skip`:\n",
668+
"\n",
669+
"These methods allow you to select or skip a specific number of elements from an iterable:"
670+
]
671+
},
672+
{
673+
"cell_type": "code",
674+
"execution_count": 17,
675+
"id": "e6673e40",
676+
"metadata": {},
677+
"outputs": [
678+
{
679+
"name": "stdout",
680+
"output_type": "stream",
681+
"text": [
682+
"[0, 1, 2, 3, 4]\n"
683+
]
684+
}
685+
],
686+
"source": [
687+
"from pipe import take, skip\n",
688+
"from itertools import count\n",
689+
"\n",
690+
"first_five = list(count() | take(5))\n",
691+
"print(first_five) "
692+
]
693+
},
694+
{
695+
"cell_type": "code",
696+
"execution_count": 18,
697+
"id": "7af10b89",
698+
"metadata": {},
699+
"outputs": [
700+
{
701+
"name": "stdout",
702+
"output_type": "stream",
703+
"text": [
704+
"[3, 4, 5]\n"
705+
]
706+
}
707+
],
708+
"source": [
709+
"skip_first_two = list([1, 2, 3, 4, 5] | skip(2))\n",
710+
"print(skip_first_two) "
711+
]
712+
},
616713
{
617714
"attachments": {},
618715
"cell_type": "markdown",
619716
"id": "3289e1d2",
620717
"metadata": {},
621718
"source": [
622-
"[Link to my article on pipe](https://towardsdatascience.com/write-clean-python-code-using-pipes-1239a0f3abf5).\n",
623-
"\n",
624719
"[Link to pipe](https://github.com/JulienPalard/Pipe)."
625720
]
626721
},

docs/Chapter6/Chapter6.html

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
<link rel="index" title="Index" href="../genindex.html" />
102102
<link rel="search" title="Search" href="../search.html" />
103103
<link rel="next" title="7.1. Alternative Approach" href="alternative_approach.html" />
104-
<link rel="prev" title="6.18. Large Language Model (LLM)" href="../Chapter5/llm.html" />
104+
<link rel="prev" title="6.16. Large Language Model (LLM)" href="../Chapter5/llm.html" />
105105
<meta name="viewport" content="width=device-width, initial-scale=1"/>
106106
<meta name="docsearch:language" content="en"/>
107107
</head>
@@ -263,18 +263,16 @@
263263
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/manage_data.html">6.4. Manage Data</a></li>
264264
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/machine_learning.html">6.5. Machine Learning</a></li>
265265
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/natural_language_processing.html">6.6. Natural Language Processing</a></li>
266-
267-
268-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/time_series.html">6.9. Time Series</a></li>
269-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/sharing_downloading.html">6.10. Sharing and Downloading</a></li>
270-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/speed_up_code.html">6.11. Tools to Speed Up Code</a></li>
271-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/visualization.html">6.12. Visualization</a></li>
272-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/best_python_practice_tools.html">6.13. Tools for Best Python Practices</a></li>
273-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/better_pandas.html">6.14. Better Pandas</a></li>
274-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/testing.html">6.15. Testing</a></li>
275-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/SQL.html">6.16. SQL Libraries</a></li>
276-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/spark.html">6.17. PySpark</a></li>
277-
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/llm.html">6.18. Large Language Model (LLM)</a></li>
266+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/time_series.html">6.7. Time Series</a></li>
267+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/sharing_downloading.html">6.8. Sharing and Downloading</a></li>
268+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/speed_up_code.html">6.9. Tools to Speed Up Code</a></li>
269+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/visualization.html">6.10. Visualization</a></li>
270+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/best_python_practice_tools.html">6.11. Tools for Best Python Practices</a></li>
271+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/better_pandas.html">6.12. Better Pandas</a></li>
272+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/testing.html">6.13. Testing</a></li>
273+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/SQL.html">6.14. SQL Libraries</a></li>
274+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/spark.html">6.15. PySpark</a></li>
275+
<li class="toctree-l2"><a class="reference internal" href="../Chapter5/llm.html">6.16. Large Language Model (LLM)</a></li>
278276
</ul>
279277
</li>
280278
<li class="toctree-l1 current active has-children"><a class="current reference internal" href="#">7. Cool Tools</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-7" name="toctree-checkbox-7" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-7"><i class="fa-solid fa-chevron-down"></i></label><ul>
@@ -529,7 +527,7 @@ <h1><span class="section-number">7. </span>Cool Tools<a class="headerlink" href=
529527
<i class="fa-solid fa-angle-left"></i>
530528
<div class="prev-next-info">
531529
<p class="prev-next-subtitle">previous</p>
532-
<p class="prev-next-title"><span class="section-number">6.18. </span>Large Language Model (LLM)</p>
530+
<p class="prev-next-title"><span class="section-number">6.16. </span>Large Language Model (LLM)</p>
533531
</div>
534532
</a>
535533
<a class="right-next"

0 commit comments

Comments
 (0)