diff --git a/02-array-seq/array-seq.ipynb b/02-array-seq/array-seq.ipynb index afdbf6b..085d4df 100644 --- a/02-array-seq/array-seq.ipynb +++ b/02-array-seq/array-seq.ipynb @@ -44,7 +44,9 @@ "outputs": [ { "data": { - "text/plain": "[36, 162, 163, 165, 8364, 164]" + "text/plain": [ + "[36, 162, 163, 165, 8364, 164]" + ] }, "execution_count": 1, "metadata": {}, @@ -75,7 +77,9 @@ "outputs": [ { "data": { - "text/plain": "[36, 162, 163, 165, 8364, 164]" + "text/plain": [ + "[36, 162, 163, 165, 8364, 164]" + ] }, "execution_count": 2, "metadata": {}, @@ -104,7 +108,9 @@ "outputs": [ { "data": { - "text/plain": "'ABC'" + "text/plain": [ + "'ABC'" + ] }, "execution_count": 3, "metadata": {}, @@ -124,7 +130,9 @@ "outputs": [ { "data": { - "text/plain": "[65, 66, 67]" + "text/plain": [ + "[65, 66, 67]" + ] }, "execution_count": 4, "metadata": {}, @@ -138,10 +146,18 @@ { "cell_type": "code", "execution_count": 5, + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "67" + "text/plain": [ + "67" + ] }, "execution_count": 5, "metadata": {}, @@ -151,13 +167,7 @@ "source": [ "codes = [last := ord(c) for c in x]\n", "last" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", @@ -173,7 +183,9 @@ "outputs": [ { "data": { - "text/plain": "[162, 163, 165, 8364, 164]" + "text/plain": [ + "[162, 163, 165, 8364, 164]" + ] }, "execution_count": 6, "metadata": {}, @@ -193,7 +205,9 @@ "outputs": [ { "data": { - "text/plain": "[162, 163, 165, 8364, 164]" + "text/plain": [ + "[162, 163, 165, 8364, 164]" + ] }, "execution_count": 7, "metadata": {}, @@ -219,7 +233,14 @@ "outputs": [ { "data": { - "text/plain": "[('black', 'S'),\n ('black', 'M'),\n ('black', 'L'),\n ('white', 'S'),\n ('white', 'M'),\n ('white', 'L')]" + "text/plain": [ + "[('black', 'S'),\n", + " ('black', 'M'),\n", + " ('black', 'L'),\n", + " ('white', 'S'),\n", + " ('white', 'M'),\n", + " ('white', 'L')]" + ] }, "execution_count": 8, "metadata": {}, @@ -264,7 +285,14 @@ "outputs": [ { "data": { - "text/plain": "[('black', 'S'),\n ('black', 'M'),\n ('black', 'L'),\n ('white', 'S'),\n ('white', 'M'),\n ('white', 'L')]" + "text/plain": [ + "[('black', 'S'),\n", + " ('black', 'M'),\n", + " ('black', 'L'),\n", + " ('white', 'S'),\n", + " ('white', 'M'),\n", + " ('white', 'L')]" + ] }, "execution_count": 10, "metadata": {}, @@ -291,7 +319,9 @@ "outputs": [ { "data": { - "text/plain": "(36, 162, 163, 165, 8364, 164)" + "text/plain": [ + "(36, 162, 163, 165, 8364, 164)" + ] }, "execution_count": 11, "metadata": {}, @@ -310,7 +340,9 @@ "outputs": [ { "data": { - "text/plain": "array('I', [36, 162, 163, 165, 8364, 164])" + "text/plain": [ + "array('I', [36, 162, 163, 165, 8364, 164])" + ] }, "execution_count": 12, "metadata": {}, @@ -358,32 +390,37 @@ }, { "cell_type": "markdown", - "source": [ - "## Tuples Are Not Just Immutable Lists" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } }, - "execution_count": 73 + "source": [ + "## Tuples Are Not Just Immutable Lists" + ] }, { "cell_type": "markdown", - "source": [ - "#### Example 2-7. Tuples used as records" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "#### Example 2-7. Tuples used as records" + ] }, { "cell_type": "code", "execution_count": 14, + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "name": "stdout", @@ -402,17 +439,17 @@ "\n", "for passport in sorted(traveler_ids):\n", " print('%s/%s' % passport)" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 15, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 15, + }, "outputs": [ { "name": "stdout", @@ -427,33 +464,35 @@ "source": [ "for country, _ in traveler_ids:\n", " print(country)" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", - "source": [ - "### Tuples as Immutable Lists" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "### Tuples as Immutable Lists" + ] }, { "cell_type": "code", "execution_count": 16, + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "True" + "text/plain": [ + "True" + ] }, "execution_count": 16, "metadata": {}, @@ -464,21 +503,23 @@ "a = (10, 'alpha', [1, 2])\n", "b = (10, 'alpha', [1, 2])\n", "a == b" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 17, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 17, + }, "outputs": [ { "data": { - "text/plain": "False" + "text/plain": [ + "False" + ] }, "execution_count": 17, "metadata": {}, @@ -488,21 +529,23 @@ "source": [ "b[-1].append(99)\n", "a == b" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 18, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 18, + }, "outputs": [ { "data": { - "text/plain": "(10, 'alpha', [1, 2, 99])" + "text/plain": [ + "(10, 'alpha', [1, 2, 99])" + ] }, "execution_count": 18, "metadata": {}, @@ -511,21 +554,23 @@ ], "source": [ "b" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 19, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 19, + }, "outputs": [ { "data": { - "text/plain": "True" + "text/plain": [ + "True" + ] }, "execution_count": 19, "metadata": {}, @@ -544,21 +589,23 @@ "tf = (10, 'alpha', (1, 2)) # Contains no mutable items\n", "tm = (10, 'alpha', [1, 2]) # Contains a mutable item (list)\n", "fixed(tf)" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 20, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 20, + }, "outputs": [ { "data": { - "text/plain": "False" + "text/plain": [ + "False" + ] }, "execution_count": 20, "metadata": {}, @@ -567,35 +614,37 @@ ], "source": [ "fixed(tm)" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", - "source": [ - "## Unpacking sequences and iterables" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "## Unpacking sequences and iterables" + ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 2, + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "33.9425" + "text/plain": [ + "33.9425" + ] }, - "execution_count": 21, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -604,44 +653,48 @@ "lax_coordinates = (33.9425, -118.408056)\n", "latitude, longitude = lax_coordinates # unpacking\n", "latitude" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 3, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 22, + }, "outputs": [ { "data": { - "text/plain": "-118.408056" + "text/plain": [ + "-118.408056" + ] }, - "execution_count": 22, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "longitude" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 23, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 23, + }, "outputs": [ { "data": { - "text/plain": "(2, 4)" + "text/plain": [ + "(2, 4)" + ] }, "execution_count": 23, "metadata": {}, @@ -650,21 +703,23 @@ ], "source": [ "divmod(20, 8)" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 24, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 24, + }, "outputs": [ { "data": { - "text/plain": "(2, 4)" + "text/plain": [ + "(2, 4)" + ] }, "execution_count": 24, "metadata": {}, @@ -674,21 +729,23 @@ "source": [ "t = (20, 8)\n", "divmod(*t)" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 25, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 25, + }, "outputs": [ { "data": { - "text/plain": "(2, 4)" + "text/plain": [ + "(2, 4)" + ] }, "execution_count": 25, "metadata": {}, @@ -698,21 +755,23 @@ "source": [ "quotient, remainder = divmod(*t)\n", "quotient, remainder" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 26, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 26, + }, "outputs": [ { "data": { - "text/plain": "'id_rsa.pub'" + "text/plain": [ + "'id_rsa.pub'" + ] }, "execution_count": 26, "metadata": {}, @@ -724,33 +783,35 @@ "\n", "_, filename = os.path.split('/home/luciano/.ssh/id_rsa.pub')\n", "filename" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", - "source": [ - "### Using * to grab excess items" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "### Using * to grab excess items" + ] }, { "cell_type": "code", "execution_count": 27, + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "(0, 1, [2, 3, 4])" + "text/plain": [ + "(0, 1, [2, 3, 4])" + ] }, "execution_count": 27, "metadata": {}, @@ -760,21 +821,23 @@ "source": [ "a, b, *rest = range(5)\n", "a, b, rest" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 28, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 28, + }, "outputs": [ { "data": { - "text/plain": "(0, 1, [2])" + "text/plain": [ + "(0, 1, [2])" + ] }, "execution_count": 28, "metadata": {}, @@ -784,21 +847,23 @@ "source": [ "a, b, *rest = range(3)\n", "a, b, rest" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 29, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 29, + }, "outputs": [ { "data": { - "text/plain": "(0, 1, [])" + "text/plain": [ + "(0, 1, [])" + ] }, "execution_count": 29, "metadata": {}, @@ -808,21 +873,23 @@ "source": [ "a, b, *rest = range(2)\n", "a, b, rest" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 30, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 30, + }, "outputs": [ { "data": { - "text/plain": "(0, [1, 2], 3, 4)" + "text/plain": [ + "(0, [1, 2], 3, 4)" + ] }, "execution_count": 30, "metadata": {}, @@ -832,21 +899,23 @@ "source": [ "a, *body, c, d = range(5)\n", "a, body, c, d" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 31, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 31, + }, "outputs": [ { "data": { - "text/plain": "([0, 1], 2, 3, 4)" + "text/plain": [ + "([0, 1], 2, 3, 4)" + ] }, "execution_count": 31, "metadata": {}, @@ -856,33 +925,35 @@ "source": [ "*head, b, c, d = range(5)\n", "head, b, c, d" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", - "source": [ - "### Unpacking with * in function calls and sequence literals" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "### Unpacking with * in function calls and sequence literals" + ] }, { "cell_type": "code", "execution_count": 32, + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "(1, 2, 3, 4, (5, 6))" + "text/plain": [ + "(1, 2, 3, 4, (5, 6))" + ] }, "execution_count": 32, "metadata": {}, @@ -895,21 +966,23 @@ "\n", "\n", "fun(*[1, 2], 3, *range(4, 7))" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 33, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 33, + }, "outputs": [ { "data": { - "text/plain": "(0, 1, 2, 3, 4)" + "text/plain": [ + "(0, 1, 2, 3, 4)" + ] }, "execution_count": 33, "metadata": {}, @@ -918,21 +991,23 @@ ], "source": [ "*range(4), 4" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 34, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 34, + }, "outputs": [ { "data": { - "text/plain": "[0, 1, 2, 3, 4]" + "text/plain": [ + "[0, 1, 2, 3, 4]" + ] }, "execution_count": 34, "metadata": {}, @@ -941,21 +1016,23 @@ ], "source": [ "[*range(4), 4]" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 35, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 35, + }, "outputs": [ { "data": { - "text/plain": "{0, 1, 2, 3, 4, 5, 6, 7}" + "text/plain": [ + "{0, 1, 2, 3, 4, 5, 6, 7}" + ] }, "execution_count": 35, "metadata": {}, @@ -964,45 +1041,45 @@ ], "source": [ "{*range(4), 4, *(5, 6, 7)}" - ], + ] + }, + { + "cell_type": "markdown", "metadata": { "collapsed": false, "pycharm": { - "name": "#%%\n" + "name": "#%% md\n" } - } - }, - { - "cell_type": "markdown", + }, "source": [ "### Nested unpacking\n", "#### Example 2-8. Unpacking nested tuples to access the longitude\n", "\n", "[02-array-seq/metro_lat_lon.py](02-array-seq/metro_lat_lon.py)" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%% md\n" - } - } + ] }, { "cell_type": "markdown", - "source": [ - "## Pattern Matching with Sequences\n", - "#### Example 2-9. Method from an imaginary Robot class" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "## Pattern Matching with Sequences\n", + "#### Example 2-9. Method from an imaginary Robot class" + ] }, { "cell_type": "code", "execution_count": 36, + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], "source": [ "# def handle_command(self, message):\n", @@ -1017,30 +1094,30 @@ "# self.leds[ident].set_color(ident, red, green, blue)\n", "# case _:\n", "# raise InvalidCommand(message)" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", - "source": [ - "#### Example 2-10. Destructuring nested tuples—requires Python ≥ 3.10.\n", - "[02-array-seq/match_lat_lon.py](02-array-seq/match_lat_lon.py)" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "#### Example 2-10. Destructuring nested tuples—requires Python ≥ 3.10.\n", + "[02-array-seq/match_lat_lon.py](02-array-seq/match_lat_lon.py)" + ] }, { "cell_type": "code", "execution_count": 37, + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "name": "stdout", @@ -1069,57 +1146,51 @@ " case [name, _, _, (lat, lon)] if lon <= 0:\n", " print(f'{name:15} | {lat:9.4f} | {lon:9.4f}')\n", "main()" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", - "source": [], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [] }, { "cell_type": "markdown", - "source": [ - "### Pattern Matching Sequences in an Interpreter\n", - "#### Example 2-11. Matching patterns without match/case.\n", - "[02-array-seq/lispy/py3.9/lis.py](02-array-seq/lispy/py3.9/lis.py)" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "### Pattern Matching Sequences in an Interpreter\n", + "#### Example 2-11. Matching patterns without match/case.\n", + "[02-array-seq/lispy/py3.9/lis.py](02-array-seq/lispy/py3.9/lis.py)" + ] }, { "cell_type": "markdown", - "source": [ - "#### Example 2-12. Pattern matching with match/case—requires Python ≥ 3.10.\n", - "[02-array-seq/lispy/py3.10/lis.py](02-array-seq/lispy/py3.10/lis.py)" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "#### Example 2-12. Pattern matching with match/case—requires Python ≥ 3.10.\n", + "[02-array-seq/lispy/py3.10/lis.py](02-array-seq/lispy/py3.10/lis.py)" + ] }, { "cell_type": "markdown", - "source": [], "metadata": { "collapsed": false - } + }, + "source": [] }, { "cell_type": "markdown", @@ -1142,7 +1213,9 @@ "outputs": [ { "data": { - "text/plain": "[10, 20]" + "text/plain": [ + "[10, 20]" + ] }, "execution_count": 38, "metadata": {}, @@ -1162,7 +1235,9 @@ "outputs": [ { "data": { - "text/plain": "[30, 40, 50, 60]" + "text/plain": [ + "[30, 40, 50, 60]" + ] }, "execution_count": 39, "metadata": {}, @@ -1180,7 +1255,9 @@ "outputs": [ { "data": { - "text/plain": "[10, 20, 30]" + "text/plain": [ + "[10, 20, 30]" + ] }, "execution_count": 40, "metadata": {}, @@ -1198,7 +1275,9 @@ "outputs": [ { "data": { - "text/plain": "[40, 50, 60]" + "text/plain": [ + "[40, 50, 60]" + ] }, "execution_count": 41, "metadata": {}, @@ -1223,7 +1302,9 @@ "outputs": [ { "data": { - "text/plain": "'bye'" + "text/plain": [ + "'bye'" + ] }, "execution_count": 42, "metadata": {}, @@ -1242,7 +1323,9 @@ "outputs": [ { "data": { - "text/plain": "'elcycib'" + "text/plain": [ + "'elcycib'" + ] }, "execution_count": 43, "metadata": {}, @@ -1260,7 +1343,9 @@ "outputs": [ { "data": { - "text/plain": "'eccb'" + "text/plain": [ + "'eccb'" + ] }, "execution_count": 44, "metadata": {}, @@ -1330,7 +1415,9 @@ "outputs": [ { "data": { - "text/plain": "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] }, "execution_count": 46, "metadata": {}, @@ -1349,7 +1436,9 @@ "outputs": [ { "data": { - "text/plain": "[0, 1, 20, 30, 5, 6, 7, 8, 9]" + "text/plain": [ + "[0, 1, 20, 30, 5, 6, 7, 8, 9]" + ] }, "execution_count": 47, "metadata": {}, @@ -1368,7 +1457,9 @@ "outputs": [ { "data": { - "text/plain": "[0, 1, 20, 30, 5, 8, 9]" + "text/plain": [ + "[0, 1, 20, 30, 5, 8, 9]" + ] }, "execution_count": 48, "metadata": {}, @@ -1387,7 +1478,9 @@ "outputs": [ { "data": { - "text/plain": "[0, 1, 20, 11, 5, 22, 9]" + "text/plain": [ + "[0, 1, 20, 11, 5, 22, 9]" + ] }, "execution_count": 49, "metadata": {}, @@ -1433,7 +1526,9 @@ "outputs": [ { "data": { - "text/plain": "[0, 1, 100, 22, 9]" + "text/plain": [ + "[0, 1, 100, 22, 9]" + ] }, "execution_count": 51, "metadata": {}, @@ -1459,7 +1554,9 @@ "outputs": [ { "data": { - "text/plain": "[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]" + "text/plain": [ + "[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]" + ] }, "execution_count": 52, "metadata": {}, @@ -1478,7 +1575,9 @@ "outputs": [ { "data": { - "text/plain": "'abcdabcdabcdabcdabcd'" + "text/plain": [ + "'abcdabcdabcdabcdabcd'" + ] }, "execution_count": 53, "metadata": {}, @@ -1510,7 +1609,9 @@ "outputs": [ { "data": { - "text/plain": "[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]" + "text/plain": [ + "[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]" + ] }, "execution_count": 54, "metadata": {}, @@ -1529,7 +1630,9 @@ "outputs": [ { "data": { - "text/plain": "[['_', '_', '_'], ['_', '_', 'X'], ['_', '_', '_']]" + "text/plain": [ + "[['_', '_', '_'], ['_', '_', 'X'], ['_', '_', '_']]" + ] }, "execution_count": 55, "metadata": {}, @@ -1555,7 +1658,9 @@ "outputs": [ { "data": { - "text/plain": "[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]" + "text/plain": [ + "[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]" + ] }, "execution_count": 56, "metadata": {}, @@ -1574,7 +1679,9 @@ "outputs": [ { "data": { - "text/plain": "[['_', '_', 'O'], ['_', '_', 'O'], ['_', '_', 'O']]" + "text/plain": [ + "[['_', '_', 'O'], ['_', '_', 'O'], ['_', '_', 'O']]" + ] }, "execution_count": 57, "metadata": {}, @@ -1600,7 +1707,9 @@ "outputs": [ { "data": { - "text/plain": "[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]" + "text/plain": [ + "[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]" + ] }, "execution_count": 58, "metadata": {}, @@ -1622,7 +1731,9 @@ "outputs": [ { "data": { - "text/plain": "[['_', '_', '_'], ['_', '_', '_'], ['X', '_', '_']]" + "text/plain": [ + "[['_', '_', '_'], ['_', '_', '_'], ['X', '_', '_']]" + ] }, "execution_count": 59, "metadata": {}, @@ -1643,7 +1754,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -1653,14 +1764,16 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 2, "metadata": {}, "outputs": [ { "data": { - "text/plain": "140694277263808" + "text/plain": [ + "137958310687616" + ] }, - "execution_count": 61, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -1672,14 +1785,16 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 3, "metadata": {}, "outputs": [ { "data": { - "text/plain": "[1, 2, 3, 1, 2, 3]" + "text/plain": [ + "[1, 2, 3, 1, 2, 3]" + ] }, - "execution_count": 62, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -1691,14 +1806,16 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 4, "metadata": {}, "outputs": [ { "data": { - "text/plain": "True" + "text/plain": [ + "True" + ] }, - "execution_count": 63, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -1724,7 +1841,9 @@ "outputs": [ { "data": { - "text/plain": "140694329335488" + "text/plain": [ + "140694329335488" + ] }, "execution_count": 65, "metadata": {}, @@ -1743,7 +1862,9 @@ "outputs": [ { "data": { - "text/plain": "False" + "text/plain": [ + "False" + ] }, "execution_count": 66, "metadata": {}, @@ -1786,15 +1907,15 @@ }, { "cell_type": "markdown", - "source": [ - "#### Example 2-17. The unexpected result: item t2 is changed and an exception is raised" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "#### Example 2-17. The unexpected result: item t2 is changed and an exception is raised" + ] }, { "cell_type": "code", @@ -1803,7 +1924,9 @@ "outputs": [ { "data": { - "text/plain": "(1, 2, [30, 40, 50, 60])" + "text/plain": [ + "(1, 2, [30, 40, 50, 60])" + ] }, "execution_count": 68, "metadata": {}, @@ -1863,7 +1986,9 @@ "outputs": [ { "data": { - "text/plain": "['apple', 'banana', 'grape', 'raspberry']" + "text/plain": [ + "['apple', 'banana', 'grape', 'raspberry']" + ] }, "execution_count": 70, "metadata": {}, @@ -1882,7 +2007,9 @@ "outputs": [ { "data": { - "text/plain": "['grape', 'raspberry', 'apple', 'banana']" + "text/plain": [ + "['grape', 'raspberry', 'apple', 'banana']" + ] }, "execution_count": 71, "metadata": {}, @@ -1900,7 +2027,9 @@ "outputs": [ { "data": { - "text/plain": "['raspberry', 'grape', 'banana', 'apple']" + "text/plain": [ + "['raspberry', 'grape', 'banana', 'apple']" + ] }, "execution_count": 72, "metadata": {}, @@ -1918,7 +2047,9 @@ "outputs": [ { "data": { - "text/plain": "['grape', 'apple', 'banana', 'raspberry']" + "text/plain": [ + "['grape', 'apple', 'banana', 'raspberry']" + ] }, "execution_count": 73, "metadata": {}, @@ -1936,7 +2067,9 @@ "outputs": [ { "data": { - "text/plain": "['raspberry', 'banana', 'grape', 'apple']" + "text/plain": [ + "['raspberry', 'banana', 'grape', 'apple']" + ] }, "execution_count": 74, "metadata": {}, @@ -1954,7 +2087,9 @@ "outputs": [ { "data": { - "text/plain": "['grape', 'raspberry', 'apple', 'banana']" + "text/plain": [ + "['grape', 'raspberry', 'apple', 'banana']" + ] }, "execution_count": 75, "metadata": {}, @@ -1972,7 +2107,9 @@ "outputs": [ { "data": { - "text/plain": "['apple', 'banana', 'grape', 'raspberry']" + "text/plain": [ + "['apple', 'banana', 'grape', 'raspberry']" + ] }, "execution_count": 76, "metadata": {}, @@ -2007,24 +2144,25 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 6, "metadata": {}, "outputs": [ { "data": { - "text/plain": "0.8190492979077034" + "text/plain": [ + "0.6624367771747223" + ] }, - "execution_count": 77, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from array import array\n", - "from random import random, seed\n", - "seed(10) # Use seed to make the output consistent\n", + "from random import random\n", "\n", - "floats = array('d', (random() for i in range(10 ** 7)))\n", + "floats = array('d', (random() for i in (range(10**7))))\n", "floats[-1]" ] }, @@ -2045,7 +2183,9 @@ "outputs": [ { "data": { - "text/plain": "0.8190492979077034" + "text/plain": [ + "0.8190492979077034" + ] }, "execution_count": 79, "metadata": {}, @@ -2068,7 +2208,9 @@ "outputs": [ { "data": { - "text/plain": "True" + "text/plain": [ + "True" + ] }, "execution_count": 80, "metadata": {}, @@ -2088,23 +2230,31 @@ }, { "cell_type": "markdown", - "source": [ - "#### Example 2-20. Handling 6 bytes memory of as 1×6, 2×3, and 3×2 views" - ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "#### Example 2-20. Handling 6 bytes memory of as 1×6, 2×3, and 3×2 views" + ] }, { "cell_type": "code", "execution_count": 81, + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "[0, 1, 2, 3, 4, 5]" + "text/plain": [ + "[0, 1, 2, 3, 4, 5]" + ] }, "execution_count": 81, "metadata": {}, @@ -2115,21 +2265,23 @@ "octets = array('B', range(6))\n", "m1 = memoryview(octets)\n", "m1.tolist()" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 82, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 82, + }, "outputs": [ { "data": { - "text/plain": "[[0, 1, 2], [3, 4, 5]]" + "text/plain": [ + "[[0, 1, 2], [3, 4, 5]]" + ] }, "execution_count": 82, "metadata": {}, @@ -2139,21 +2291,23 @@ "source": [ "m2 = m1.cast('B', [2, 3])\n", "m2.tolist()" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 83, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 83, + }, "outputs": [ { "data": { - "text/plain": "[[0, 1], [2, 3], [4, 5]]" + "text/plain": [ + "[[0, 1], [2, 3], [4, 5]]" + ] }, "execution_count": 83, "metadata": {}, @@ -2163,21 +2317,23 @@ "source": [ "m3 = m1.cast('B', [3, 2])\n", "m3.tolist()" - ], + ] + }, + { + "cell_type": "code", + "execution_count": 84, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } - }, - { - "cell_type": "code", - "execution_count": 84, + }, "outputs": [ { "data": { - "text/plain": "array('B', [0, 1, 2, 33, 22, 5])" + "text/plain": [ + "array('B', [0, 1, 2, 33, 22, 5])" + ] }, "execution_count": 84, "metadata": {}, @@ -2188,13 +2344,7 @@ "m2[1,1] = 22\n", "m3[1,1] = 33\n", "octets" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", @@ -2210,7 +2360,9 @@ "outputs": [ { "data": { - "text/plain": "5" + "text/plain": [ + "5" + ] }, "execution_count": 85, "metadata": {}, @@ -2230,7 +2382,9 @@ "outputs": [ { "data": { - "text/plain": "-2" + "text/plain": [ + "-2" + ] }, "execution_count": 86, "metadata": {}, @@ -2248,7 +2402,9 @@ "outputs": [ { "data": { - "text/plain": "[254, 255, 255, 255, 0, 0, 1, 0, 2, 0]" + "text/plain": [ + "[254, 255, 255, 255, 0, 0, 1, 0, 2, 0]" + ] }, "execution_count": 87, "metadata": {}, @@ -2267,7 +2423,9 @@ "outputs": [ { "data": { - "text/plain": "array('h', [-2, -1, 1024, 1, 2])" + "text/plain": [ + "array('h', [-2, -1, 1024, 1, 2])" + ] }, "execution_count": 88, "metadata": {}, @@ -2300,7 +2458,9 @@ "outputs": [ { "data": { - "text/plain": "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])" + "text/plain": [ + "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])" + ] }, "execution_count": 89, "metadata": {}, @@ -2321,7 +2481,9 @@ "outputs": [ { "data": { - "text/plain": "numpy.ndarray" + "text/plain": [ + "numpy.ndarray" + ] }, "execution_count": 90, "metadata": {}, @@ -2339,7 +2501,9 @@ "outputs": [ { "data": { - "text/plain": "(12,)" + "text/plain": [ + "(12,)" + ] }, "execution_count": 91, "metadata": {}, @@ -2357,7 +2521,11 @@ "outputs": [ { "data": { - "text/plain": "array([[ 0, 1, 2, 3],\n [ 4, 5, 6, 7],\n [ 8, 9, 10, 11]])" + "text/plain": [ + "array([[ 0, 1, 2, 3],\n", + " [ 4, 5, 6, 7],\n", + " [ 8, 9, 10, 11]])" + ] }, "execution_count": 92, "metadata": {}, @@ -2376,7 +2544,9 @@ "outputs": [ { "data": { - "text/plain": "array([ 8, 9, 10, 11])" + "text/plain": [ + "array([ 8, 9, 10, 11])" + ] }, "execution_count": 93, "metadata": {}, @@ -2394,7 +2564,9 @@ "outputs": [ { "data": { - "text/plain": "9" + "text/plain": [ + "9" + ] }, "execution_count": 94, "metadata": {}, @@ -2412,7 +2584,9 @@ "outputs": [ { "data": { - "text/plain": "array([1, 5, 9])" + "text/plain": [ + "array([1, 5, 9])" + ] }, "execution_count": 95, "metadata": {}, @@ -2430,7 +2604,12 @@ "outputs": [ { "data": { - "text/plain": "array([[ 0, 4, 8],\n [ 1, 5, 9],\n [ 2, 6, 10],\n [ 3, 7, 11]])" + "text/plain": [ + "array([[ 0, 4, 8],\n", + " [ 1, 5, 9],\n", + " [ 2, 6, 10],\n", + " [ 3, 7, 11]])" + ] }, "execution_count": 96, "metadata": {}, @@ -2475,7 +2654,9 @@ "outputs": [ { "data": { - "text/plain": "array([0.06078257, 0.61741189, 0.84349987])" + "text/plain": [ + "array([0.06078257, 0.61741189, 0.84349987])" + ] }, "execution_count": 99, "metadata": {}, @@ -2493,7 +2674,9 @@ "outputs": [ { "data": { - "text/plain": "array([0.03039128, 0.30870594, 0.42174994])" + "text/plain": [ + "array([0.03039128, 0.30870594, 0.42174994])" + ] }, "execution_count": 100, "metadata": {}, @@ -2512,7 +2695,9 @@ "outputs": [ { "data": { - "text/plain": "True" + "text/plain": [ + "True" + ] }, "execution_count": 101, "metadata": {}, @@ -2545,7 +2730,9 @@ "outputs": [ { "data": { - "text/plain": "memmap([0.06078257, 0.61741189, 0.84349987])" + "text/plain": [ + "memmap([0.06078257, 0.61741189, 0.84349987])" + ] }, "execution_count": 103, "metadata": {}, @@ -2572,22 +2759,24 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 2, "metadata": {}, "outputs": [ { "data": { - "text/plain": "deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" + "text/plain": [ + "deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)" + ] }, - "execution_count": 104, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "import collections\n", + "from collections import deque\n", "\n", - "dq = collections.deque(range(10), maxlen=10)\n", + "dq = deque(range(10), maxlen=10)\n", "dq" ] }, @@ -2598,7 +2787,9 @@ "outputs": [ { "data": { - "text/plain": "deque([7, 8, 9, 0, 1, 2, 3, 4, 5, 6])" + "text/plain": [ + "deque([7, 8, 9, 0, 1, 2, 3, 4, 5, 6])" + ] }, "execution_count": 105, "metadata": {}, @@ -2617,7 +2808,9 @@ "outputs": [ { "data": { - "text/plain": "deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])" + "text/plain": [ + "deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])" + ] }, "execution_count": 106, "metadata": {}, @@ -2636,7 +2829,9 @@ "outputs": [ { "data": { - "text/plain": "deque([-1, 1, 2, 3, 4, 5, 6, 7, 8, 9])" + "text/plain": [ + "deque([-1, 1, 2, 3, 4, 5, 6, 7, 8, 9])" + ] }, "execution_count": 107, "metadata": {}, @@ -2655,7 +2850,9 @@ "outputs": [ { "data": { - "text/plain": "deque([3, 4, 5, 6, 7, 8, 9, 11, 22, 33])" + "text/plain": [ + "deque([3, 4, 5, 6, 7, 8, 9, 11, 22, 33])" + ] }, "execution_count": 108, "metadata": {}, @@ -2674,7 +2871,9 @@ "outputs": [ { "data": { - "text/plain": "deque([40, 30, 20, 10, 3, 4, 5, 6, 7, 8])" + "text/plain": [ + "deque([40, 30, 20, 10, 3, 4, 5, 6, 7, 8])" + ] }, "execution_count": 109, "metadata": {}, @@ -2743,7 +2942,9 @@ "outputs": [ { "data": { - "text/plain": "[0, '1', 5, 6, '9', 14, 19, '23', 28, '28']" + "text/plain": [ + "[0, '1', 5, 6, '9', 14, 19, '23', 28, '28']" + ] }, "execution_count": 112, "metadata": {}, @@ -2763,7 +2964,9 @@ "outputs": [ { "data": { - "text/plain": "[0, '1', 14, 19, '23', 28, '28', 5, 6, '9']" + "text/plain": [ + "[0, '1', 14, 19, '23', 28, '28', 5, 6, '9']" + ] }, "execution_count": 113, "metadata": {}, @@ -2773,18 +2976,11 @@ "source": [ "sorted(l, key=str)" ] - }, - { - "cell_type": "code", - "execution_count": 113, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -2798,9 +2994,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 2 -} \ No newline at end of file +} diff --git a/03-dict-set/03-dict-set-my.ipynb b/03-dict-set/03-dict-set-my.ipynb new file mode 100644 index 0000000..00cfb2f --- /dev/null +++ b/03-dict-set/03-dict-set-my.ipynb @@ -0,0 +1,98 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "07b0ec1a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Bangladesh': 880,\n", + " 'Brazil': 55,\n", + " 'China': 86,\n", + " 'India': 91,\n", + " 'Indonesia': 62,\n", + " 'Japan': 81,\n", + " 'Nigeria': 234,\n", + " 'Pakistan': 92,\n", + " 'Russia': 7,\n", + " 'United States': 1}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dial_codes = [ # <1>\n", + " (880, 'Bangladesh'),\n", + " (55, 'Brazil'),\n", + " (86, 'China'),\n", + " (91, 'India'),\n", + " (62, 'Indonesia'),\n", + " (81, 'Japan'),\n", + " (234, 'Nigeria'),\n", + " (92, 'Pakistan'),\n", + " (7, 'Russia'),\n", + " (1, 'United States'),\n", + "]\n", + "\n", + "country_dial = {country : code for code, country in dial_codes}\n", + "country_dial" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "a62fe850", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{55: 'BRAZIL', 62: 'INDONESIA', 7: 'RUSSIA', 1: 'UNITED STATES'}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{code : country.upper() for country, code in sorted(country_dial.items()) if code < 70}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c1e2537f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/03-dict-set/03-dict-set.ipynb b/03-dict-set/03-dict-set.ipynb index 1bfc9d5..c15dc72 100644 --- a/03-dict-set/03-dict-set.ipynb +++ b/03-dict-set/03-dict-set.ipynb @@ -9,8 +9,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) \n", - "[Clang 6.0 (clang-600.0.57)]\n" + "3.13.2 | packaged by Anaconda, Inc. | (main, Feb 6 2025, 18:56:02) [GCC 11.2.0]\n" ] } ], @@ -363,7 +362,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -377,7 +376,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.2" } }, "nbformat": 4, diff --git a/06-obj-ref/06-obj-ref-my.ipynb b/06-obj-ref/06-obj-ref-my.ipynb new file mode 100644 index 0000000..e8c077e --- /dev/null +++ b/06-obj-ref/06-obj-ref-my.ipynb @@ -0,0 +1,160 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "b3d69b0c", + "metadata": {}, + "outputs": [], + "source": [ + "class Gizmo:\n", + " def __init__(self):\n", + " print(f'Gizmo id: {id(self)}')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "eab650d2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gizmo id: 139964324471744\n" + ] + } + ], + "source": [ + "x = Gizmo()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "f75f36ac", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gizmo id: 139964314373008\n" + ] + }, + { + "ename": "TypeError", + "evalue": "unsupported operand type(s) for *: 'Gizmo' and 'int'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m y = \u001b[43mGizmo\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m10\u001b[39;49m\n", + "\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for *: 'Gizmo' and 'int'" + ] + } + ], + "source": [ + "y = Gizmo() * 10" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f5e9be8f", + "metadata": {}, + "outputs": [], + "source": [ + "dir()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f3c9ed77", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[10, 20, [...]], 30]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [10, 20]\n", + "b = [a, 30]\n", + "a.append(b)\n", + "\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "23a0d99a", + "metadata": {}, + "outputs": [], + "source": [ + "a = [1, 2]\n", + "b = a" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "9393ddd4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "del a\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3a47d1a1", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/07-1class-func/07-1class-func-my.ipynb b/07-1class-func/07-1class-func-my.ipynb new file mode 100644 index 0000000..d9f9e3d --- /dev/null +++ b/07-1class-func/07-1class-func-my.ipynb @@ -0,0 +1,108 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "73e3c67e", + "metadata": {}, + "outputs": [], + "source": [ + "def factorial(n):\n", + " \"\"\"returns n!\"\"\"\n", + " return 1 if n < 2 else n * factorial(n - 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d6376226", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1405006117752879898543142606244511569936384000000000" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "factorial(42)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "675ab80f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'returns n!'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "factorial.__doc__" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "fdc84af6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "function" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(factorial)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dd1595a0", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/07-1class-func/birds.py b/07-1class-func/birds.py new file mode 100644 index 0000000..8ecbe41 --- /dev/null +++ b/07-1class-func/birds.py @@ -0,0 +1,15 @@ +class Bird: + pass + +class Duck(Bird): + def quack(self): + print('quack') + +def alert(birdie): + birdie.quack() + +def alert_duck(birdie: Duck) -> None: + birdie.quack() + +def alert_bird(birdie: Bird) -> None: + birdie.quack() diff --git a/09-closure-deco/09-closure-deco-my.ipynb b/09-closure-deco/09-closure-deco-my.ipynb new file mode 100644 index 0000000..5cc2638 --- /dev/null +++ b/09-closure-deco/09-closure-deco-my.ipynb @@ -0,0 +1,567 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "6cee2756", + "metadata": {}, + "outputs": [], + "source": [ + "def deco(func):\n", + " def inner():\n", + " print('running inner()')\n", + " return inner" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c52aaa2d", + "metadata": {}, + "outputs": [], + "source": [ + "@deco\n", + "def target():\n", + " print('running target()')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "3cc80516", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "running inner()\n" + ] + } + ], + "source": [ + "target()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "db6646aa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + ".inner()>" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "target" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "346c8343", + "metadata": {}, + "outputs": [], + "source": [ + "class Averager():\n", + " def __init__(self):\n", + " self.series = []\n", + " \n", + " def __call__(self, new_value):\n", + " self.series.append(new_value)\n", + " total = sum(self.series)\n", + " return total / len(self.series)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "bc863bf5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "avg = Averager()\n", + "avg(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "3a0b2c4c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.5" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "avg(11)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "8d022e82", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11.0" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "avg(12)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "22a8f9d5", + "metadata": {}, + "outputs": [], + "source": [ + "def make_averager():\n", + " series = []\n", + "\n", + " def averager(new_value):\n", + " series.append(new_value)\n", + " total = sum(series)\n", + " return total / len(series)\n", + " \n", + " return averager" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "9203d6b2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "avg = make_averager()\n", + "avg(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "09169a7f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.5" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "avg(11)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "c44a4383", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12.0" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "avg(15)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6eab0680", + "metadata": {}, + "outputs": [], + "source": [ + "def make_averager():\n", + " total = 0\n", + " length = 0\n", + "\n", + " def averager(new_value):\n", + " nonlocal total\n", + " nonlocal length\n", + "\n", + " total += new_value\n", + " length += 1\n", + " return total / length\n", + " \n", + " return averager" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "5c00b091", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "avg = make_averager()\n", + "avg(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "072a66a3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.5" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "avg(11)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "bdc46456", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12.0" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "avg(15)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f926e541", + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "\n", + "def clock(func):\n", + " def clocked(*args):\n", + " t0 = time.perf_counter()\n", + " result = func(*args)\n", + " elapsted = time.perf_counter() - t0\n", + " name = func.__name__\n", + " arg_str = ', '.join(repr(arg) for arg in args)\n", + " print(f'[{elapsted:0.8f}s] {name}({arg_str}) -> {result!r}')\n", + " return result\n", + " return clocked" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "15ac74f4", + "metadata": {}, + "outputs": [], + "source": [ + "@clock\n", + "def snooze(seconds):\n", + " time.sleep(seconds)\n", + "\n", + "@clock\n", + "def factorial(n):\n", + " return 1 if n < 2 else n*factorial(n-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "10bf5f1d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.12314337s] snooze(0.123) -> None\n" + ] + } + ], + "source": [ + "snooze(.123)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1cd74b55", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.00000185s] factorial(1) -> 1\n", + "[0.00015414s] factorial(2) -> 2\n", + "[0.00024095s] factorial(3) -> 6\n", + "[0.00029704s] factorial(4) -> 24\n", + "[0.00031943s] factorial(5) -> 120\n", + "[0.00033855s] factorial(6) -> 720\n" + ] + }, + { + "data": { + "text/plain": [ + "720" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "factorial(6)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "7dff9dd7", + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "import functools\n", + "\n", + "def clock(func):\n", + " @functools.wraps(func)\n", + " def clocked(*args, **kwargs):\n", + " t0 = time.perf_counter()\n", + " result = func(*args, **kwargs)\n", + " elapsed = time.perf_counter() - t0\n", + " name = func.__name__\n", + " arg_lst = [repr(arg) for arg in args]\n", + " arg_lst.extend(f'{k}={v!r}' for k, v in kwargs.items())\n", + " arg_str = ', '.join(arg_lst)\n", + " print(f'[{elapsed:0.8f}s] {name}({arg_str}) -> {result!r}')\n", + " return result\n", + " return clocked" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "8f8ddf80", + "metadata": {}, + "outputs": [], + "source": [ + "@clock\n", + "def factorial(n):\n", + " return 1 if n < 2 else n*factorial(n-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "a26d70e5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.00000045s] factorial(1) -> 1\n", + "[0.00009076s] factorial(2) -> 2\n", + "[0.00011086s] factorial(3) -> 6\n", + "[0.00012257s] factorial(4) -> 24\n", + "[0.00013247s] factorial(5) -> 120\n", + "[0.00014292s] factorial(6) -> 720\n" + ] + }, + { + "data": { + "text/plain": [ + "720" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "factorial(6)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "54570916", + "metadata": {}, + "outputs": [], + "source": [ + "@functools.cache\n", + "@clock\n", + "def fibonacci(n):\n", + " if n < 2:\n", + " return n\n", + " return fibonacci(n - 2) + fibonacci(n - 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "82312c5c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.00000069s] fibonacci(0) -> 0\n", + "[0.00000118s] fibonacci(1) -> 1\n", + "[0.00027445s] fibonacci(2) -> 1\n", + "[0.00000146s] fibonacci(3) -> 2\n", + "[0.00032200s] fibonacci(4) -> 3\n", + "[0.00000073s] fibonacci(5) -> 5\n", + "[0.00036564s] fibonacci(6) -> 8\n" + ] + }, + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fibonacci(6)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "c55780d1", + "metadata": {}, + "outputs": [], + "source": [ + "import html\n", + "\n", + "def htmlize(obj):\n", + " content = html.escape(repr(obj))\n", + " return f'
{content}
'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b3e8f075", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/11-pythonic-obj/11-pythonic-obj-my.ipynb b/11-pythonic-obj/11-pythonic-obj-my.ipynb new file mode 100644 index 0000000..2928417 --- /dev/null +++ b/11-pythonic-obj/11-pythonic-obj-my.ipynb @@ -0,0 +1,242 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "38d4dac9", + "metadata": {}, + "outputs": [], + "source": [ + "from array import array\n", + "import math" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7d937f65", + "metadata": {}, + "outputs": [], + "source": [ + "class Vector2d:\n", + " typecode = 'd'\n", + "\n", + " def __init__(self, x, y):\n", + " self.__x = float(x)\n", + " self.__y = float(y)\n", + " \n", + " @property\n", + " def x(self):\n", + " return self.__x\n", + " \n", + " @property\n", + " def y(self):\n", + " return self.__y\n", + " \n", + " def __iter__(self):\n", + " return (i for i in (self.x, self.y))\n", + " \n", + " def __hash__(self):\n", + " return hash((self.x, self.y))\n", + " \n", + " def __repr__(self):\n", + " class_name = type(self).__name__\n", + " return f'{class_name}({self.x!r}, {self.y!r})'\n", + " \n", + " def __str__(self):\n", + " return str(tuple(self))\n", + "\n", + " def __bytes__(self):\n", + " return (bytes([ord(self.typecode)]) + bytes(array(self.typecode, self)))\n", + " \n", + " def __eq__(self, other):\n", + " return tuple(self) == tuple(other)\n", + " \n", + " def __abs__(self):\n", + " return math.hypot(self.x, self.y)\n", + " \n", + " def __bool__(self):\n", + " return self.x or self.y\n", + "\n", + " def __format__(self, fmt_spec=''):\n", + " if fmt_spec.endswith('p'):\n", + " fmt_spec = fmt_spec[:-1]\n", + " coords = (abs(self), self.angle())\n", + " outer_fmt = '<{}, {}>'\n", + " else:\n", + " coords = self\n", + " outer_fmt = '({}, {})'\n", + " components = (format(c, fmt_spec) for c in coords)\n", + " return outer_fmt.format(*components)\n", + "\n", + " def angle(self):\n", + " return math.atan2(self.y, self.x)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "05ed942e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'<1.4142135623730951, 0.7853981633974483>'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "format(Vector2d(1, 1), 'p')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "b3caf933", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'<1.414e+00, 7.854e-01>'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "format(Vector2d(1, 1), '.3ep')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3b32e574", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'<1.41421, 0.78540>'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "format(Vector2d(1, 1), '0.5fp')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1d2f1449", + "metadata": {}, + "outputs": [], + "source": [ + "class Vector2d:\n", + " typecode = 'd'\n", + "\n", + " def __init__(self, x, y):\n", + " self.__x = float(x)\n", + " self.__y = float(y)\n", + " \n", + " @property\n", + " def x(self):\n", + " return self.__x\n", + " \n", + " @property\n", + " def y(self):\n", + " return self.__y\n", + " \n", + " def __iter__(self):\n", + " return (i for i in (self.x, self.y))\n", + " \n", + " def __hash__(self):\n", + " return hash((self.x, self.y))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "39d69e03", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1079245023883434373, 1994163070182233067)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v1 = Vector2d(3, 4)\n", + "v2 = Vector2d(3.1, 4.2)\n", + "hash(v1), hash(v2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67812dd2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{<__main__.Vector2d at 0x7141942456d0>, <__main__.Vector2d at 0x714195379a90>}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{v1, v2}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dade208e", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/13-protocol-abc/13-protocol-abc.ipynb b/13-protocol-abc/13-protocol-abc.ipynb new file mode 100644 index 0000000..dcc9ac1 --- /dev/null +++ b/13-protocol-abc/13-protocol-abc.ipynb @@ -0,0 +1,316 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "cb82bca5", + "metadata": {}, + "outputs": [], + "source": [ + "import collections\n", + "\n", + "Card = collections.namedtuple('Card', ['rank', 'suit'])\n", + "\n", + "class FrenckDeck:\n", + " ranks = [str(n) for n in range(2, 11)] + list('JQKA')\n", + " suits = 'spades diamonds clubs hearts'.split()\n", + "\n", + " def __init__(self):\n", + " self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]\n", + "\n", + " def __len__(self):\n", + " return len(self._cards)\n", + " \n", + " def __getitem__(self, position):\n", + " return self._cards[position]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "fbf12600", + "metadata": {}, + "outputs": [], + "source": [ + "from random import shuffle" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "5d3c4052", + "metadata": {}, + "outputs": [], + "source": [ + "def set_card(deck, position, card):\n", + " deck._cards[position] = card\n", + "\n", + "FrenckDeck.__setitem__ = set_card" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "be98d6af", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[Card(rank='2', suit='spades'),\n", + " Card(rank='3', suit='spades'),\n", + " Card(rank='4', suit='spades'),\n", + " Card(rank='5', suit='spades'),\n", + " Card(rank='6', suit='spades')]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "deck = FrenckDeck()\n", + "deck[:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "fc497234", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[Card(rank='10', suit='diamonds'),\n", + " Card(rank='7', suit='diamonds'),\n", + " Card(rank='7', suit='clubs'),\n", + " Card(rank='8', suit='diamonds'),\n", + " Card(rank='Q', suit='diamonds')]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "shuffle(deck)\n", + "deck[:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "262cae4e", + "metadata": {}, + "outputs": [], + "source": [ + "from collections import abc" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9291022d", + "metadata": {}, + "outputs": [], + "source": [ + "class FrenckDeck2(abc.MutableSequence):\n", + " ranks = [str(n) for n in range(2, 11)] + list('JQKA')\n", + " suits = 'spades diamonds clubs hearts'.split()\n", + "\n", + " def __init__(self):\n", + " self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]\n", + " \n", + " def __len__(self):\n", + " return len(self._cards)\n", + " \n", + " def __getitem__(self, index):\n", + " return self._cards[index]\n", + " \n", + " def __setitem__(self, index, value):\n", + " self._cards[index] = value\n", + "\n", + " def __delitem__(self, index):\n", + " del self._cards[index]\n", + " \n", + " def insert(self, index, value):\n", + " self._cards.insert(index, value)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "993bf882", + "metadata": {}, + "outputs": [], + "source": [ + "deck2 = FrenckDeck2()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "16572c41", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Card(rank='A', suit='hearts')" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "deck2.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "8f5aca7a", + "metadata": {}, + "outputs": [], + "source": [ + "deck2.append(deck2.pop())" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "c1183e4b", + "metadata": {}, + "outputs": [], + "source": [ + "import abc\n", + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9397e166", + "metadata": {}, + "outputs": [], + "source": [ + "class Tombola(abc.ABC):\n", + " @abc.abstractmethod\n", + " def load(self, iterable):\n", + " \"\"\"Add items from an iterable.\"\"\"\n", + "\n", + " @abc.abstractmethod\n", + " def pick(self):\n", + " \"\"\"Remove item at random, returning it.\n", + " \n", + " This method should raise `LookupError` when then instance is empty.\n", + " \"\"\"\n", + " \n", + " def loaded(self):\n", + " \"\"\"Return `True` if there's at least 1 item, `False` otherwise.\"\"\"\n", + " return bool(self.inspect())\n", + " \n", + " def inspect(self):\n", + " \"\"\"Return a sorted tuple with the items currently inside.\"\"\"\n", + " items = []\n", + " while True:\n", + " try:\n", + " items.append(self.pick())\n", + " except LookupError:\n", + " break\n", + " \n", + " self.load(items)\n", + " return tuple(items)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "ca6e8c04", + "metadata": {}, + "outputs": [], + "source": [ + "class BingoCage(Tombola):\n", + " def __init__(self, items):\n", + " self._randomizer = random.SystemRandom()\n", + " self._items = []\n", + " self.load(items)\n", + "\n", + " def load(self, items):\n", + " self._items.extend(items)\n", + " self._randomizer.shuffle(self._items)\n", + " \n", + " def pick(self):\n", + " try:\n", + " return self._items.pop()\n", + " except IndexError:\n", + " raise LookupError('pick from empty BingoCage')\n", + " \n", + " def __call__(self):\n", + " self.pick()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "25bede84", + "metadata": {}, + "outputs": [], + "source": [ + "class LottoBlower(Tombola):\n", + " def __init__(self, iterable):\n", + " self._balls = list(iterable)\n", + " \n", + " def load(self, iterable):\n", + " self._balls.extend(iterable)\n", + " \n", + " def pick(self):\n", + " try:\n", + " position = random.randrange(len(self._balls))\n", + " except ValueError:\n", + " raise LookupError('pick from empty LottoBlower')\n", + " return self._balls.pop(position)\n", + " \n", + " def loaded(self):\n", + " return bool(self._balls)\n", + " \n", + " def inspect(self):\n", + " return tuple(self._balls)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6fe4ce2f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}