|
478 | 478 | "id": "dba1f40e",
|
479 | 479 | "metadata": {},
|
480 | 480 | "source": [
|
481 |
| - "### Pipe: Use Inflix Notation in Python" |
| 481 | + "### Pipe: A Elegant Alternative to Nested map and filter Calls in Python" |
482 | 482 | ]
|
483 | 483 | },
|
484 | 484 | {
|
|
500 | 500 | ]
|
501 | 501 | },
|
502 | 502 | {
|
503 |
| - "attachments": {}, |
504 | 503 | "cell_type": "markdown",
|
505 |
| - "id": "d23f3a80", |
| 504 | + "id": "568929e4", |
506 | 505 | "metadata": {},
|
507 | 506 | "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:" |
509 | 512 | ]
|
510 | 513 | },
|
511 | 514 | {
|
512 | 515 | "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": {}, |
521 | 519 | "outputs": [
|
522 | 520 | {
|
523 | 521 | "data": {
|
524 | 522 | "text/plain": [
|
525 | 523 | "[4, 16, 36]"
|
526 | 524 | ]
|
527 | 525 | },
|
528 |
| - "execution_count": 16, |
| 526 | + "execution_count": 20, |
529 | 527 | "metadata": {},
|
530 | 528 | "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" |
541 | 529 | }
|
542 | 530 | ],
|
543 | 531 | "source": [
|
544 | 532 | "nums = [1, 2, 3, 4, 5, 6]\n",
|
| 533 | + "\n", |
545 | 534 | "list(\n",
|
546 | 535 | " 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", |
549 | 538 | ")"
|
550 | 539 | ]
|
551 | 540 | },
|
552 | 541 | {
|
553 |
| - "attachments": {}, |
554 | 542 | "cell_type": "markdown",
|
555 |
| - "id": "f2183c8f", |
| 543 | + "id": "2a5fd571", |
556 | 544 | "metadata": {},
|
557 | 545 | "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:" |
574 | 547 | ]
|
575 | 548 | },
|
576 | 549 | {
|
577 | 550 | "cell_type": "code",
|
578 |
| - "execution_count": 15, |
579 |
| - "id": "87225efd", |
| 551 | + "execution_count": 21, |
| 552 | + "id": "e3bcf8c1", |
580 | 553 | "metadata": {
|
581 | 554 | "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" |
584 | 557 | }
|
585 | 558 | },
|
586 | 559 | "outputs": [
|
|
590 | 563 | "[4, 16, 36]"
|
591 | 564 | ]
|
592 | 565 | },
|
593 |
| - "execution_count": 15, |
| 566 | + "execution_count": 21, |
594 | 567 | "metadata": {},
|
595 | 568 | "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" |
606 | 569 | }
|
607 | 570 | ],
|
608 | 571 | "source": [
|
| 572 | + "from pipe import select, where\n", |
| 573 | + "\n", |
609 | 574 | "list(\n",
|
610 | 575 | " nums\n",
|
611 | 576 | " | select(lambda x: x ** 2)\n",
|
612 | 577 | " | where(lambda x: x % 2 == 0)\n",
|
613 | 578 | ")"
|
614 | 579 | ]
|
615 | 580 | },
|
| 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 | + }, |
616 | 713 | {
|
617 | 714 | "attachments": {},
|
618 | 715 | "cell_type": "markdown",
|
619 | 716 | "id": "3289e1d2",
|
620 | 717 | "metadata": {},
|
621 | 718 | "source": [
|
622 |
| - "[Link to my article on pipe](https://towardsdatascience.com/write-clean-python-code-using-pipes-1239a0f3abf5).\n", |
623 |
| - "\n", |
624 | 719 | "[Link to pipe](https://github.com/JulienPalard/Pipe)."
|
625 | 720 | ]
|
626 | 721 | },
|
|
0 commit comments