From be7599e2e43c13d6026f60598a7dd69cd8b46c78 Mon Sep 17 00:00:00 2001 From: Ty Date: Wed, 3 Oct 2018 20:58:19 -0700 Subject: [PATCH 01/10] checking in work --- python_tips_2.ipynb | 282 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 222 insertions(+), 60 deletions(-) diff --git a/python_tips_2.ipynb b/python_tips_2.ipynb index 2460f1e..c8f8d1c 100644 --- a/python_tips_2.ipynb +++ b/python_tips_2.ipynb @@ -116,7 +116,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -231,47 +231,175 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 6, 10, 15]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "'''accumulate()'''" + "'''\n", + "accumulate()\n", + "\n", + "Accumulate makes an iterator that returns accumulated sums\n", + "Works similar to itertools.reduce() although only with addition\n", + "'''\n", + "\n", + "list(accumulate([1,2,3,4,5]))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'b', 'c', 'e', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l']\n" + ] + } + ], "source": [ - "'''chain() and chain.from_iterable'''" + "'''\n", + "chain() and chain.from_iterable()\n", + "\n", + "Chain makes an iterator that returns elements from the first iterable until it is exhausted,\n", + "then proceeds to the next iterable, until all of the iterables are exhausted.\n", + "Used for treating consecutive sequences as a single sequence.\n", + "\n", + "You can chain together lists, tuples, sets and strings\n", + "'''\n", + "\n", + "print(list(chain(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl'))) " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "from_iterable() takes exactly one argument (4 given)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m ''' \n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchain\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_iterable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'b'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'd'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'e'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'f'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'g'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'h'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'i'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'jkl'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: from_iterable() takes exactly one argument (4 given)" + ] + } + ], "source": [ - "'''compress()'''" + "'''\n", + "chain.from_interable()\n", + "\n", + "Alternate constructor for chain(). Gets chained inputs from a single iterable argument that is evaluated lazily.\n", + "''' \n", + "\n", + "print(list(chain.from_iterable(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl')))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'b', 'c', 'e', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l']\n" + ] + } + ], "source": [ - "'''dropwhile()'''" + "print(list(chain.from_iterable([('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl']))) # Now as a single argument" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['A', 'C', 'E', 'F']" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "'''filterflase()'''" + "'''\n", + "compress()\n", + "\n", + "'''\n", + "\n", + "list(compress('ABCDEF', [1,0,1,0,1,1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 4, 1]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'''\n", + "dropwhile()\n", + "\n", + "'''\n", + "\n", + "list(dropwhile(lambda x: x<5, [1,4,6,4,1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'''\n", + "filterflase()\n", + "\n", + "'''\n", + "\n", + "list(filterfalse(lambda x: x%2, range(10)))" ] }, { @@ -280,7 +408,10 @@ "metadata": {}, "outputs": [], "source": [ - "'''groupby()'''" + "'''\n", + "groupby()\n", + "\n", + "'''" ] }, { @@ -289,7 +420,10 @@ "metadata": {}, "outputs": [], "source": [ - "'''islice()'''" + "'''\n", + "islice()\n", + "\n", + "'''" ] }, { @@ -298,7 +432,10 @@ "metadata": {}, "outputs": [], "source": [ - "'''starmap()'''" + "'''\n", + "starmap()\n", + "\n", + "'''" ] }, { @@ -307,7 +444,10 @@ "metadata": {}, "outputs": [], "source": [ - "'''tee()'''" + "'''\n", + "tee()\n", + "\n", + "'''" ] }, { @@ -316,7 +456,10 @@ "metadata": {}, "outputs": [], "source": [ - "'''zip_longest()'''" + "'''\n", + "zip_longest()\n", + "\n", + "'''" ] }, { @@ -350,16 +493,19 @@ } ], "source": [ - "# Product ()\n", + "'''\n", + "Product ()\n", "\n", - "#is equivalent for-loops. Repeat lets you choose how many times to nest.\n", + "Product is equivalent to for loops. Repeat lets you choose how many times to nest.\n", + "'''\n", "\n", + "# Single for loop\n", "list(product('ABCD', repeat=1))" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -368,12 +514,13 @@ "[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]" ] }, - "execution_count": 23, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "# Nested for-loop\n", "list(combinations('ABCD', 2))" ] }, @@ -383,7 +530,10 @@ "metadata": {}, "outputs": [], "source": [ - "combinations_with_replacement()" + "'''\n", + "combinations_with_replacement()\n", + "\n", + "'''" ] }, { @@ -392,7 +542,10 @@ "metadata": {}, "outputs": [], "source": [ - "count()" + "'''\n", + "count()\n", + "\n", + "'''" ] }, { @@ -401,7 +554,10 @@ "metadata": {}, "outputs": [], "source": [ - "accumulate()" + "'''\n", + "accumulate()\n", + "\n", + "'''" ] }, { @@ -416,6 +572,15 @@ "https://docs.python.org/3/library/collections.html" ] }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from collections import *" + ] + }, { "cell_type": "code", "execution_count": null, @@ -450,33 +615,38 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 5, "metadata": {}, + "outputs": [], "source": [ - "Partial makes a new version of a function with one or more arguments already filled in. Used for quick access." + "from functools import *" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "from functools import partial\n", - "# Good resource https://www.pydanny.com/python-partials-are-fun.html\n", + "'''\n", + "Partial makes a new version of a function with one or more arguments already filled in. Used for quick access.\n", "\n", + "Good resource https://www.pydanny.com/python-partials-are-fun.html\n", + "'''\n", + "\n", + "# Initial function\n", "def student(first,last,grade):\n", " print(first,last,grade)\n", "\n", + "# Partial function\n", "freshman = partial(student, grade=10)" ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "scrolled": true - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -490,16 +660,9 @@ "freshman('Joe','Smith')" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Reduce is a really useful function for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list. This one is tricky." - ] - }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -508,16 +671,22 @@ "3628800" ] }, - "execution_count": 20, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# Reduce needs to be imported in Python3.x\n", - "from functools import reduce\n", + "'''\n", + "reduce()\n", + "\n", + "Reduce needs to be imported in Python3.x\n", + "\n", + "Reduce is a really useful function for performing some computation on a list and returning the result.\n", + "It applies a rolling computation to sequential pairs of values in a list. This one is tricky.\n", "\n", - "# Easiest example to understnad is trying to multiply a whole list together i.e 1*2*3*4*5*6*7*8*9*10\n", + "Easiest example to understnad is trying to multiply a whole list together i.e 1*2*3*4*5*6*7*8*9*10\n", + "'''\n", "\n", "list_1 = list(range(1,11))\n", "\n", @@ -526,7 +695,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -535,7 +704,7 @@ "10" ] }, - "execution_count": 21, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -548,7 +717,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 12, "metadata": { "scrolled": true }, @@ -559,7 +728,7 @@ "1" ] }, - "execution_count": 22, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -568,13 +737,6 @@ "reduce(lambda x, y: y if y < x else x, list_1) # Finding the smallest number in the list" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": {}, From c8c9bf6648e4803d9989672640e3a26ce7b182d2 Mon Sep 17 00:00:00 2001 From: Ty Date: Sun, 7 Oct 2018 11:38:01 -0700 Subject: [PATCH 02/10] checking in work --- python_tips_1.ipynb | 5 +- python_tips_2.ipynb | 191 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 158 insertions(+), 38 deletions(-) diff --git a/python_tips_1.ipynb b/python_tips_1.ipynb index bf764d3..1a37ddf 100644 --- a/python_tips_1.ipynb +++ b/python_tips_1.ipynb @@ -636,7 +636,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 24, @@ -982,8 +982,7 @@ "# These are use in documentation, specifically within in functions or class methods\n", "\n", "def documentation(x, y):\n", - " '''\n", - " This function takes two parameters\n", + " '''This function takes two parameters\n", " It will then sum x and y\n", " '''\n", " return(x+y)" diff --git a/python_tips_2.ipynb b/python_tips_2.ipynb index c8f8d1c..e2050d4 100644 --- a/python_tips_2.ipynb +++ b/python_tips_2.ipynb @@ -116,7 +116,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -128,12 +128,12 @@ "metadata": {}, "source": [ "Infinite itorators
\n", - "Please learn about generators first. Do not use count() and cycle() to create a list." + "Please learn about generators first." ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 29, "metadata": {}, "outputs": [ { @@ -142,25 +142,24 @@ "text": [ "0\n", "5\n", - "10\n", - "15\n" + "10\n" ] } ], "source": [ - "'''\n", - "count()\n", + "'''count()\n", "\n", "This creates an iterable object that goes up by the step you specify.\n", "This will continuously yield increments of 5 i.e. 0, 5, 10, 15, 20...\n", + "\n", + "Do not use as a list.\n", "'''\n", "\n", "count_object = count(0, 5) # (start, step)\n", "\n", "print(next(count_object))\n", "print(next(count_object))\n", - "print(next(count_object))\n", - "print(next(count_object))" + "print(next(count_object))\n" ] }, { @@ -181,11 +180,12 @@ } ], "source": [ - "'''\n", - "cycle()\n", + "'''cycle()\n", "\n", "This creates an iterable object of what you pass in in an endless cycle.\n", "This will continuously yield, A B C D A B C D A B C D ...\n", + "\n", + "Do not use as a list.\n", "''' \n", "\n", "cycle_object = cycle('ABCD')\n", @@ -212,10 +212,11 @@ } ], "source": [ - "'''\n", - "repeat()\n", + "'''repeat()\n", "\n", "Used to repeat an element up to n times.\n", + "\n", + "Does not work as intended as a generator\n", "'''\n", "\n", "print(list(repeat(10, 3)))\n", @@ -246,16 +247,37 @@ } ], "source": [ - "'''\n", - "accumulate()\n", + "'''accumulate()\n", "\n", "Accumulate makes an iterator that returns accumulated sums\n", "Works similar to itertools.reduce() although only with addition\n", + "\n", + "Works as a generator.\n", + "\n", "'''\n", "\n", "list(accumulate([1,2,3,4,5]))" ] }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "accu_onj = accumulate([1,2,3,4,5])\n", + "\n", + "print(next(accu_onj))" + ] + }, { "cell_type": "code", "execution_count": 25, @@ -270,19 +292,39 @@ } ], "source": [ - "'''\n", - "chain() and chain.from_iterable()\n", + "'''chain() and chain.from_iterable()\n", "\n", "Chain makes an iterator that returns elements from the first iterable until it is exhausted,\n", "then proceeds to the next iterable, until all of the iterables are exhausted.\n", "Used for treating consecutive sequences as a single sequence.\n", "\n", "You can chain together lists, tuples, sets and strings\n", + "\n", + "Works as a generator\n", "'''\n", "\n", "print(list(chain(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl'))) " ] }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n" + ] + } + ], + "source": [ + "chain_obj = chain(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl')\n", + "\n", + "print(next(chain_obj))" + ] + }, { "cell_type": "code", "execution_count": 32, @@ -301,8 +343,7 @@ } ], "source": [ - "'''\n", - "chain.from_interable()\n", + "'''chain.from_interable()\n", "\n", "Alternate constructor for chain(). Gets chained inputs from a single iterable argument that is evaluated lazily.\n", "''' \n", @@ -344,9 +385,13 @@ } ], "source": [ - "'''\n", - "compress()\n", + "'''compress()\n", + "\n", + "Make an iterator that filters elements from data returning\n", + "only those that have a corresponding element in selectors that\n", + "evaluates to True. Stops when either the data or selectors iterables has been exhausted\n", "\n", + "Works as a generator.\n", "'''\n", "\n", "list(compress('ABCDEF', [1,0,1,0,1,1]))" @@ -354,32 +399,81 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A\n" + ] + } + ], + "source": [ + "compress_obj = compress('ABCDEF', [1,0,1,0,1,1])\n", + "\n", + "print(next(compress_obj))" + ] + }, + { + "cell_type": "code", + "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[6, 4, 1]" + "[5, 6, 7, 8, 9]" ] }, - "execution_count": 41, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "'''\n", - "dropwhile()\n", + "'''dropwhile()\n", + "\n", + "Makes an iterator that drops elements from the iterable\n", + "as long as the predicate is true; afterwards, returns every element.\n", + "\n", + "Works as a generator.\n", "\n", + "1 < 5 True won't return\n", + "2 < 5 True won't return\n", + "3 < 5 True won't return\n", + "4 < 5 True won't return\n", + "5 !< 5 False will return \n", + "\n", + "Now will return 5 and everything after.\n", "'''\n", "\n", - "list(dropwhile(lambda x: x<5, [1,4,6,4,1]))" + "list(dropwhile(lambda x: x<5, [1,2,3,4,5,6,7,8,9]))" ] }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "dropwhile_obj = dropwhile(lambda x: x<5, [1,2,3,4,5,6,7,8,9])\n", + "\n", + "print(next(dropwhile_obj))" + ] + }, + { + "cell_type": "code", + "execution_count": 61, "metadata": {}, "outputs": [ { @@ -388,20 +482,48 @@ "[0, 2, 4, 6, 8]" ] }, - "execution_count": 44, + "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "'''\n", - "filterflase()\n", + "'''filterflase()\n", + "\n", + "Make an iterator that filters elements from iterable \n", + "returning only those for which the predicate is False (0)\n", "\n", + "Works as a generator.\n", + "\n", + "0 % 2 = 0 False will return\n", + "1 % 2 = 1 True won't return\n", + "2 % 2 = 0 False will return\n", + "3 % 2 = 1 True won't return\n", + "etc...\n", "'''\n", "\n", "list(filterfalse(lambda x: x%2, range(10)))" ] }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "filterfalse_obj = filterfalse(lambda x: x%2, range(10))\n", + "\n", + "print(next(filterfalse_obj))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -793,7 +915,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -803,7 +925,6 @@ " def __init__(self, name, job):\n", " self.name = name\n", " self.job = job\n", - "\n", " \n", "# This class inherits name and job from the employee class using super().__init__\n", "\n", @@ -815,7 +936,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -824,7 +945,7 @@ "'Todd'" ] }, - "execution_count": 42, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } From a799ca4aa2bfe2a0bb19abcf5bcc0a429b6e0f55 Mon Sep 17 00:00:00 2001 From: Ty Date: Sun, 7 Oct 2018 12:14:14 -0700 Subject: [PATCH 03/10] checking in again --- README.rst | 2 + python_tips_2.ipynb | 234 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 215 insertions(+), 21 deletions(-) diff --git a/README.rst b/README.rst index 7adfbc2..a856c30 100644 --- a/README.rst +++ b/README.rst @@ -10,6 +10,8 @@ These files are .ipynb. It is a notebook document used by Jupyter Notebook, an i I suggest downloading `Anaconda `_. Anaconda is a free and open source distribution of the Python and R programming languages that aims to simplify package management and deployment. Jupyter Notebook is included in Anaconda. +I will most likey seperate out functools, iterools and collection into their own .ipynb files. Let me know how I should organize these. + Tips 1 topics: - Lambda functions - Enumerate diff --git a/python_tips_2.ipynb b/python_tips_2.ipynb index e2050d4..3b39803 100644 --- a/python_tips_2.ipynb +++ b/python_tips_2.ipynb @@ -9,6 +9,8 @@ "I may not cover everything in each library.
\n", "Please go through official documentation if you want more thorough examples.\n", "\n", + "All of the functions are operating under their default parameters.\n", + "\n", "Topics:\n", " - [Generators](#generators)\n", " - [Itertools](#itertools)\n", @@ -253,7 +255,6 @@ "Works similar to itertools.reduce() although only with addition\n", "\n", "Works as a generator.\n", - "\n", "'''\n", "\n", "list(accumulate([1,2,3,4,5]))" @@ -387,7 +388,7 @@ "source": [ "'''compress()\n", "\n", - "Make an iterator that filters elements from data returning\n", + "Compress makes an iterator that filters elements from data returning\n", "only those that have a corresponding element in selectors that\n", "evaluates to True. Stops when either the data or selectors iterables has been exhausted\n", "\n", @@ -435,7 +436,7 @@ "source": [ "'''dropwhile()\n", "\n", - "Makes an iterator that drops elements from the iterable\n", + "Dropwhile makes an iterator that drops elements from the iterable\n", "as long as the predicate is true; afterwards, returns every element.\n", "\n", "Works as a generator.\n", @@ -490,7 +491,7 @@ "source": [ "'''filterflase()\n", "\n", - "Make an iterator that filters elements from iterable \n", + "Makes an iterator that filters elements from iterable \n", "returning only those for which the predicate is False (0)\n", "\n", "Works as a generator.\n", @@ -526,26 +527,152 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['A', 'B', 'C', 'D', 'A', 'B']" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "'''groupby()\n", + "\n", + "Makes an iterator that returns consecutive keys and groups from the iterable.\n", + "\n", + "The operation of groupby() is similar to the uniq filter in Unix.\n", + "It generates a break or new group every time the value of the key function changes.\n", + "\n", + "Generally, the iterable needs to already be sorted on the same key function.\n", "'''\n", - "groupby()\n", "\n", - "'''" + "[k for k, g in groupby('AAAABBBCCDAABBB')]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[['A', 'A', 'A', 'A'],\n", + " ['B', 'B', 'B'],\n", + " ['C', 'C'],\n", + " ['D'],\n", + " ['A', 'A'],\n", + " ['B', 'B', 'B']]" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "# Using groupby like this will return the groups\n", + "\n", + "[list(g) for k, g in groupby('AAAABBBCCDAABBB')]" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['A', 'B']\n", + "['A', 'B']\n" + ] + } + ], + "source": [ + "'''islice()\n", + "\n", + "Very similar to calling the index in a string\n", "'''\n", - "islice()\n", "\n", - "'''" + "print(list(islice('ABCDEFG', 2)))\n", + "\n", + "# similar to\n", + "\n", + "print(list('ABCDEFG'[0:2]))" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['C', 'D']\n", + "['C', 'D']\n" + ] + } + ], + "source": [ + "print(list(islice('ABCDEFG', 2, 4)))\n", + "\n", + "# similar to\n", + "\n", + "print(list('ABCDEFG'[2:4]))" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['C', 'D', 'E', 'F', 'G']\n", + "['C', 'D', 'E', 'F', 'G']\n" + ] + } + ], + "source": [ + "print(list(islice('ABCDEFG', 2, None)))\n", + "\n", + "# similar to\n", + "\n", + "print(list('ABCDEFG'[2:]))" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['A', 'C', 'E', 'G']\n", + "['A', 'C', 'E', 'G']\n" + ] + } + ], + "source": [ + "print(list(islice('ABCDEFG', 0, None, 2)))\n", + "\n", + "# similar to\n", + "\n", + "print(list('ABCDEFG'[::2]))" ] }, { @@ -554,8 +681,7 @@ "metadata": {}, "outputs": [], "source": [ - "'''\n", - "starmap()\n", + "'''starmap()\n", "\n", "'''" ] @@ -566,8 +692,7 @@ "metadata": {}, "outputs": [], "source": [ - "'''\n", - "tee()\n", + "'''tee()\n", "\n", "'''" ] @@ -578,8 +703,7 @@ "metadata": {}, "outputs": [], "source": [ - "'''\n", - "zip_longest()\n", + "'''zip_longest()\n", "\n", "'''" ] @@ -708,21 +832,89 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "'''namedtuple()\n", + "'''" + ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "'''deque'''" + ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "'''ChainMap\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''Counter\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''OrderedDict\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''defaultdict\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''UserDict\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''UserList\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''UserString\n", + "'''" + ] }, { "cell_type": "markdown", From b00c9cf2f125eb4a11f31f3ea1db0ea711cd3692 Mon Sep 17 00:00:00 2001 From: Ty Date: Sun, 7 Oct 2018 12:26:17 -0700 Subject: [PATCH 04/10] Small readme fix --- README.rst | 2 +- python_tips_2.ipynb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index a856c30..fd6e04d 100644 --- a/README.rst +++ b/README.rst @@ -5,7 +5,7 @@ Python Tips Please clone or fork, the notebooks are meant to be interacted with. -------------------------------------------------------------------- -These files are .ipynb. It is a notebook document used by Jupyter Notebook, an interactive computational environment designed to help scientists work with the Python language. +These files are .ipynb. It is a notebook document used by Jupyter Notebook, an interactive computational environment designed to help scientists work with the Python language (as well as Julia and R). I suggest downloading `Anaconda `_. Anaconda is a free and open source distribution of the Python and R programming languages that aims to simplify package management and deployment. Jupyter Notebook is included in Anaconda. diff --git a/python_tips_2.ipynb b/python_tips_2.ipynb index 3b39803..ca4f0c5 100644 --- a/python_tips_2.ipynb +++ b/python_tips_2.ipynb @@ -109,6 +109,8 @@ "# Itertools \n", "[Return to table of contents](#toc)\n", "\n", + "Please learn about generators first.\n", + "\n", "This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python.\n", "\n", "The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.\n", @@ -129,8 +131,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Infinite itorators
\n", - "Please learn about generators first." + "Infinite itorators
" ] }, { From 8b3cd94b895c1e943446f0f739ae9babfeba9816 Mon Sep 17 00:00:00 2001 From: Tyler Guo Date: Mon, 8 Oct 2018 14:49:26 -0700 Subject: [PATCH 05/10] Renamed files and checking in work --- README.rst | 19 +- ...ips_2.ipynb => built_in_library_tips.ipynb | 229 +++++++++--------- python_tips_1.ipynb => python_tips.ipynb | 108 ++++++++- 3 files changed, 221 insertions(+), 135 deletions(-) rename python_tips_2.ipynb => built_in_library_tips.ipynb (88%) rename python_tips_1.ipynb => python_tips.ipynb (93%) diff --git a/README.rst b/README.rst index fd6e04d..e98a311 100644 --- a/README.rst +++ b/README.rst @@ -5,14 +5,14 @@ Python Tips Please clone or fork, the notebooks are meant to be interacted with. -------------------------------------------------------------------- +Note that these were written in Python 3 (3.6) + These files are .ipynb. It is a notebook document used by Jupyter Notebook, an interactive computational environment designed to help scientists work with the Python language (as well as Julia and R). I suggest downloading `Anaconda `_. Anaconda is a free and open source distribution of the Python and R programming languages that aims to simplify package management and deployment. Jupyter Notebook is included in Anaconda. -I will most likey seperate out functools, iterools and collection into their own .ipynb files. Let me know how I should organize these. - -Tips 1 topics: +Python Tips topics: - Lambda functions - Enumerate - List, set and dict comprehension @@ -21,13 +21,18 @@ Tips 1 topics: - Zip - Print formatting - Args and kwargs + - Class Inheritance - Mutable parameters -Tips 2 topics (WIP): +Built-In Libraries Tips topics: + - Generators - Itertools (built-in library) + - Decorators - Functools (built-in library) reduce, partial.. etc. - Collections (built-in library) - - Decorators - - Class Inheritance. - - Generators - Datetime (built-in library) + + Built-In Functions Tips 1(WIP): + - Built-in Functions + + diff --git a/python_tips_2.ipynb b/built_in_library_tips.ipynb similarity index 88% rename from python_tips_2.ipynb rename to built_in_library_tips.ipynb index ca4f0c5..f6bd0c9 100644 --- a/python_tips_2.ipynb +++ b/built_in_library_tips.ipynb @@ -6,10 +6,12 @@ "source": [ "# TABLE OF CONTENTS: \n", "\n", + "These tips focus largely on built-in libraries that I think are good to know.\n", + "\n", "I may not cover everything in each library.
\n", "Please go through official documentation if you want more thorough examples.\n", "\n", - "All of the functions are operating under their default parameters.\n", + "All functions are operating under their default parameters.\n", "\n", "Topics:\n", " - [Generators](#generators)\n", @@ -19,9 +21,8 @@ " - [Combinatoric iterators](#combinatoric)\n", " - [Collections](#collections)\n", " - [Functools](#functools)\n", - " - [Class Inheritence](#class)\n", " - [Datetime](#dt)\n", - " - [Timedelta](#timedelta)" + " - [Calendar](#calendar)" ] }, { @@ -120,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -676,13 +677,42 @@ "print(list('ABCDEFG'[::2]))" ] }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[32, 9, 1000]\n", + "[32, 9, 1000]\n" + ] + } + ], + "source": [ + "'''starmap()\n", + "\n", + "Makes an iterator that computes the function\n", + "using arguments obtained from the iterable.\n", + "\n", + "'''\n", + "print(list(starmap(pow, [(2,5), (3,2), (10,3)])))\n", + "\n", + "# similar to\n", + "\n", + "starmap_equivalent = [2**5, 3**2, 10**3]\n", + "print(starmap_equivalent)" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "'''starmap()\n", + "'''takewhile()\n", "\n", "'''" ] @@ -1084,90 +1114,21 @@ { "cell_type": "markdown", "metadata": {}, - "source": [ - "# Class inheretance\n", - "[Return to table of contents](#toc)\n", - "\n", - "Simple overview of class inheretance. If you want to inherit instance variables from another clsss following this syntax:" - ] + "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# syntax\n", - "\n", - "'''\n", - "childclass(parentcall):\n", - " def __init__(self, parent_var, new_var):\n", - " super().__init__(parent_var)\n", - "'''" - ] + "source": [] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], - "source": [ - "# Class\n", - "\n", - "class employee():\n", - " def __init__(self, name, job):\n", - " self.name = name\n", - " self.job = job\n", - " \n", - "# This class inherits name and job from the employee class using super().__init__\n", - "\n", - "class manager(employee):\n", - " def __init__(self, name, job, level):\n", - " super().__init__(name, job)\n", - " self.level = level" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Todd'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Todd = employee('Todd','Developer')\n", - "Todd.name" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Manager'" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sharon = manager('Sharon', 'Senior Developer', 'Manager')\n", - "Sharon.level" - ] + "source": [] }, { "cell_type": "markdown", @@ -1183,129 +1144,165 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "from datetime import *" + ] + }, + { + "cell_type": "code", + "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "datetime.datetime(2018, 9, 28, 9, 54, 20, 815758)" + "datetime.datetime(2018, 10, 8, 14, 41, 12, 607021)" ] }, - "execution_count": 2, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "from datetime import datetime\n", + "'''datetime()\n", "\n", - "# A datetime object will return following attributes: year, month, day, hour, minute, seconds, micro-seconds.\n", + "Display the current time and date of your machine.\n", + "A datetime object will return following attributes:\n", + "year, month, day, hour, minute, seconds, micro-seconds.\n", + "'''\n", "\n", - "# Display the current time and date of your machine.\n", "datetime.now()" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Today is the 28 day of month 9, and the year is 2018!\n" + "Today is the 8 day of month 10, and the year is 2018!\n" ] } ], "source": [ - "now_datetime_object = datetime.now()\n", + "now = datetime.now()\n", "\n", "# We can also extract only certain data as desired.\n", "\n", - "now_year = now_datetime_object.year\n", - "now_month = now_datetime_object.month\n", - "now_day = now_datetime_object.day\n", + "now_year = now.year\n", + "now_month = now.month\n", + "now_day = now.day\n", "\n", "print('Today is the {} day of month {}, and the year is {}!'.format(now_day, now_month, now_year))" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The current month is September.\n" + "The current month is October.\n" ] } ], "source": [ - "# strftime() formats datetime objects into readable strings\n", - "# The %B is how strftime knows to return the string of the current month in full.\n", - "# Useful cheatsheet for how to use strftime: https://devhints.io/strftime.\n", + "'''strftime() \n", + "\n", + "Strftime formats datetime objects into readable strings\n", + "\n", + "The %B is how strftime knows to return the \n", + "string of the current month in full.\n", + "\n", + "Useful cheatsheet for how to use strftime:\n", + "https://devhints.io/strftime.\n", + "'''\n", "\n", "print('The current month is {}.'.format(now_datetime_object.strftime('%B')))" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Today's date is 28-09-2018, and be careful; now it is not a datetimeobject!\n", - " it is actually !\n" + "Today's date is 08-10-2018. Be careful;\n", + "It is not a datetimeobject anymore it is a !\n" ] } ], "source": [ "# strftime() can generally be used to format datetime objects.\n", "\n", - "pretty_format = now_datetime_object.strftime('%d-%m-%Y')\n", - "print(\"Today's date is {}, and be careful; now it is not a datetimeobject!\\n \\\n", - " it is actually {}!\".format(pretty_format, type(pretty_format)))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Timedelta" + "formatted = now_datetime_object.strftime('%d-%m-%Y')\n", + "print('''Today's date is {}. Be careful;\n", + "It's not a datetimeobject anymore it is a {}!'''\n", + " .format(formatted, type(formatted)))" ] }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, + "execution_count": 43, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "I started studying in university back in 21-10-2015, and I'll finish in 17-10-2018\n" + "I started studying in university back in 21-10-2015,and I'll finish in 17-10-2018\n" ] } ], "source": [ - "# To get a date X days\\months\\etc from now, we can use timedelta().\n", + "'''timedelta()\n", + "Used to get a date X days\\months\\etc from now.\n", "\n", + "'''\n", "from datetime import timedelta\n", "\n", - "# School lasts about three years, so we calculate the time difference between starting university time and three years \n", - "# (or 3 * 52 weeks) from this time\n", + "''' School lasts about three years, \n", + "so we calculate the time difference between\n", + "starting university time and three years \n", + "(or 3 * 52 weeks) from this time'''\n", "\n", "start_uni_time = datetime(2015, 10, 21)\n", "end_uni_time = start_uni_time + timedelta(weeks=52 * 3)\n", - "print(\"I started studying in university back in {}, and I'll finish in {}\".format(\n", - " start_uni_time.strftime('%d-%m-%Y'), end_uni_time.strftime('%d-%m-%Y')))" + "\n", + "print('''I started studying in university back in {},and I'll finish in {}'''\n", + " .format(start_uni_time.strftime('%d-%m-%Y'), end_uni_time.strftime('%d-%m-%Y')))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Calendar \n", + "[Return to table of contents](#toc)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "import calendar" ] } ], @@ -1325,7 +1322,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/python_tips_1.ipynb b/python_tips.ipynb similarity index 93% rename from python_tips_1.ipynb rename to python_tips.ipynb index 1a37ddf..8aa2143 100644 --- a/python_tips_1.ipynb +++ b/python_tips.ipynb @@ -6,6 +6,8 @@ "source": [ "# TABLE OF CONTENTS:\n", "\n", + "These tips are focused on topics I think every Python programmer should know.\n", + "\n", "Please go through official documentation if you want more thorough examples.\n", "\n", "Topics:\n", @@ -25,6 +27,7 @@ " - [Args](#args)\n", " - [Kwargs](#kwargs)\n", " - [Using Them Together](#together)\n", + " - [Class Inheritence](#class)\n", " - [Mutable parameters](#mparams)" ] }, @@ -636,7 +639,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 24, @@ -1231,6 +1234,87 @@ "multiple('a','b',1,2,3,4,5,key1='item1',key2='item2')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Class inheretance\n", + "[Return to table of contents](#toc)\n", + "\n", + "Simple overview of class inheretance. If you want to inherit instance variables from another clsss following this syntax:" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "# syntax\n", + "\n", + "'''\n", + "childclass(parentcall):\n", + " def __init__(self, parent_var, new_var):\n", + " super().__init__(parent_var)\n", + "'''\n", + "\n", + "# Class\n", + "\n", + "class employee():\n", + " def __init__(self, name, job):\n", + " self.name = name\n", + " self.job = job\n", + " \n", + "# This class inherits name and job from the employee class using super().__init__\n", + "\n", + "class manager(employee):\n", + " def __init__(self, name, job, level):\n", + " super().__init__(name, job)\n", + " self.level = level" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Todd'" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Todd = employee('Todd','Developer')\n", + "Todd.name" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Manager'" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sharon = manager('Sharon', 'Senior Developer', 'Manager')\n", + "Sharon.level" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1241,7 +1325,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ @@ -1255,7 +1339,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 55, "metadata": { "scrolled": true }, @@ -1266,7 +1350,7 @@ "[5]" ] }, - "execution_count": 52, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } @@ -1277,7 +1361,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 56, "metadata": {}, "outputs": [ { @@ -1286,7 +1370,7 @@ "[5, 7]" ] }, - "execution_count": 53, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -1297,7 +1381,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ @@ -1312,7 +1396,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 58, "metadata": {}, "outputs": [ { @@ -1321,7 +1405,7 @@ "[5]" ] }, - "execution_count": 55, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } @@ -1332,7 +1416,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 59, "metadata": {}, "outputs": [ { @@ -1341,7 +1425,7 @@ "[7]" ] }, - "execution_count": 56, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } @@ -1367,7 +1451,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.6.6" } }, "nbformat": 4, From 66276f2471bd731b19927352c4e96dc9b99d5f35 Mon Sep 17 00:00:00 2001 From: Tyler Guo Date: Mon, 8 Oct 2018 16:49:53 -0700 Subject: [PATCH 06/10] Another work checkin --- built_in_library_tips.ipynb | 576 +++++++++++++++++++++++++++++++++--- 1 file changed, 538 insertions(+), 38 deletions(-) diff --git a/built_in_library_tips.ipynb b/built_in_library_tips.ipynb index f6bd0c9..d0f6c18 100644 --- a/built_in_library_tips.ipynb +++ b/built_in_library_tips.ipynb @@ -135,9 +135,16 @@ "Infinite itorators
" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Count" + ] + }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 108, "metadata": {}, "outputs": [ { @@ -152,6 +159,7 @@ ], "source": [ "'''count()\n", + "Arguments/Defaults: count(start=0, step=1)\n", "\n", "This creates an iterable object that goes up by the step you specify.\n", "This will continuously yield increments of 5 i.e. 0, 5, 10, 15, 20...\n", @@ -166,9 +174,16 @@ "print(next(count_object))\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Cycle" + ] + }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 109, "metadata": {}, "outputs": [ { @@ -185,6 +200,7 @@ ], "source": [ "'''cycle()\n", + "Arguments/Defaults: cycle(iterable)\n", "\n", "This creates an iterable object of what you pass in in an endless cycle.\n", "This will continuously yield, A B C D A B C D A B C D ...\n", @@ -201,6 +217,13 @@ "print(next(cycle_object))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Repeat" + ] + }, { "cell_type": "code", "execution_count": 19, @@ -217,6 +240,7 @@ ], "source": [ "'''repeat()\n", + "Arguments/Defaults: repeat(object, times=None)\n", "\n", "Used to repeat an element up to n times.\n", "\n", @@ -234,6 +258,13 @@ "Iterators terminating on the shortest input sequence" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Accumulate" + ] + }, { "cell_type": "code", "execution_count": 3, @@ -252,6 +283,7 @@ ], "source": [ "'''accumulate()\n", + "Arguments/Defaults: accumulate(iterable, func=operator.add)\n", "\n", "Accumulate makes an iterator that returns accumulated sums\n", "Works similar to itertools.reduce() although only with addition\n", @@ -281,6 +313,13 @@ "print(next(accu_onj))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Chain" + ] + }, { "cell_type": "code", "execution_count": 25, @@ -295,7 +334,8 @@ } ], "source": [ - "'''chain() and chain.from_iterable()\n", + "'''chain()\n", + "Arguments/Defaults: chain(*iterables)\n", "\n", "Chain makes an iterator that returns elements from the first iterable until it is exhausted,\n", "then proceeds to the next iterable, until all of the iterables are exhausted.\n", @@ -347,8 +387,12 @@ ], "source": [ "'''chain.from_interable()\n", + "Arguments/Defaults: from_iterable(iterables)\n", "\n", - "Alternate constructor for chain(). Gets chained inputs from a single iterable argument that is evaluated lazily.\n", + "Alternate constructor for chain(). Gets chained inputs from a single \n", + "iterable argument that is evaluated.\n", + "\n", + "Works as a generator\n", "''' \n", "\n", "print(list(chain.from_iterable(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl')))" @@ -371,6 +415,13 @@ "print(list(chain.from_iterable([('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl']))) # Now as a single argument" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compress" + ] + }, { "cell_type": "code", "execution_count": 38, @@ -389,6 +440,7 @@ ], "source": [ "'''compress()\n", + "Arguments/Defaults: compress(data, selectors)\n", "\n", "Compress makes an iterator that filters elements from data returning\n", "only those that have a corresponding element in selectors that\n", @@ -419,6 +471,13 @@ "print(next(compress_obj))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Dropwhile" + ] + }, { "cell_type": "code", "execution_count": 59, @@ -437,6 +496,7 @@ ], "source": [ "'''dropwhile()\n", + "Arguments/Defaults: dropwhile(predicate, iterable)\n", "\n", "Dropwhile makes an iterator that drops elements from the iterable\n", "as long as the predicate is true; afterwards, returns every element.\n", @@ -474,6 +534,13 @@ "print(next(dropwhile_obj))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Filterfalse" + ] + }, { "cell_type": "code", "execution_count": 61, @@ -492,8 +559,9 @@ ], "source": [ "'''filterflase()\n", + "Arguments/Defaults: filterfalse(predicate, iterable)\n", "\n", - "Makes an iterator that filters elements from iterable \n", + "Makes an iterator that filters elements from an iterable \n", "returning only those for which the predicate is False (0)\n", "\n", "Works as a generator.\n", @@ -527,6 +595,13 @@ "print(next(filterfalse_obj))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Groupby" + ] + }, { "cell_type": "code", "execution_count": 66, @@ -545,6 +620,7 @@ ], "source": [ "'''groupby()\n", + "Arguments/Defaults: groupby(iterable, key=None)\n", "\n", "Makes an iterator that returns consecutive keys and groups from the iterable.\n", "\n", @@ -554,6 +630,8 @@ "Generally, the iterable needs to already be sorted on the same key function.\n", "'''\n", "\n", + "# Only returns the individual elements\n", + "\n", "[k for k, g in groupby('AAAABBBCCDAABBB')]" ] }, @@ -584,6 +662,13 @@ "[list(g) for k, g in groupby('AAAABBBCCDAABBB')]" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Islice" + ] + }, { "cell_type": "code", "execution_count": 78, @@ -600,6 +685,7 @@ ], "source": [ "'''islice()\n", + "Arguments/Defaults: islice(iterable, *args)\n", "\n", "Very similar to calling the index in a string\n", "'''\n", @@ -677,9 +763,16 @@ "print(list('ABCDEFG'[::2]))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Starmap" + ] + }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 46, "metadata": {}, "outputs": [ { @@ -693,10 +786,13 @@ ], "source": [ "'''starmap()\n", + "Arguments/Defaults: starmap(function, iterable)\n", "\n", "Makes an iterator that computes the function\n", "using arguments obtained from the iterable.\n", "\n", + "\n", + "Works as a generator.\n", "'''\n", "print(list(starmap(pow, [(2,5), (3,2), (10,3)])))\n", "\n", @@ -708,35 +804,234 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "32\n" + ] + } + ], + "source": [ + "starmap_obj = starmap(pow, [(2,5), (3,2), (10,3)])\n", + "\n", + "print(next(starmap_obj))" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], + "source": [ + "Takewhile" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4]" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'''takewhile()\n", + "Arguments/Defaults: takewhile(predicate, iterable)\n", "\n", - "'''" + "Make an iterator that returns elements \n", + "from the iterable as long as the predicate is true.\n", + "'''\n", + "\n", + "list(takewhile(lambda x: x<5, [1,4,6,4,1]))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "takewhile_obj = takewhile(lambda x: x<5, [1,4,6,4,1])\n", + "\n", + "print(next(takewhile_obj))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Tee" + ] + }, + { + "cell_type": "code", + "execution_count": 192, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(, )\n" + ] + } + ], "source": [ "'''tee()\n", + "Arguments/Defaults: tee(iterable, n=2)\n", "\n", - "'''" + "This one is tricky, it will take the iterable that you give it \n", + "and return a tuple of n iterables, which you can then iterate through. \n", + "'''\n", + "\n", + "b = tee((1,2,3,4))\n", + "print(b)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 193, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "for first in b[0]:\n", + " print(first)" + ] + }, + { + "cell_type": "code", + "execution_count": 194, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "for second in b[1]:\n", + " print(second)" + ] + }, + { + "cell_type": "code", + "execution_count": 195, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "ename": "IndexError", + "evalue": "tuple index out of range", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mthird\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mthird\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mIndexError\u001b[0m: tuple index out of range" + ] + } + ], + "source": [ + "for third in b[2]:\n", + " print(third)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Zip Longest" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]\n", + "[('A', 'x'), ('B', 'y'), ('C', 'z'), ('D', '-')]\n", + "[('A', 'x'), ('B', 'y'), ('C', 'z'), ('D', 'a'), ('-', 'b')]\n" + ] + } + ], "source": [ "'''zip_longest()\n", "\n", - "'''" + "Make an iterator that aggregates elements from each of the iterables.\n", + "If the iterables are of uneven length, missing values are filled-in with the fillvalue.\n", + "Iteration continues until the longest iterable is exhausted.\n", + "'''\n", + "\n", + "print(list(zip_longest('ABCD', 'xy', fillvalue='-')))\n", + "\n", + "print(list(zip_longest('ABCD', 'xyz', fillvalue='-')))\n", + "\n", + "print(list(zip_longest('ABCD', 'xyzab', fillvalue='-')))" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('A', 'x')\n" + ] + } + ], + "source": [ + "zip_longest_obj = zip_longest('ABCD', 'xy', fillvalue='-')\n", + "\n", + "print(next(zip_longest_obj))" ] }, { @@ -750,12 +1045,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Product is the equivalent for-loops. Repeat lets you choose how many times to nest." + "Product" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 146, "metadata": {}, "outputs": [ { @@ -764,7 +1059,7 @@ "[('A',), ('B',), ('C',), ('D',)]" ] }, - "execution_count": 16, + "execution_count": 146, "metadata": {}, "output_type": "execute_result" } @@ -772,8 +1067,11 @@ "source": [ "'''\n", "Product ()\n", + "Arguments/Defaults: (*iterables, repeat=1)\n", "\n", "Product is equivalent to for loops. Repeat lets you choose how many times to nest.\n", + "\n", + "Works as a generator.\n", "'''\n", "\n", "# Single for loop\n", @@ -782,59 +1080,245 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]" + "[('A', 'A'),\n", + " ('A', 'B'),\n", + " ('A', 'C'),\n", + " ('A', 'D'),\n", + " ('B', 'A'),\n", + " ('B', 'B'),\n", + " ('B', 'C'),\n", + " ('B', 'D'),\n", + " ('C', 'A'),\n", + " ('C', 'B'),\n", + " ('C', 'C'),\n", + " ('C', 'D'),\n", + " ('D', 'A'),\n", + " ('D', 'B'),\n", + " ('D', 'C'),\n", + " ('D', 'D')]" ] }, - "execution_count": 15, + "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# Nested for-loop\n", - "list(combinations('ABCD', 2))" + "list(product('ABCD', repeat=2))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 182, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('A',)\n" + ] + } + ], + "source": [ + "product_object = product('ABCD', repeat=1)\n", + "\n", + "print(next(product_object))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Permuations" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('A', 'B'),\n", + " ('A', 'C'),\n", + " ('A', 'D'),\n", + " ('B', 'A'),\n", + " ('B', 'C'),\n", + " ('B', 'D'),\n", + " ('C', 'A'),\n", + " ('C', 'B'),\n", + " ('C', 'D'),\n", + " ('D', 'A'),\n", + " ('D', 'B'),\n", + " ('D', 'C')]" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "'''permutations()\n", + "Arguments/Defaults:\n", + "\n", + "Results all possible orderings, no repeated elements\n", + "\n", + "Works as a generator.\n", "'''\n", - "combinations_with_replacement()\n", "\n", - "'''" + "list(permutations('ABCD', 2))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 184, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('A', 'B')\n" + ] + } + ], + "source": [ + "permutation_obj = permutations('ABCD', 2)\n", + "\n", + "print(next(permutation_obj))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Combinations" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "'''combinations()\n", + "Arguments/Defaults:\n", + "\n", + "results in sorted order, no repeated elements\n", + "\n", + "Works as a generator.\n", "'''\n", - "count()\n", "\n", - "'''" + "list(combinations('ABCD', 2))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 185, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('A', 'B')\n" + ] + } + ], + "source": [ + "combinations_obj = combinations('ABCD', 2)\n", + "\n", + "print(next(combinations_obj))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Combinations With Replacement" + ] + }, + { + "cell_type": "code", + "execution_count": 188, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('A', 'A'),\n", + " ('A', 'B'),\n", + " ('A', 'C'),\n", + " ('A', 'D'),\n", + " ('B', 'B'),\n", + " ('B', 'C'),\n", + " ('B', 'D'),\n", + " ('C', 'C'),\n", + " ('C', 'D'),\n", + " ('D', 'D')]" + ] + }, + "execution_count": 188, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'''\n", - "accumulate()\n", + "combinations_with_replacement()\n", + "Arguments/Defaults:\n", "\n", - "'''" + "Results in sorted order, with repeated elements\n", + "The list should return with these additional elements:\n", + "[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')]\n", + "\n", + "Works as a generator.\n", + "'''\n", + "\n", + "list(combinations_with_replacement('ABCD', 2))" + ] + }, + { + "cell_type": "code", + "execution_count": 189, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('A', 'A')\n" + ] + } + ], + "source": [ + "combinations_replace_obj = combinations_with_replacement('ABCD', 2)\n", + "\n", + "print(next(combinations_replace_obj))" ] }, { @@ -968,13 +1452,22 @@ "from functools import *" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Partial" + ] + }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "'''\n", + "'''Partial\n", + "Arguments/Defaults: (func, *args, **keywords)\n", + "\n", "Partial makes a new version of a function with one or more arguments already filled in. Used for quick access.\n", "\n", "Good resource https://www.pydanny.com/python-partials-are-fun.html\n", @@ -1005,6 +1498,13 @@ "freshman('Joe','Smith')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Reduce" + ] + }, { "cell_type": "code", "execution_count": 10, @@ -1022,8 +1522,8 @@ } ], "source": [ - "'''\n", - "reduce()\n", + "'''reduce()\n", + "Arguments/Defaults: (function, iterable, initializer=None)\n", "\n", "Reduce needs to be imported in Python3.x\n", "\n", From 149208f28754df557a312a22eeda7c5d1c9224f1 Mon Sep 17 00:00:00 2001 From: Tyler Guo Date: Tue, 9 Oct 2018 15:48:02 -0700 Subject: [PATCH 07/10] Checking in again --- built_in_library_tips.ipynb | 1046 +++++++++++++++++++++++++++++------ python_tips.ipynb | 2 +- 2 files changed, 870 insertions(+), 178 deletions(-) diff --git a/built_in_library_tips.ipynb b/built_in_library_tips.ipynb index d0f6c18..bb8392e 100644 --- a/built_in_library_tips.ipynb +++ b/built_in_library_tips.ipynb @@ -158,8 +158,7 @@ } ], "source": [ - "'''count()\n", - "Arguments/Defaults: count(start=0, step=1)\n", + "'''count(start=0, step=1)\n", "\n", "This creates an iterable object that goes up by the step you specify.\n", "This will continuously yield increments of 5 i.e. 0, 5, 10, 15, 20...\n", @@ -199,8 +198,7 @@ } ], "source": [ - "'''cycle()\n", - "Arguments/Defaults: cycle(iterable)\n", + "'''cycle(iterable)\n", "\n", "This creates an iterable object of what you pass in in an endless cycle.\n", "This will continuously yield, A B C D A B C D A B C D ...\n", @@ -239,18 +237,36 @@ } ], "source": [ - "'''repeat()\n", - "Arguments/Defaults: repeat(object, times=None)\n", + "'''repeat(object, times=None)\n", "\n", "Used to repeat an element up to n times.\n", "\n", - "Does not work as intended as a generator\n", + "Works as a generator.\n", "'''\n", "\n", "print(list(repeat(10, 3)))\n", "print(list(repeat('hello',4)))" ] }, + { + "cell_type": "code", + "execution_count": 219, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "repeat_obj = repeat(10, 3)\n", + "\n", + "print(next(repeat_obj))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -282,8 +298,7 @@ } ], "source": [ - "'''accumulate()\n", - "Arguments/Defaults: accumulate(iterable, func=operator.add)\n", + "'''accumulate(iterable, func=operator.add)\n", "\n", "Accumulate makes an iterator that returns accumulated sums\n", "Works similar to itertools.reduce() although only with addition\n", @@ -334,8 +349,7 @@ } ], "source": [ - "'''chain()\n", - "Arguments/Defaults: chain(*iterables)\n", + "'''chain(*iterables)\n", "\n", "Chain makes an iterator that returns elements from the first iterable until it is exhausted,\n", "then proceeds to the next iterable, until all of the iterables are exhausted.\n", @@ -386,8 +400,7 @@ } ], "source": [ - "'''chain.from_interable()\n", - "Arguments/Defaults: from_iterable(iterables)\n", + "'''chain.from_interable(iterables)\n", "\n", "Alternate constructor for chain(). Gets chained inputs from a single \n", "iterable argument that is evaluated.\n", @@ -439,12 +452,13 @@ } ], "source": [ - "'''compress()\n", - "Arguments/Defaults: compress(data, selectors)\n", + "'''compress(data, selectors)\n", "\n", "Compress makes an iterator that filters elements from data returning\n", "only those that have a corresponding element in selectors that\n", - "evaluates to True. Stops when either the data or selectors iterables has been exhausted\n", + "evaluates to True. \n", + "\n", + "Stops when either the data or selectors iterables have been exhausted.\n", "\n", "Works as a generator.\n", "'''\n", @@ -495,8 +509,7 @@ } ], "source": [ - "'''dropwhile()\n", - "Arguments/Defaults: dropwhile(predicate, iterable)\n", + "'''dropwhile(predicate, iterable)\n", "\n", "Dropwhile makes an iterator that drops elements from the iterable\n", "as long as the predicate is true; afterwards, returns every element.\n", @@ -558,8 +571,7 @@ } ], "source": [ - "'''filterflase()\n", - "Arguments/Defaults: filterfalse(predicate, iterable)\n", + "'''filterfalse(predicate, iterable)\n", "\n", "Makes an iterator that filters elements from an iterable \n", "returning only those for which the predicate is False (0)\n", @@ -619,13 +631,14 @@ } ], "source": [ - "'''groupby()\n", - "Arguments/Defaults: groupby(iterable, key=None)\n", + "'''groupby(iterable, key=None)\n", "\n", - "Makes an iterator that returns consecutive keys and groups from the iterable.\n", + "Makes an iterator that returns consecutive keys\n", + "and groups from the iterable.\n", "\n", "The operation of groupby() is similar to the uniq filter in Unix.\n", - "It generates a break or new group every time the value of the key function changes.\n", + "It generates a break or new group every time the value of the key\n", + "function changes.\n", "\n", "Generally, the iterable needs to already be sorted on the same key function.\n", "'''\n", @@ -684,8 +697,7 @@ } ], "source": [ - "'''islice()\n", - "Arguments/Defaults: islice(iterable, *args)\n", + "'''islice(iterable, *args)\n", "\n", "Very similar to calling the index in a string\n", "'''\n", @@ -785,8 +797,7 @@ } ], "source": [ - "'''starmap()\n", - "Arguments/Defaults: starmap(function, iterable)\n", + "'''starmap(function, iterable)\n", "\n", "Makes an iterator that computes the function\n", "using arguments obtained from the iterable.\n", @@ -849,10 +860,9 @@ } ], "source": [ - "'''takewhile()\n", - "Arguments/Defaults: takewhile(predicate, iterable)\n", + "'''takewhile(predicate, iterable)\n", "\n", - "Make an iterator that returns elements \n", + "Makes an iterator that returns elements \n", "from the iterable as long as the predicate is true.\n", "'''\n", "\n", @@ -899,8 +909,7 @@ } ], "source": [ - "'''tee()\n", - "Arguments/Defaults: tee(iterable, n=2)\n", + "'''tee(iterable, n=2)\n", "\n", "This one is tricky, it will take the iterable that you give it \n", "and return a tuple of n iterables, which you can then iterate through. \n", @@ -1001,10 +1010,12 @@ } ], "source": [ - "'''zip_longest()\n", + "'''zip_longest(*iterables, fillvalue=None)\n", + "\n", + "Makes an iterator that aggregates elements from each of the iterables.\n", + "If the iterables are of uneven length, missing values are filled-in \n", + "with the fillvalue.\n", "\n", - "Make an iterator that aggregates elements from each of the iterables.\n", - "If the iterables are of uneven length, missing values are filled-in with the fillvalue.\n", "Iteration continues until the longest iterable is exhausted.\n", "'''\n", "\n", @@ -1065,9 +1076,7 @@ } ], "source": [ - "'''\n", - "Product ()\n", - "Arguments/Defaults: (*iterables, repeat=1)\n", + "'''Product (*iterables, repeat=1)\n", "\n", "Product is equivalent to for loops. Repeat lets you choose how many times to nest.\n", "\n", @@ -1169,8 +1178,7 @@ } ], "source": [ - "'''permutations()\n", - "Arguments/Defaults:\n", + "'''permutations(iterable, r=None_\n", "\n", "Results all possible orderings, no repeated elements\n", "\n", @@ -1223,8 +1231,7 @@ } ], "source": [ - "'''combinations()\n", - "Arguments/Defaults:\n", + "'''combinations(iterable, r)\n", "\n", "results in sorted order, no repeated elements\n", "\n", @@ -1288,9 +1295,7 @@ } ], "source": [ - "'''\n", - "combinations_with_replacement()\n", - "Arguments/Defaults:\n", + "'''combinations_with_replacement(iterable, r)\n", "\n", "Results in sorted order, with repeated elements\n", "The list should return with these additional elements:\n", @@ -1335,7 +1340,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -1343,72 +1348,136 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "'''namedtuple()\n", - "'''" + "Namedtuple" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 244, "metadata": {}, "outputs": [], "source": [ - "'''deque'''" + "'''namedtuple(typename, field_names, verbose=False, rename=False)\n", + "\n", + "Returns a new tuple subclass named typename. \n", + "The new subclass is used to create tuple-like objects that can act like\n", + "dictionaries and lists.\n", + "\n", + "._make creates list like tuple\n", + "\n", + "._asdict() creates dict like tuple\n", + "\n", + "._replace Return a new instance of the named tuple \n", + " replacing specified fields with new values.\n", + "\n", + "._fields Tuple of strings listing the field names. \n", + "'''\n", + "\n", + "Point = namedtuple('Subclass', ['x', 'y'])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 245, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Subclass(x=11, y=22)\n", + "Subclass(x=11, y=22)\n" + ] + } + ], "source": [ - "'''ChainMap\n", - "'''" + "# These create list like objects, iterable and indexable.\n", + "\n", + "# ._make\n", + "\n", + "print(Point(11,22))\n", + "\n", + "# Similar to\n", + "\n", + "t = [11,22]\n", + "print(Point._make(t))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 246, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "OrderedDict([('x', 11), ('y', 22)])" + ] + }, + "execution_count": 246, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "'''Counter\n", - "'''" + "# ._asdict()\n", + "\n", + "Point(11,22)._asdict()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 250, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Subclass(x=33, y=22)" + ] + }, + "execution_count": 250, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "'''OrderedDict\n", - "'''" + "# ._replace\n", + "\n", + "p = Point(x=11, y=22)\n", + "p._replace(x=33)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 251, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "('x', 'y')" + ] + }, + "execution_count": 251, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "'''defaultdict\n", - "'''" + "# ._fields\n", + "\n", + "p._fields" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "'''UserDict\n", - "'''" + "Deque" ] }, { @@ -1417,218 +1486,841 @@ "metadata": {}, "outputs": [], "source": [ - "'''UserList\n", + "'''deque\n", + "deque([iterable[, maxlen]])\n", + "\n", + "Deques are a generalization of stacks and queues\n", + "\n", + "The name is pronounced “deck” and is short for\n", + "“double-ended queue.\n", + "\n", + "Think of a deque as a list like container with \n", + "fast appends and pops on either end.\n", + "\n", + "\n", + "append(x) Add x to the right side of the deque.\n", + "\n", + "appendleft(x) Add x to the left side of the deque.\n", + "\n", + "clear() Remove all elements from the deque \n", + " leaving it with length 0.\n", + "\n", + "count(x) Count the number of deque elements equal to x.\n", + "\n", + "extend(iterable) Extend the right side of the deque \n", + " by appending elements from the iterable argument.\n", + "\n", + "extendleft(iterable) Extend the left side of the deque by appending\n", + " elements from iterable. Note, the series of \n", + " left appends results in reversing the order \n", + " of elements in the iterable argument.\n", + "\n", + "pop() Remove and return an element from the right \n", + " side of the deque. If no elements are present, \n", + " raises an IndexError.\n", + "\n", + "popleft() Remove and return an element from the left \n", + " side of the deque. If no elements are present,\n", + " raises an IndexError.\n", + "\n", + "remove(value) Removed the first occurrence of value.\n", + " If not found, raises a ValueError.\n", + "\n", + "reverse() Reverse the elements of the deque in-place\n", + " and then return None.\n", + "\n", + "rotate(n) Rotate the deque n steps to the right.\n", + " If n is negative, rotate to the left.\n", + " Rotating one step to the right is equivalent to:\n", + " d.appendleft(d.pop()).\n", "'''" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "'''UserString\n", - "'''" - ] - }, - { - "cell_type": "markdown", + "execution_count": 95, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([1, 2, 3])\n" + ] + } + ], "source": [ - "# Functools\n", - "[Return to table of contents](#toc)\n", - "\n", - "The functools module is for higher-order functions: functions that act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module.\n", + "d = deque([1,2,3])\n", "\n", - "https://docs.python.org/3/library/functools.html#functools.partial" + "print(d)" ] }, { "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "from functools import *" - ] - }, - { - "cell_type": "markdown", + "execution_count": 96, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([1, 2, 3, 4])\n" + ] + } + ], "source": [ - "Partial" + "# append()\n", + "\n", + "d.append(4)\n", + "\n", + "print(d)" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 97, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([0, 1, 2, 3, 4])\n" + ] + } + ], "source": [ - "'''Partial\n", - "Arguments/Defaults: (func, *args, **keywords)\n", - "\n", - "Partial makes a new version of a function with one or more arguments already filled in. Used for quick access.\n", + "# appendleft()\n", "\n", - "Good resource https://www.pydanny.com/python-partials-are-fun.html\n", - "'''\n", - "\n", - "# Initial function\n", - "def student(first,last,grade):\n", - " print(first,last,grade)\n", + "d.appendleft(0) \n", "\n", - "# Partial function\n", - "freshman = partial(student, grade=10)" + "print(d)" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Joe Smith 10\n" + "deque([])\n" ] } ], "source": [ - "freshman('Joe','Smith')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Reduce" + "# clear()\n", + "\n", + "d.clear()\n", + "\n", + "print(d)" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 100, "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([1, 2, 3])\n" + ] + }, { "data": { "text/plain": [ - "3628800" + "1" ] }, - "execution_count": 10, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "'''reduce()\n", - "Arguments/Defaults: (function, iterable, initializer=None)\n", - "\n", - "Reduce needs to be imported in Python3.x\n", - "\n", - "Reduce is a really useful function for performing some computation on a list and returning the result.\n", - "It applies a rolling computation to sequential pairs of values in a list. This one is tricky.\n", + "# count()\n", + "d = deque([1,2,3])\n", "\n", - "Easiest example to understnad is trying to multiply a whole list together i.e 1*2*3*4*5*6*7*8*9*10\n", - "'''\n", - "\n", - "list_1 = list(range(1,11))\n", + "print(d)\n", "\n", - "reduce((lambda x, y: x * y), list_1) " + "d.count(2)" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 101, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([1, 2, 3, 4, 5, 6])\n" + ] } ], "source": [ - "# Another great example is using reduce to compare elements in a list against each other.\n", + "# entend()\n", "\n", - "reduce(lambda x, y: y if y > x else x, list_1) # Finding the largest number in the list" + "d.extend([4,5,6])\n", + "\n", + "print(d)" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "scrolled": true - }, + "execution_count": 102, + "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([-2, -1, 0, 1, 2, 3, 4, 5, 6])\n" + ] } ], "source": [ - "reduce(lambda x, y: y if y < x else x, list_1) # Finding the smallest number in the list" + "# extendleft()\n", + "\n", + "d.extendleft([0,-1,-2])\n", + "\n", + "print(d)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 103, "metadata": {}, - "source": [ - "# Decorators\n", - "[Return to table of contents](#toc)" - ] - }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "deque([-2, -1, 0, 1, 2, 3, 4, 5])\n" + ] + } + ], + "source": [ + "# pop()\n", + "\n", + "print(d.pop())\n", + "\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-2\n", + "deque([-1, 0, 1, 2, 3, 4, 5])\n" + ] + } + ], + "source": [ + "# popleft()\n", + "\n", + "print(d.popleft())\n", + "\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([-1, 0, 1, 2, 3, 4, 5])\n", + "deque([-1, 0, 1, 2, 3, 4])\n" + ] + } + ], + "source": [ + "# remove()\n", + "\n", + "print(d)\n", + "\n", + "d.remove(5)\n", + "\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([-1, 0, 1, 2, 3, 4])\n", + "deque([4, 3, 2, 1, 0, -1])\n" + ] + } + ], + "source": [ + "# reverse()\n", + "\n", + "print(d)\n", + "\n", + "d.reverse()\n", + "\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([-1, 4, 3, 2, 1, 0])\n", + "deque([0, -1, 4, 3, 2, 1])\n", + "deque([-1, 4, 3, 2, 1, 0])\n" + ] + } + ], + "source": [ + "# rotate(n) # You can adjust n\n", + "\n", + "print(d)\n", + "\n", + "d.rotate() # Pushed the right most element to become the left most element.\n", + "\n", + "print(d)\n", + "\n", + "d.rotate(-1) # Pushed the left most element to become the right most element.\n", + "\n", + "print(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "ChainMap" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "'''ChainMap\n", + "\n", + "A ChainMap class is provided for quickly linking\n", + "a number of mappings so they can be treated as a single unit.\n", + "It is often much faster than creating a new dictionary\n", + "and running multiple update() calls.\n", + "\n", + "If there a key that is the same within the ChainMap object, \n", + "it will assign the value to the first instance of that key.\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": {}, + "outputs": [], + "source": [ + "dict1 = {'a':1,'b':2,'c':3}\n", + "dict2 = {'c':4,'d':5,'e':6}\n", + "\n", + "chained = ChainMap(dict1,dict2)" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 117, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chained['b']" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 118, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Assigned 3 not 4 because dict1 was paased first.\n", + "\n", + "chained['c']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Counter" + ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "'''Counter\n", + "\n", + "\n", + "'''" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "OrderedDict" + ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "'''OrderedDict\n", + "'''" + ] }, { "cell_type": "markdown", "metadata": {}, - "source": [] + "source": [ + "DefaultDict" + ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "'''defaultdict\n", + "'''" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Userdict" + ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "'''UserDict\n", + "'''" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Userlist" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''UserList\n", + "'''" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Userstring" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''UserString\n", + "'''" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Decorators\n", + "[Return to table of contents](#toc)\n", + "\n", + "A decorator in Python is any callable Python object that is used to modify a function or a class\n", + "\n", + "Decorators work like wrappers or first class function." + ] + }, + { + "cell_type": "code", + "execution_count": 273, + "metadata": {}, + "outputs": [], + "source": [ + "# Outer is the first class funcion.\n", + "def outer(x):\n", + " message = x\n", + " \n", + " def inner():\n", + " print(''+message+'')\n", + " \n", + " return inner()" + ] + }, + { + "cell_type": "code", + "execution_count": 274, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello\n" + ] + } + ], + "source": [ + "outer('hello')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now using as a decorator" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "def html_wrapper(function):\n", + " def wrap(code):\n", + " print(f'

{code}

')\n", + " return wrap" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "@html_wrapper\n", + "def need_to_wrap(code):\n", + " print(code)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "

Wrap this paragraph

\n" + ] + } + ], + "source": [ + "need_to_wrap('Wrap this paragraph')" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "@html_wrapper\n", + "def need_to_wrap(code):\n", + " print(code)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "

Using it again

\n" + ] + } + ], + "source": [ + "need_to_wrap('Using it again')" + ] + }, + { + "cell_type": "code", + "execution_count": 307, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "128\n", + "32\n", + "243\n" + ] + } + ], + "source": [ + "'''Great example from \n", + "https://realpython.com/inner-functions-what-are-they-good-for/\n", + "'''\n", + "\n", + "def generate_power(exponent):\n", + " def decorator(f):\n", + " def inner(*args):\n", + " result = f(*args)\n", + " return exponent**result\n", + " return inner\n", + " return decorator\n", + "\n", + "@generate_power(2)\n", + "def raise_two(n):\n", + " return n\n", + "\n", + "print(raise_two(7)) # 2**7\n", + "print(raise_two(5)) # 2**5\n", + "\n", + "@generate_power(3)\n", + "def raise_three(n):\n", + " return n\n", + "\n", + "print(raise_three(5)) # 3**5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Functools\n", + "[Return to table of contents](#toc)\n", + "\n", + "The functools module is for higher-order functions: functions that act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module.\n", + "\n", + "https://docs.python.org/3/library/functools.html#functools.partial" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "from functools import *" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Partial" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "'''Partial(func, *args, **keywords)\n", + "\n", + "Partial makes a new version of a function with one or more arguments already filled in. Used for quick access.\n", + "\n", + "Good resource https://www.pydanny.com/python-partials-are-fun.html\n", + "'''\n", + "\n", + "# Initial function\n", + "def student(first,last,grade):\n", + " print(first,last,grade)\n", + "\n", + "# Partial function\n", + "freshman = partial(student, grade=10)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Joe Smith 10\n" + ] + } + ], + "source": [ + "freshman('Joe','Smith')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''partialmethod(func, *args, **keywords)\n", + "\n", + "\n", + "'''" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Reduce" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3628800" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'''reduce()\n", + "Arguments/Defaults: (function, iterable, initializer=None)\n", + "\n", + "Reduce needs to be imported in Python3.x\n", + "\n", + "Reduce is a really useful function for performing some computation on a list and returning the result.\n", + "It applies a rolling computation to sequential pairs of values in a list. This one is tricky.\n", + "\n", + "Easiest example to understnad is trying to multiply a whole list together i.e 1*2*3*4*5*6*7*8*9*10\n", + "'''\n", + "\n", + "list_1 = list(range(1,11))\n", + "\n", + "reduce((lambda x, y: x * y), list_1) " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Another great example is using reduce to compare elements in a list against each other.\n", + "\n", + "reduce(lambda x, y: y if y > x else x, list_1) # Finding the largest number in the list" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "reduce(lambda x, y: y if y < x else x, list_1) # Finding the smallest number in the list" + ] }, { "cell_type": "markdown", diff --git a/python_tips.ipynb b/python_tips.ipynb index 8aa2143..f0fae04 100644 --- a/python_tips.ipynb +++ b/python_tips.ipynb @@ -639,7 +639,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 24, From 7177334c8a4552e10b4a274fefaacca2abb79713 Mon Sep 17 00:00:00 2001 From: Tyler Guo Date: Tue, 9 Oct 2018 16:35:06 -0700 Subject: [PATCH 08/10] Checking in again --- README.rst | 6 +- built_in_library_tips.ipynb | 567 +++++++++++++++++++++++------------- python_tips.ipynb | 6 +- 3 files changed, 366 insertions(+), 213 deletions(-) diff --git a/README.rst b/README.rst index e98a311..f000f86 100644 --- a/README.rst +++ b/README.rst @@ -12,10 +12,12 @@ These files are .ipynb. It is a notebook document used by Jupyter Notebook, an i I suggest downloading `Anaconda `_. Anaconda is a free and open source distribution of the Python and R programming languages that aims to simplify package management and deployment. Jupyter Notebook is included in Anaconda. +Please feel free to contribute, critique and comment. + Python Tips topics: - Lambda functions - Enumerate - - List, set and dict comprehension + - Comprehension (list, set, dict) - Map - Filter - Zip @@ -27,9 +29,9 @@ Python Tips topics: Built-In Libraries Tips topics: - Generators - Itertools (built-in library) + - Collections (built-in library) - Decorators - Functools (built-in library) reduce, partial.. etc. - - Collections (built-in library) - Datetime (built-in library) Built-In Functions Tips 1(WIP): diff --git a/built_in_library_tips.ipynb b/built_in_library_tips.ipynb index bb8392e..c2e3d5b 100644 --- a/built_in_library_tips.ipynb +++ b/built_in_library_tips.ipynb @@ -13,6 +13,9 @@ "\n", "All functions are operating under their default parameters.\n", "\n", + "[Great website](https://realpython.com)
\n", + "[Great youtuber](https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g)\n", + "\n", "Topics:\n", " - [Generators](#generators)\n", " - [Itertools](#itertools)\n", @@ -20,9 +23,9 @@ " - [Iterators terminating on the shortest input sequence](#itotsis)\n", " - [Combinatoric iterators](#combinatoric)\n", " - [Collections](#collections)\n", + " - [Decorators](#decorators)\n", " - [Functools](#functools)\n", - " - [Datetime](#dt)\n", - " - [Calendar](#calendar)" + " - [Datetime](#dt)" ] }, { @@ -37,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -52,9 +55,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 27, 256]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Iterates, stores in product_list and returns product_list.\n", "\n", @@ -63,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -76,9 +90,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "4\n", + "27\n", + "256\n" + ] + } + ], "source": [ "# Each time your generator is called upon using next, it will only then yield the result. \n", "# This will continue until your generator is exhausted.\n", @@ -88,14 +113,25 @@ "print(next(my_gen))\n", "print(next(my_gen))\n", "print(next(my_gen))\n", - "print(next(my_gen))" + "# print(next(my_gen)) This will raise a StopIteration uncomment to run." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "4\n", + "27\n", + "256\n" + ] + } + ], "source": [ "# You can also iterate over your generator\n", "\n", @@ -121,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -144,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -182,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -224,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -250,7 +286,7 @@ }, { "cell_type": "code", - "execution_count": 219, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -283,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -292,7 +328,7 @@ "[1, 3, 6, 10, 15]" ] }, - "execution_count": 3, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -311,7 +347,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -337,14 +373,14 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "['a', 'b', 'c', 'e', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l']\n" + "['a', 'b', 'c', 'f', 'e', 'd', 'g', 'h', 'i', 'j', 'k', 'l']\n" ] } ], @@ -365,7 +401,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -384,19 +420,18 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 15, "metadata": {}, "outputs": [ { - "ename": "TypeError", - "evalue": "from_iterable() takes exactly one argument (4 given)", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m ''' \n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchain\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_iterable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'b'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'd'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'e'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'f'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'g'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'h'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'i'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'jkl'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: from_iterable() takes exactly one argument (4 given)" - ] + "data": { + "text/plain": [ + "'chain.from_interable(iterables)\\n\\nAlternate constructor for chain(). Gets chained inputs from a single \\niterable argument that is evaluated.\\n\\nWorks as a generator\\n'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -408,23 +443,27 @@ "Works as a generator\n", "''' \n", "\n", - "print(list(chain.from_iterable(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl')))" + "# This will raise a TypeError uncomment to run.\n", + "\n", + "# print(list(chain.from_iterable(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl')))" ] }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "['a', 'b', 'c', 'e', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l']\n" + "['a', 'b', 'c', 'f', 'e', 'd', 'g', 'h', 'i', 'j', 'k', 'l']\n" ] } ], "source": [ + "# Now with a single argument.\n", + "\n", "print(list(chain.from_iterable([('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl']))) # Now as a single argument" ] }, @@ -437,7 +476,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -446,7 +485,7 @@ "['A', 'C', 'E', 'F']" ] }, - "execution_count": 38, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -468,7 +507,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -494,7 +533,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -503,7 +542,7 @@ "[5, 6, 7, 8, 9]" ] }, - "execution_count": 59, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -530,7 +569,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -556,7 +595,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -565,7 +604,7 @@ "[0, 2, 4, 6, 8]" ] }, - "execution_count": 61, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -590,7 +629,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 22, "metadata": {}, "outputs": [ { @@ -616,7 +655,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -625,7 +664,7 @@ "['A', 'B', 'C', 'D', 'A', 'B']" ] }, - "execution_count": 66, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -650,7 +689,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -664,7 +703,7 @@ " ['B', 'B', 'B']]" ] }, - "execution_count": 68, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } @@ -684,7 +723,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 25, "metadata": {}, "outputs": [ { @@ -711,7 +750,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -733,7 +772,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -755,7 +794,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -784,7 +823,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 29, "metadata": {}, "outputs": [ { @@ -815,7 +854,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 30, "metadata": { "scrolled": true }, @@ -843,7 +882,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 31, "metadata": { "scrolled": true }, @@ -854,7 +893,7 @@ "[1, 4]" ] }, - "execution_count": 52, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } @@ -871,7 +910,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -897,14 +936,14 @@ }, { "cell_type": "code", - "execution_count": 192, + "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "(, )\n" + "(, )\n" ] } ], @@ -921,7 +960,7 @@ }, { "cell_type": "code", - "execution_count": 193, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -942,7 +981,7 @@ }, { "cell_type": "code", - "execution_count": 194, + "execution_count": 35, "metadata": { "scrolled": true }, @@ -965,26 +1004,16 @@ }, { "cell_type": "code", - "execution_count": 195, + "execution_count": 36, "metadata": { "scrolled": true }, - "outputs": [ - { - "ename": "IndexError", - "evalue": "tuple index out of range", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mthird\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mthird\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mIndexError\u001b[0m: tuple index out of range" - ] - } - ], + "outputs": [], "source": [ - "for third in b[2]:\n", - " print(third)" + "# Will raise IndexError uncomment to run.\n", + "\n", + "# for third in b[2]:\n", + "# print(third)" ] }, { @@ -996,7 +1025,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 37, "metadata": {}, "outputs": [ { @@ -1028,7 +1057,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 38, "metadata": {}, "outputs": [ { @@ -1061,7 +1090,7 @@ }, { "cell_type": "code", - "execution_count": 146, + "execution_count": 39, "metadata": {}, "outputs": [ { @@ -1070,7 +1099,7 @@ "[('A',), ('B',), ('C',), ('D',)]" ] }, - "execution_count": 146, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -1089,7 +1118,7 @@ }, { "cell_type": "code", - "execution_count": 147, + "execution_count": 40, "metadata": {}, "outputs": [ { @@ -1113,7 +1142,7 @@ " ('D', 'D')]" ] }, - "execution_count": 147, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -1124,7 +1153,7 @@ }, { "cell_type": "code", - "execution_count": 182, + "execution_count": 41, "metadata": {}, "outputs": [ { @@ -1150,7 +1179,7 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": 42, "metadata": { "scrolled": true }, @@ -1172,7 +1201,7 @@ " ('D', 'C')]" ] }, - "execution_count": 111, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } @@ -1190,7 +1219,7 @@ }, { "cell_type": "code", - "execution_count": 184, + "execution_count": 43, "metadata": {}, "outputs": [ { @@ -1216,7 +1245,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 44, "metadata": {}, "outputs": [ { @@ -1225,7 +1254,7 @@ "[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]" ] }, - "execution_count": 113, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } @@ -1243,7 +1272,7 @@ }, { "cell_type": "code", - "execution_count": 185, + "execution_count": 45, "metadata": {}, "outputs": [ { @@ -1269,7 +1298,7 @@ }, { "cell_type": "code", - "execution_count": 188, + "execution_count": 46, "metadata": { "scrolled": true }, @@ -1289,7 +1318,7 @@ " ('D', 'D')]" ] }, - "execution_count": 188, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } @@ -1309,7 +1338,7 @@ }, { "cell_type": "code", - "execution_count": 189, + "execution_count": 47, "metadata": {}, "outputs": [ { @@ -1340,7 +1369,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ @@ -1356,15 +1385,17 @@ }, { "cell_type": "code", - "execution_count": 244, + "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "'''namedtuple(typename, field_names, verbose=False, rename=False)\n", "\n", "Returns a new tuple subclass named typename. \n", + "\n", "The new subclass is used to create tuple-like objects that can act like\n", - "dictionaries and lists.\n", + "dictionaries and lists. Very good at being descriptive check \n", + "the real world application below.\n", "\n", "._make creates list like tuple\n", "\n", @@ -1381,7 +1412,7 @@ }, { "cell_type": "code", - "execution_count": 245, + "execution_count": 50, "metadata": {}, "outputs": [ { @@ -1408,7 +1439,7 @@ }, { "cell_type": "code", - "execution_count": 246, + "execution_count": 51, "metadata": {}, "outputs": [ { @@ -1417,7 +1448,7 @@ "OrderedDict([('x', 11), ('y', 22)])" ] }, - "execution_count": 246, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -1430,7 +1461,7 @@ }, { "cell_type": "code", - "execution_count": 250, + "execution_count": 52, "metadata": {}, "outputs": [ { @@ -1439,7 +1470,7 @@ "Subclass(x=33, y=22)" ] }, - "execution_count": 250, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -1453,7 +1484,7 @@ }, { "cell_type": "code", - "execution_count": 251, + "execution_count": 53, "metadata": {}, "outputs": [ { @@ -1462,7 +1493,7 @@ "('x', 'y')" ] }, - "execution_count": 251, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } @@ -1473,6 +1504,31 @@ "p._fields" ] }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Color(red=255, green=255, blue=224)" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Real world application\n", + "\n", + "Color = namedtuple('Color', ['red','green','blue'])\n", + "banana = Color(red=255, green=255, blue=224)\n", + "\n", + "banana" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1482,9 +1538,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "deque([1, 2, 3])\n" + ] + } + ], "source": [ "'''deque\n", "deque([iterable[, maxlen]])\n", @@ -1533,23 +1597,8 @@ " If n is negative, rotate to the left.\n", " Rotating one step to the right is equivalent to:\n", " d.appendleft(d.pop()).\n", - "'''" - ] - }, - { - "cell_type": "code", - "execution_count": 95, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "deque([1, 2, 3])\n" - ] - } - ], - "source": [ + "'''\n", + "\n", "d = deque([1,2,3])\n", "\n", "print(d)" @@ -1557,7 +1606,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 56, "metadata": {}, "outputs": [ { @@ -1578,7 +1627,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 57, "metadata": {}, "outputs": [ { @@ -1599,7 +1648,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 58, "metadata": {}, "outputs": [ { @@ -1620,7 +1669,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 59, "metadata": {}, "outputs": [ { @@ -1636,7 +1685,7 @@ "1" ] }, - "execution_count": 100, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } @@ -1652,7 +1701,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 60, "metadata": {}, "outputs": [ { @@ -1673,7 +1722,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 61, "metadata": {}, "outputs": [ { @@ -1694,7 +1743,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 62, "metadata": {}, "outputs": [ { @@ -1716,7 +1765,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 63, "metadata": {}, "outputs": [ { @@ -1738,7 +1787,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 64, "metadata": {}, "outputs": [ { @@ -1762,7 +1811,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 65, "metadata": {}, "outputs": [ { @@ -1786,16 +1835,16 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "deque([4, 3, 2, 1, 0, -1])\n", "deque([-1, 4, 3, 2, 1, 0])\n", - "deque([0, -1, 4, 3, 2, 1])\n", - "deque([-1, 4, 3, 2, 1, 0])\n" + "deque([4, 3, 2, 1, 0, -1])\n" ] } ], @@ -1822,11 +1871,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'ChainMap(*maps)\\n\\nA ChainMap class is provided for quickly linking\\na number of mappings so they can be treated as a single unit.\\nIt is often much faster than creating a new dictionary\\nand running multiple update() calls.\\n\\nIf there a key that is the same within the ChainMap object, \\nit will assign the value to the first instance of that key.\\n'" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "'''ChainMap\n", + "'''ChainMap(*maps)\n", "\n", "A ChainMap class is provided for quickly linking\n", "a number of mappings so they can be treated as a single unit.\n", @@ -1840,7 +1900,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 68, "metadata": {}, "outputs": [], "source": [ @@ -1852,7 +1912,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 69, "metadata": {}, "outputs": [ { @@ -1861,7 +1921,7 @@ "2" ] }, - "execution_count": 117, + "execution_count": 69, "metadata": {}, "output_type": "execute_result" } @@ -1872,7 +1932,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 70, "metadata": { "scrolled": true }, @@ -1883,7 +1943,7 @@ "3" ] }, - "execution_count": 118, + "execution_count": 70, "metadata": {}, "output_type": "execute_result" } @@ -1903,14 +1963,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Counter({1: 3, 2: 3, 3: 3, 5: 3, 7: 3, 4: 3, 9: 3})" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "'''Counter\n", + "'''Counter([iterable-or-mapping])\n", "\n", + "It is an unordered collection where elements\n", + "are stored as dictionary keys and their counts\n", + "are stored as dictionary values.\n", "\n", - "'''" + "Keys are the elements in the list and \n", + "the values are the number of occurences\n", + "'''\n", + "\n", + "example_list = [1,1,1,2,2,2,3,3,3,5,5,5,7,7,7,4,4,4,9,9,9,]\n", + "\n", + "Counter(example_list)" ] }, { @@ -1922,11 +2002,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'OrderedDict\\n\\n'" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'''OrderedDict\n", + "\n", "'''" ] }, @@ -1939,9 +2031,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'defaultdict\\n'" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'''defaultdict\n", "'''" @@ -1956,9 +2059,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'UserDict\\n'" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'''UserDict\n", "'''" @@ -1973,9 +2087,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'UserList\\n'" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'''UserList\n", "'''" @@ -1990,9 +2115,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'UserString\\n'" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'''UserString\n", "'''" @@ -2012,7 +2148,7 @@ }, { "cell_type": "code", - "execution_count": 273, + "execution_count": 77, "metadata": {}, "outputs": [], "source": [ @@ -2028,7 +2164,7 @@ }, { "cell_type": "code", - "execution_count": 274, + "execution_count": 78, "metadata": {}, "outputs": [ { @@ -2052,7 +2188,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 79, "metadata": {}, "outputs": [], "source": [ @@ -2064,7 +2200,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 80, "metadata": {}, "outputs": [], "source": [ @@ -2075,7 +2211,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 81, "metadata": {}, "outputs": [ { @@ -2092,7 +2228,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ @@ -2103,7 +2239,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 83, "metadata": {}, "outputs": [ { @@ -2120,7 +2256,7 @@ }, { "cell_type": "code", - "execution_count": 307, + "execution_count": 84, "metadata": {}, "outputs": [ { @@ -2134,7 +2270,7 @@ } ], "source": [ - "'''Great example from \n", + "'''Example from \n", "https://realpython.com/inner-functions-what-are-they-good-for/\n", "'''\n", "\n", @@ -2174,7 +2310,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 85, "metadata": {}, "outputs": [], "source": [ @@ -2190,7 +2326,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 86, "metadata": {}, "outputs": [], "source": [ @@ -2211,7 +2347,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 87, "metadata": {}, "outputs": [ { @@ -2228,9 +2364,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 88, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'partialmethod(func, *args, **keywords)\\n\\n\\n'" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'''partialmethod(func, *args, **keywords)\n", "\n", @@ -2247,7 +2394,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 89, "metadata": {}, "outputs": [ { @@ -2256,7 +2403,7 @@ "3628800" ] }, - "execution_count": 10, + "execution_count": 89, "metadata": {}, "output_type": "execute_result" } @@ -2280,7 +2427,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 90, "metadata": {}, "outputs": [ { @@ -2289,7 +2436,7 @@ "10" ] }, - "execution_count": 11, + "execution_count": 90, "metadata": {}, "output_type": "execute_result" } @@ -2302,7 +2449,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 91, "metadata": { "scrolled": true }, @@ -2313,7 +2460,7 @@ "1" ] }, - "execution_count": 12, + "execution_count": 91, "metadata": {}, "output_type": "execute_result" } @@ -2336,7 +2483,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 92, "metadata": {}, "outputs": [], "source": [ @@ -2345,16 +2492,16 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "datetime.datetime(2018, 10, 8, 14, 41, 12, 607021)" + "datetime.datetime(2018, 10, 9, 16, 32, 52, 450932)" ] }, - "execution_count": 36, + "execution_count": 93, "metadata": {}, "output_type": "execute_result" } @@ -2372,14 +2519,16 @@ }, { "cell_type": "code", - "execution_count": 37, - "metadata": {}, + "execution_count": 94, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Today is the 8 day of month 10, and the year is 2018!\n" + "Today is the 9 day of month 10, and the year is 2018!\n" ] } ], @@ -2392,12 +2541,20 @@ "now_month = now.month\n", "now_day = now.day\n", "\n", - "print('Today is the {} day of month {}, and the year is {}!'.format(now_day, now_month, now_year))" + "print('Today is the {} day of month {}, and the year is {}!'\n", + " .format(now_day, now_month, now_year))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Strftime" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 95, "metadata": {}, "outputs": [ { @@ -2420,35 +2577,42 @@ "https://devhints.io/strftime.\n", "'''\n", "\n", - "print('The current month is {}.'.format(now_datetime_object.strftime('%B')))" + "print('The current month is {}.'.format(now.strftime('%B')))" ] }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Today's date is 08-10-2018. Be careful;\n", - "It is not a datetimeobject anymore it is a !\n" + "Today's date is 09-10-2018. Be careful;\n", + "It's not a datetimeobject anymore it is a !\n" ] } ], "source": [ "# strftime() can generally be used to format datetime objects.\n", "\n", - "formatted = now_datetime_object.strftime('%d-%m-%Y')\n", + "formatted = now.strftime('%d-%m-%Y')\n", "print('''Today's date is {}. Be careful;\n", "It's not a datetimeobject anymore it is a {}!'''\n", " .format(formatted, type(formatted)))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Timedelta" + ] + }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 97, "metadata": { "scrolled": true }, @@ -2479,23 +2643,6 @@ "print('''I started studying in university back in {},and I'll finish in {}'''\n", " .format(start_uni_time.strftime('%d-%m-%Y'), end_uni_time.strftime('%d-%m-%Y')))" ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Calendar \n", - "[Return to table of contents](#toc)" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [], - "source": [ - "import calendar" - ] } ], "metadata": { diff --git a/python_tips.ipynb b/python_tips.ipynb index f0fae04..0a518dd 100644 --- a/python_tips.ipynb +++ b/python_tips.ipynb @@ -10,6 +10,10 @@ "\n", "Please go through official documentation if you want more thorough examples.\n", "\n", + "[Great website](https://realpython.com)
\n", + "[Great youtuber](https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g)\n", + "\n", + "\n", "Topics:\n", " - [Lambda functions](#lambda)\n", " - [Enumerate](#enum)\n", @@ -639,7 +643,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 24, From fff7a5a35b78f1802e68246d4cf15618ddbfede8 Mon Sep 17 00:00:00 2001 From: Tyler Guo Date: Tue, 9 Oct 2018 16:57:46 -0700 Subject: [PATCH 09/10] Checking in some more --- built_in_library_tips.ipynb | 69 +++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/built_in_library_tips.ipynb b/built_in_library_tips.ipynb index c2e3d5b..d8095e5 100644 --- a/built_in_library_tips.ipynb +++ b/built_in_library_tips.ipynb @@ -24,6 +24,7 @@ " - [Combinatoric iterators](#combinatoric)\n", " - [Collections](#collections)\n", " - [Decorators](#decorators)\n", + " - [Setters, getters and deleters](#sgd)\n", " - [Functools](#functools)\n", " - [Datetime](#dt)" ] @@ -2296,6 +2297,29 @@ "print(raise_three(5)) # 3**5" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Setters, Getters and Deleters
\n", + "\n", + "[Video that helped me](https://www.youtube.com/watch?v=jCzT9XFZ5bw&t=459s)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -2314,7 +2338,12 @@ "metadata": {}, "outputs": [], "source": [ - "from functools import *" + "from functools import *\n", + "\n", + "'''Functools has a lot of wrapper tools and functionality. \n", + "I may go over them at some point. For now I think partial and reduce\n", + "are very good to know.\n", + "'''" ] }, { @@ -2364,25 +2393,43 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 103, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'partialmethod(func, *args, **keywords)\\n\\n\\n'" - ] - }, - "execution_count": 88, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] } ], "source": [ "'''partialmethod(func, *args, **keywords)\n", "\n", + "Return a new partialmethod descriptor \n", + "which behaves like partial except that \n", + "it is designed to be used as a method \n", + "definition rather than being directly callable.\n", + "'''\n", "\n", - "'''" + "class Cell(object):\n", + " def __init__(self):\n", + " self._alive = False\n", + " @property\n", + " def alive(self):\n", + " return self._alive\n", + " def set_state(self, state):\n", + " self._alive = bool(state)\n", + " set_alive = partialmethod(set_state, True)\n", + " set_dead = partialmethod(set_state, False)\n", + "\n", + "c = Cell()\n", + "print(c.alive) # Calls `self._alive = False` via @property\n", + "\n", + "c.set_alive() # Partial method is callable and sets the state True\n", + "print(c.alive)" ] }, { From cd2dbc293443883e2e6ef2e2b3cdd58b8ef2c9c7 Mon Sep 17 00:00:00 2001 From: Tyler Guo Date: Wed, 10 Oct 2018 13:57:35 -0700 Subject: [PATCH 10/10] Ready for PR --- README.rst | 10 +- built_in_library_tips.ipynb | 930 +++++++++++++++++++++++++----------- python_tips.ipynb | 10 +- 3 files changed, 669 insertions(+), 281 deletions(-) diff --git a/README.rst b/README.rst index f000f86..79b50a1 100644 --- a/README.rst +++ b/README.rst @@ -30,11 +30,7 @@ Built-In Libraries Tips topics: - Generators - Itertools (built-in library) - Collections (built-in library) - - Decorators - - Functools (built-in library) reduce, partial.. etc. + - Decorators (Property decorators: getters, setters, deleters) + - Functools (built-in library) reduce, partials. - Datetime (built-in library) - - Built-In Functions Tips 1(WIP): - - Built-in Functions - - + \ No newline at end of file diff --git a/built_in_library_tips.ipynb b/built_in_library_tips.ipynb index d8095e5..4adf718 100644 --- a/built_in_library_tips.ipynb +++ b/built_in_library_tips.ipynb @@ -24,7 +24,7 @@ " - [Combinatoric iterators](#combinatoric)\n", " - [Collections](#collections)\n", " - [Decorators](#decorators)\n", - " - [Setters, getters and deleters](#sgd)\n", + " - [Property Decorators: Getter, Setter, Deleter](#gsd)\n", " - [Functools](#functools)\n", " - [Datetime](#dt)" ] @@ -337,10 +337,8 @@ "source": [ "'''accumulate(iterable, func=operator.add)\n", "\n", - "Accumulate makes an iterator that returns accumulated sums\n", + "Accumulate makes an iterator that returns accumulated sums.\n", "Works similar to itertools.reduce() although only with addition\n", - "\n", - "Works as a generator.\n", "'''\n", "\n", "list(accumulate([1,2,3,4,5]))" @@ -360,6 +358,8 @@ } ], "source": [ + "# Generator\n", + "\n", "accu_onj = accumulate([1,2,3,4,5])\n", "\n", "print(next(accu_onj))" @@ -393,8 +393,6 @@ "Used for treating consecutive sequences as a single sequence.\n", "\n", "You can chain together lists, tuples, sets and strings\n", - "\n", - "Works as a generator\n", "'''\n", "\n", "print(list(chain(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl'))) " @@ -414,6 +412,8 @@ } ], "source": [ + "# Generator\n", + "\n", "chain_obj = chain(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl')\n", "\n", "print(next(chain_obj))" @@ -422,17 +422,16 @@ { "cell_type": "code", "execution_count": 15, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { - "data": { - "text/plain": [ - "'chain.from_interable(iterables)\\n\\nAlternate constructor for chain(). Gets chained inputs from a single \\niterable argument that is evaluated.\\n\\nWorks as a generator\\n'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'b', 'c', 'f', 'e', 'd', 'g', 'h', 'i', 'j', 'k', 'l']\n" + ] } ], "source": [ @@ -444,28 +443,22 @@ "Works as a generator\n", "''' \n", "\n", - "# This will raise a TypeError uncomment to run.\n", + "# Now with a single argument.\n", "\n", - "# print(list(chain.from_iterable(('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl')))" + "print(list(chain.from_iterable([('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl']))) # Now as a single argument" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['a', 'b', 'c', 'f', 'e', 'd', 'g', 'h', 'i', 'j', 'k', 'l']\n" - ] - } - ], + "outputs": [], "source": [ - "# Now with a single argument.\n", + "# This will raise a TypeError uncomment to run.\n", "\n", - "print(list(chain.from_iterable([('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl']))) # Now as a single argument" + "# print(list(chain.from_iterable(\n", + "# ('a','b','c'), {'d','e','f'}, ['g','h','i'], 'jkl')\n", + "# ))" ] }, { @@ -499,17 +492,18 @@ "evaluates to True. \n", "\n", "Stops when either the data or selectors iterables have been exhausted.\n", - "\n", - "Works as a generator.\n", "'''\n", "\n", + "# A, C, E and F correspond to 1 or True to they will return\n", "list(compress('ABCDEF', [1,0,1,0,1,1]))" ] }, { "cell_type": "code", "execution_count": 18, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", @@ -520,6 +514,8 @@ } ], "source": [ + "# Generator\n", + "\n", "compress_obj = compress('ABCDEF', [1,0,1,0,1,1])\n", "\n", "print(next(compress_obj))" @@ -582,6 +578,8 @@ } ], "source": [ + "# Generator\n", + "\n", "dropwhile_obj = dropwhile(lambda x: x<5, [1,2,3,4,5,6,7,8,9])\n", "\n", "print(next(dropwhile_obj))" @@ -642,6 +640,8 @@ } ], "source": [ + "# Generator\n", + "\n", "filterfalse_obj = filterfalse(lambda x: x%2, range(10))\n", "\n", "print(next(filterfalse_obj))" @@ -842,9 +842,9 @@ "Makes an iterator that computes the function\n", "using arguments obtained from the iterable.\n", "\n", - "\n", "Works as a generator.\n", "'''\n", + "\n", "print(list(starmap(pow, [(2,5), (3,2), (10,3)])))\n", "\n", "# similar to\n", @@ -944,18 +944,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "(, )\n" + "(, , )\n" ] } ], "source": [ "'''tee(iterable, n=2)\n", "\n", - "This one is tricky, it will take the iterable that you give it \n", + "Tee will take the iterable that you give it \n", "and return a tuple of n iterables, which you can then iterate through. \n", "'''\n", - "\n", - "b = tee((1,2,3,4))\n", + "b = tee((1,2,3,4), 3) # Created 3 iterable object with the same elements\n", "print(b)" ] }, @@ -1009,11 +1008,32 @@ "metadata": { "scrolled": true }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "for third in b[2]:\n", + " print(third)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, "outputs": [], "source": [ "# Will raise IndexError uncomment to run.\n", "\n", - "# for third in b[2]:\n", + "# for third in b[3]:\n", "# print(third)" ] }, @@ -1026,7 +1046,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 38, "metadata": {}, "outputs": [ { @@ -1058,7 +1078,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 39, "metadata": {}, "outputs": [ { @@ -1091,7 +1111,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 40, "metadata": {}, "outputs": [ { @@ -1100,7 +1120,7 @@ "[('A',), ('B',), ('C',), ('D',)]" ] }, - "execution_count": 39, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -1108,7 +1128,8 @@ "source": [ "'''Product (*iterables, repeat=1)\n", "\n", - "Product is equivalent to for loops. Repeat lets you choose how many times to nest.\n", + "Product is equivalent to for loops. \n", + "Repeat lets you choose how many times to nest.\n", "\n", "Works as a generator.\n", "'''\n", @@ -1119,7 +1140,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 41, "metadata": {}, "outputs": [ { @@ -1143,7 +1164,7 @@ " ('D', 'D')]" ] }, - "execution_count": 40, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -1154,7 +1175,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 42, "metadata": {}, "outputs": [ { @@ -1180,7 +1201,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 43, "metadata": { "scrolled": true }, @@ -1202,7 +1223,7 @@ " ('D', 'C')]" ] }, - "execution_count": 42, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -1210,7 +1231,7 @@ "source": [ "'''permutations(iterable, r=None_\n", "\n", - "Results all possible orderings, no repeated elements\n", + "Returns all permutations, no repeated elements\n", "\n", "Works as a generator.\n", "'''\n", @@ -1220,7 +1241,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 44, "metadata": {}, "outputs": [ { @@ -1246,7 +1267,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 45, "metadata": {}, "outputs": [ { @@ -1255,7 +1276,7 @@ "[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]" ] }, - "execution_count": 44, + "execution_count": 45, "metadata": {}, "output_type": "execute_result" } @@ -1263,7 +1284,8 @@ "source": [ "'''combinations(iterable, r)\n", "\n", - "results in sorted order, no repeated elements\n", + "Returns all combinations results in sorted order,\n", + "no repeated elements.\n", "\n", "Works as a generator.\n", "'''\n", @@ -1273,7 +1295,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 46, "metadata": {}, "outputs": [ { @@ -1299,7 +1321,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 47, "metadata": { "scrolled": true }, @@ -1319,7 +1341,7 @@ " ('D', 'D')]" ] }, - "execution_count": 46, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -1327,7 +1349,9 @@ "source": [ "'''combinations_with_replacement(iterable, r)\n", "\n", - "Results in sorted order, with repeated elements\n", + "Returns all combinations with replacements,\n", + "Results in sorted order\n", + "\n", "The list should return with these additional elements:\n", "[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')]\n", "\n", @@ -1339,7 +1363,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 48, "metadata": {}, "outputs": [ { @@ -1365,12 +1389,16 @@ "\n", "This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.\n", "\n", + "NOTES:
\n", + "user(dict|list|string) have lost value moving from Python2 -> Python3.\n", + "I won't be covering them.\n", + "\n", "https://docs.python.org/3/library/collections.html" ] }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 49, "metadata": {}, "outputs": [], "source": [ @@ -1386,17 +1414,22 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 50, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Color(red=255, green=255, blue=224)\n" + ] + } + ], "source": [ "'''namedtuple(typename, field_names, verbose=False, rename=False)\n", "\n", - "Returns a new tuple subclass named typename. \n", - "\n", - "The new subclass is used to create tuple-like objects that can act like\n", - "dictionaries and lists. Very good at being descriptive check \n", - "the real world application below.\n", + "Used to create tuple-like objects that act like lists\n", + "dictionaries. Very good at being descriptive check.\n", "\n", "._make creates list like tuple\n", "\n", @@ -1408,126 +1441,145 @@ "._fields Tuple of strings listing the field names. \n", "'''\n", "\n", - "Point = namedtuple('Subclass', ['x', 'y'])" + "# Real world application\n", + "\n", + "Color = namedtuple('Color', ['red','green','blue'])\n", + "banana = Color(red=255, green=255, blue=224)\n", + "\n", + "print(banana)" ] }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 51, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Subclass(x=11, y=22)\n", - "Subclass(x=11, y=22)\n" - ] - } - ], + "outputs": [], "source": [ - "# These create list like objects, iterable and indexable.\n", - "\n", - "# ._make\n", - "\n", - "print(Point(11,22))\n", - "\n", - "# Similar to\n", - "\n", - "t = [11,22]\n", - "print(Point._make(t))" + "# Functionality" ] }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "OrderedDict([('x', 11), ('y', 22)])" + "255" ] }, - "execution_count": 51, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# ._asdict()\n", + "# Indexable\n", "\n", - "Point(11,22)._asdict()" + "banana[0]" ] }, { "cell_type": "code", - "execution_count": 52, - "metadata": {}, + "execution_count": 53, + "metadata": { + "scrolled": true + }, "outputs": [ { "data": { "text/plain": [ - "Subclass(x=33, y=22)" + "255" ] }, - "execution_count": 52, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# ._replace\n", + "# Key accessable\n", "\n", - "p = Point(x=11, y=22)\n", - "p._replace(x=33)" + "# ._asdict()\n", + "\n", + "banana_dict = banana._asdict()\n", + "\n", + "banana_dict['red']" ] }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "descriptor(x=11, y=22)\n", + "descriptor(x=11, y=22)\n" + ] + } + ], + "source": [ + "# These create list like objects, iterable and indexable.\n", + "\n", + "# ._make\n", + "\n", + "something = namedtuple('descriptor', ['x', 'y'])\n", + "\n", + "print(something(11,22))\n", + "\n", + "# Similar to\n", + "\n", + "values = [11,22]\n", + "print(something._make(values))" + ] + }, + { + "cell_type": "code", + "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "('x', 'y')" + "descriptor(x=33, y=22)" ] }, - "execution_count": 53, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# ._fields\n", + "# ._replace \n", "\n", - "p._fields" + "p = something(x=11, y=22)\n", + "p._replace(x=33)" ] }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "Color(red=255, green=255, blue=224)" + "('x', 'y')" ] }, - "execution_count": 54, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# Real world application\n", - "\n", - "Color = namedtuple('Color', ['red','green','blue'])\n", - "banana = Color(red=255, green=255, blue=224)\n", + "# ._fields\n", "\n", - "banana" + "something._fields" ] }, { @@ -1539,7 +1591,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 57, "metadata": {}, "outputs": [ { @@ -1551,8 +1603,7 @@ } ], "source": [ - "'''deque\n", - "deque([iterable[, maxlen]])\n", + "'''deque([iterable[, maxlen]])\n", "\n", "Deques are a generalization of stacks and queues\n", "\n", @@ -1607,7 +1658,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 58, "metadata": {}, "outputs": [ { @@ -1628,7 +1679,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 59, "metadata": {}, "outputs": [ { @@ -1649,7 +1700,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 60, "metadata": {}, "outputs": [ { @@ -1670,7 +1721,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 61, "metadata": {}, "outputs": [ { @@ -1686,7 +1737,7 @@ "1" ] }, - "execution_count": 59, + "execution_count": 61, "metadata": {}, "output_type": "execute_result" } @@ -1702,7 +1753,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 62, "metadata": {}, "outputs": [ { @@ -1723,7 +1774,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 63, "metadata": {}, "outputs": [ { @@ -1744,7 +1795,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 64, "metadata": {}, "outputs": [ { @@ -1766,7 +1817,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 65, "metadata": {}, "outputs": [ { @@ -1788,7 +1839,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 66, "metadata": {}, "outputs": [ { @@ -1812,7 +1863,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 67, "metadata": {}, "outputs": [ { @@ -1836,7 +1887,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 68, "metadata": {}, "outputs": [ { @@ -1872,39 +1923,19 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 69, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'ChainMap(*maps)\\n\\nA ChainMap class is provided for quickly linking\\na number of mappings so they can be treated as a single unit.\\nIt is often much faster than creating a new dictionary\\nand running multiple update() calls.\\n\\nIf there a key that is the same within the ChainMap object, \\nit will assign the value to the first instance of that key.\\n'" - ] - }, - "execution_count": 67, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "'''ChainMap(*maps)\n", "\n", "A ChainMap class is provided for quickly linking\n", "a number of mappings so they can be treated as a single unit.\n", - "It is often much faster than creating a new dictionary\n", - "and running multiple update() calls.\n", "\n", "If there a key that is the same within the ChainMap object, \n", "it will assign the value to the first instance of that key.\n", - "'''" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "metadata": {}, - "outputs": [], - "source": [ + "'''\n", + "\n", "dict1 = {'a':1,'b':2,'c':3}\n", "dict2 = {'c':4,'d':5,'e':6}\n", "\n", @@ -1913,7 +1944,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 70, "metadata": {}, "outputs": [ { @@ -1922,7 +1953,7 @@ "2" ] }, - "execution_count": 69, + "execution_count": 70, "metadata": {}, "output_type": "execute_result" } @@ -1933,7 +1964,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 71, "metadata": { "scrolled": true }, @@ -1944,7 +1975,7 @@ "3" ] }, - "execution_count": 70, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -1964,7 +1995,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 72, "metadata": {}, "outputs": [ { @@ -1973,7 +2004,7 @@ "Counter({1: 3, 2: 3, 3: 3, 5: 3, 7: 3, 4: 3, 9: 3})" ] }, - "execution_count": 71, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -2003,136 +2034,141 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 73, + "metadata": {}, + "outputs": [], + "source": [ + "'''OrderedDict([items])\n", + "\n", + "An OrderedDict is a dict that remembers\n", + "the order that keys were first inserted.\n", + "'''\n", + "\n", + "ordered_dict = OrderedDict({'a':1, 'b':2, 'c':3, 'd':4})" + ] + }, + { + "cell_type": "code", + "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'OrderedDict\\n\\n'" + "OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4)])" ] }, - "execution_count": 72, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'''OrderedDict\n", - "\n", - "'''" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "DefaultDict" - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'defaultdict\\n'" - ] - }, - "execution_count": 73, + "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "'''defaultdict\n", - "'''" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Userdict" + "ordered_dict" ] }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 75, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'UserDict\\n'" - ] - }, - "execution_count": 74, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "a 1\n", + "b 2\n", + "c 3\n", + "d 4\n" + ] } ], "source": [ - "'''UserDict\n", - "'''" + "# Supports all dictionary functions.\n", + "\n", + "for k,v in ordered_dict.items():\n", + " print(k,v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Userlist" + "Defaultdict" ] }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 76, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'UserList\\n'" - ] - }, - "execution_count": 75, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "{'m': 1, 'i': 4, 's': 4, 'p': 2}\n", + "[('m', 1), ('i', 4), ('s', 4), ('p', 2)]\n" + ] } ], "source": [ - "'''UserList\n", - "'''" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Userstring" + "'''defaultdict([default_factory[, ...]])\n", + "\n", + "When a key is encountered for the first time it is assigned\n", + "the default_factory value you assign.\n", + "\n", + "Great to use when you need to count individual elements within a collection\n", + "Similar to count, but with the ability to set that default_factory.\n", + "'''\n", + "\n", + "string = 'mississippi'\n", + "\n", + "d = {}\n", + "\n", + "for letter in string:\n", + " if letter not in d:\n", + " d[letter] = 1 # For every new key we give it a default value of 1\n", + " else:\n", + " d[letter] += 1\n", + " \n", + "print(d)\n", + " \n", + "# Similar to\n", + "\n", + "default_dict2 = defaultdict(int)\n", + "for k in string:\n", + " default_dict2[k] += 1\n", + "\n", + "print(list(default_dict2.items()))" ] }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'UserString\\n'" + "[('yellow', [1, 3]), ('blue', [2, 4]), ('red', [1])]" ] }, - "execution_count": 76, + "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "'''UserString\n", - "'''" + "# Setting the default to a list.\n", + "\n", + "colors = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]\n", + "\n", + "colors_default_dict = defaultdict(list)\n", + "\n", + "for k, v in colors:\n", + " colors_default_dict[k].append(v)\n", + "\n", + "list(colors_default_dict.items())" ] }, { @@ -2149,7 +2185,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 78, "metadata": {}, "outputs": [], "source": [ @@ -2165,7 +2201,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 79, "metadata": {}, "outputs": [ { @@ -2189,7 +2225,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 80, "metadata": {}, "outputs": [], "source": [ @@ -2201,7 +2237,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 81, "metadata": {}, "outputs": [], "source": [ @@ -2212,7 +2248,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 82, "metadata": {}, "outputs": [ { @@ -2229,7 +2265,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 83, "metadata": {}, "outputs": [], "source": [ @@ -2240,7 +2276,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 84, "metadata": {}, "outputs": [ { @@ -2257,7 +2293,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 85, "metadata": {}, "outputs": [ { @@ -2301,24 +2337,363 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Setters, Getters and Deleters \n", + "Property Decorators: Getters, Setters and Deleters \n", + "\n", + "[Video that helped me a lot](https://www.youtube.com/watch?v=jCzT9XFZ5bw&t=459s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Property/Getter" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [], + "source": [ + "''' Property gives classes getter, setter and deleter functionality. \n", + "@property on its own will give getter funtionality\n", + "allowing class methods to be called like an attribute.\n", + "'''\n", + "\n", + "class Person:\n", + " def __init__(self, first, last):\n", + " self.first = first\n", + " self.last = last\n", + " \n", + " @property # This will allow me to call fullname as an attribute.\n", + " def fullname(self):\n", + " return f'{self.first} {self.last}'" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Guy Fieri'" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Guy = Person('Guy', 'Fieri')\n", + "\n", + "Guy.fullname\n", + "\n", + "# Similar to\n", + "\n", + "# Guy.fullname()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Setters" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Why are setters useful?\\n\\nThis gives us the ability to alter class attributes used by a certain class\\nmethod without forcing a method call.\\n'" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'''Why are setters useful?\n", + "\n", + "This gives us the ability to alter class attributes used by a certain class\n", + "method without forcing a method call.\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": {}, + "outputs": [], + "source": [ + "class Person:\n", + " def __init__(self, first, last):\n", + " self.first = first\n", + " self.last = last\n", + " self.email = f'{self.first}.{self.last}@flavortown.com'.lower()\n", + " \n", + " def fullname(self):\n", + " return f'{self.first} {self.last}'" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Guy\n", + "Guy Fieri\n", + "guy.fieri@flavortown.com\n" + ] + } + ], + "source": [ + "Guy = Person('Guy', 'Fieri')\n", "\n", - "[Video that helped me](https://www.youtube.com/watch?v=jCzT9XFZ5bw&t=459s)" + "# Looks good.\n", + "print(Guy.first)\n", + "print(Guy.fullname())\n", + "print(Guy.email)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "notguy\n", + "guy.fieri@flavortown.com\n" + ] + } + ], + "source": [ + "# But what if we change a first or last name?\n", + "Guy.first = 'notguy'\n", + "print(Guy.first)\n", + "print(Guy.email) # It doesn't change. So lets make it a method" + ] + }, + { + "cell_type": "code", + "execution_count": 92, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class Person:\n", + " def __init__(self, first, last):\n", + " self.first = first\n", + " self.last = last\n", + " \n", + " def email(self):\n", + " return f'{self.first}.{self.last}@flavortown.com'.lower()\n", + " \n", + " def fullname(self):\n", + " return f'{self.first} {self.last}'" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "notguy\n", + "notguy.fieri@flavortown.com\n" + ] + } + ], + "source": [ + "'''It worked!\n", + "\n", + "But, this would mean I would need to change all \n", + "instance of email into a method to update them if I \n", + "changed a first or last name.\n", + "\n", + "Now for the setters.\n", + "'''\n", + "\n", + "Guy = Person('Guy', 'Fieri')\n", + "Guy.first = 'notguy'\n", + "\n", + "print(Guy.first)\n", + "print(Guy.email()) " + ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 94, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class Person:\n", + " def __init__(self, first, last):\n", + " self.first = first\n", + " self.last = last\n", + " \n", + " @property\n", + " def email(self):\n", + " return f'{self.first}.{self.last}@flavortown.com'.lower()\n", + " \n", + " @property\n", + " def fullname(self):\n", + " return f'{self.first} {self.last}'\n", + " \n", + " @fullname.setter\n", + " def fullname(self, new_name):\n", + " first, last = new_name.split(' ')\n", + " self.first = first\n", + " self.last = last" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Guy Fieri\n", + "fieri.notguy@flavortown.com\n", + "Fieri\n" + ] + } + ], + "source": [ + "# We can now change attributes though setters from a class method.\n", + "\n", + "Guy = Person('Guy', 'Fieri')\n", + "print(Guy.fullname)\n", + "\n", + "Guy.fullname = 'Fieri Notguy'\n", + "print(Guy.email)\n", + "print(Guy.first)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Deleters" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": {}, + "outputs": [], + "source": [ + "'''Used when we want to delete things or default them to another value.\n", + "uses del\n", + "'''\n", + "\n", + "class Person:\n", + " def __init__(self, first, last):\n", + " self.first = first\n", + " self.last = last\n", + " \n", + " @property\n", + " def fullname(self):\n", + " return f'{self.first} {self.last}'\n", + " \n", + " @fullname.deleter\n", + " def fullname(self):\n", + " print('Deleted!')\n", + " self.first = None\n", + " self.last = None" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Guy Fieri\n", + "Deleted!\n", + "None\n" + ] + } + ], + "source": [ + "Guy = Person('Guy', 'Fieri')\n", + "print(Guy.fullname)\n", + "\n", + "del Guy.fullname # Deletes the values and sets them to None.\n", + "print(Guy.first)" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "outputs": [], + "source": [ + "class Person:\n", + " def __init__(self, first, last):\n", + " self.first = first\n", + " self.last = last\n", + " \n", + " @property\n", + " def fullname(self):\n", + " return f'{self.first} {self.last}'\n", + " \n", + " @fullname.deleter # Reset to default\n", + " def fullname(self):\n", + " print('Deleted!')\n", + " self.first = 'Joe'\n", + " self.last = 'Smith'" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Guy Fieri\n", + "Deleted!\n", + "Joe\n", + "Smith\n" + ] + } + ], + "source": [ + "Guy = Person('Guy', 'Fieri')\n", + "print(Guy.fullname)\n", + "\n", + "del Guy.fullname # Reset to the defaults we specified.\n", + "\n", + "print(Guy.first)\n", + "print(Guy.last)" + ] }, { "cell_type": "markdown", @@ -2334,9 +2709,20 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 100, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Functools has a lot of wrapper tools and functionality. \\nI may go over them at some point. For now I think partial and reduce\\nare very good to know.\\n'" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from functools import *\n", "\n", @@ -2355,7 +2741,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 101, "metadata": {}, "outputs": [], "source": [ @@ -2376,7 +2762,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 102, "metadata": {}, "outputs": [ { @@ -2391,6 +2777,13 @@ "freshman('Joe','Smith')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Partialmethod" + ] + }, { "cell_type": "code", "execution_count": 103, @@ -2441,7 +2834,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 104, "metadata": {}, "outputs": [ { @@ -2450,14 +2843,13 @@ "3628800" ] }, - "execution_count": 89, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "'''reduce()\n", - "Arguments/Defaults: (function, iterable, initializer=None)\n", + "'''reduce(function, iterable, initializer=None)\n", "\n", "Reduce needs to be imported in Python3.x\n", "\n", @@ -2474,7 +2866,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 105, "metadata": {}, "outputs": [ { @@ -2483,7 +2875,7 @@ "10" ] }, - "execution_count": 90, + "execution_count": 105, "metadata": {}, "output_type": "execute_result" } @@ -2496,7 +2888,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 106, "metadata": { "scrolled": true }, @@ -2507,7 +2899,7 @@ "1" ] }, - "execution_count": 91, + "execution_count": 106, "metadata": {}, "output_type": "execute_result" } @@ -2530,7 +2922,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 107, "metadata": {}, "outputs": [], "source": [ @@ -2539,16 +2931,16 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "datetime.datetime(2018, 10, 9, 16, 32, 52, 450932)" + "datetime.datetime(2018, 10, 10, 13, 56, 15, 346320)" ] }, - "execution_count": 93, + "execution_count": 108, "metadata": {}, "output_type": "execute_result" } @@ -2566,7 +2958,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 109, "metadata": { "scrolled": true }, @@ -2575,7 +2967,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Today is the 9 day of month 10, and the year is 2018!\n" + "Today is the 10 day of month 10, and the year is 2018!\n" ] } ], @@ -2601,7 +2993,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 110, "metadata": {}, "outputs": [ { @@ -2629,14 +3021,14 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 111, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Today's date is 09-10-2018. Be careful;\n", + "Today's date is 10-10-2018. Be careful;\n", "It's not a datetimeobject anymore it is a !\n" ] } @@ -2659,7 +3051,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 112, "metadata": { "scrolled": true }, diff --git a/python_tips.ipynb b/python_tips.ipynb index 0a518dd..4abfe5c 100644 --- a/python_tips.ipynb +++ b/python_tips.ipynb @@ -643,7 +643,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 24, @@ -1264,14 +1264,14 @@ "\n", "# Class\n", "\n", - "class employee():\n", + "class Employee():\n", " def __init__(self, name, job):\n", " self.name = name\n", " self.job = job\n", " \n", "# This class inherits name and job from the employee class using super().__init__\n", "\n", - "class manager(employee):\n", + "class Manager(Employee):\n", " def __init__(self, name, job, level):\n", " super().__init__(name, job)\n", " self.level = level" @@ -1294,7 +1294,7 @@ } ], "source": [ - "Todd = employee('Todd','Developer')\n", + "Todd = Employee('Todd','Developer')\n", "Todd.name" ] }, @@ -1315,7 +1315,7 @@ } ], "source": [ - "Sharon = manager('Sharon', 'Senior Developer', 'Manager')\n", + "Sharon = Manager('Sharon', 'Senior Developer', 'Manager')\n", "Sharon.level" ] },