|
21 | 21 | "source": [
|
22 | 22 | "## Design patterns\n",
|
23 | 23 | "\n",
|
| 24 | + "- great resource on Refactoring, Design Patterns and Clean code - https://refactoring.guru/\n", |
| 25 | + "- design patterns are a formalized way of describing a solution to a problem\n", |
24 | 26 | "- the analogy: engineers and architects designing to build bridges, towers, buildings, and physical structures\n",
|
25 | 27 | " - they follow certain principles to ensure structural integrity\n",
|
26 | 28 | "- design patterns are an attempt to bring this formal definition to software engineering\n",
|
|
663 | 665 | },
|
664 | 666 | {
|
665 | 667 | "cell_type": "code",
|
666 |
| - "execution_count": 33, |
| 668 | + "execution_count": null, |
667 | 669 | "id": "800548c6",
|
668 | 670 | "metadata": {},
|
669 | 671 | "outputs": [
|
|
686 | 688 | }
|
687 | 689 | ],
|
688 | 690 | "source": [
|
| 691 | + "# there's a file in the data directory called system.log\n", |
| 692 | + "# let's read it in using a generator expression\n", |
689 | 693 | "! cat data/system.log"
|
690 | 694 | ]
|
691 | 695 | },
|
|
733 | 737 | },
|
734 | 738 | {
|
735 | 739 | "cell_type": "code",
|
736 |
| - "execution_count": 37, |
| 740 | + "execution_count": null, |
737 | 741 | "id": "05760ea0",
|
738 | 742 | "metadata": {},
|
739 | 743 | "outputs": [],
|
740 | 744 | "source": [
|
741 | 745 | "with full_log_path.open() as source:\n",
|
| 746 | + " # create a generator expression that will yield lines from the log file\n", |
| 747 | + " # that contain the word 'WARN'\n", |
742 | 748 | " power_lines = (line for line in source if 'WARN' in line)\n",
|
743 | 749 | " with warn_log_path.open('w') as target:\n",
|
| 750 | + " # iterate over the lines to write each line to the new file\n", |
744 | 751 | " for line in power_lines:\n",
|
745 | 752 | " target.write(line)"
|
746 | 753 | ]
|
|
775 | 782 | "\n",
|
776 | 783 | "- generator functions embody the essential features of a generator expression\n",
|
777 | 784 | " - which is the generalization of comprehension\n",
|
778 |
| - "- use regular expression to parse lines into different columns of values\n", |
779 | 785 | "- special functions that return an iterator which returns a stream of values\n",
|
780 | 786 | "- resumable functions that can retain local variable information to resume the function where it left off\n",
|
781 | 787 | "- uses `yield` keyword to yield the next value as opposed to `return`\n",
|
|
0 commit comments