diff --git a/2022/python_workshop/notebooks/02_basic_data_types.ipynb b/2022/python_workshop/notebooks/02_basic_data_types.ipynb index d9a6aa8..98b8107 100644 --- a/2022/python_workshop/notebooks/02_basic_data_types.ipynb +++ b/2022/python_workshop/notebooks/02_basic_data_types.ipynb @@ -80,9 +80,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x_int = 10, y_int = 10\n", + "type of x_int: \n", + "type of y_int: \n", + "x_initial_identity = 1716000483920\n", + "y_initial_identity = 1716000483920\n", + "are they the same? True\n", + "x_int is y_int? True\n", + "does x_int have the same value as y_int? True\n", + "assigning new value to x\n", + "x_int = 11, y_int = 10\n", + "does x_int have the same value as y_int? False\n", + "x_initial_identity; 1716000483920\n", + "x_final_identity: 1716000483952\n" + ] + } + ], "source": [ "# assign x a value and then assign x to y\n", "x_int = 10\n", @@ -154,17 +174,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Este ok\n", + "Si aici este ok\n" + ] + } + ], "source": [ "# let's try to divide by 0\n", "# we will use a try-except block\n", "\n", "try:\n", - " x = 1 / 0\n", + " x = 1\n", "except ZeroDivisionError:\n", - " print(\"you can't divide by zero!\")" + " print(\"you can't divide by zero!\")\n", + " raise ZeroDivisionError\n", + "else:\n", + " print('Este ok')\n", + "finally:\n", + " print(\"Si aici este ok\")\n" ] }, { @@ -265,12 +299,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Peter Pan is a terrible boxer.\n", + "Whenever he throws a punch, it Neverlands\n", + "Neverlands\n", + "\n", + "I was wondering why the frisbee kept getting bigger and bigger.\n", + "But then it hit me.\n", + " Ba-dum tss.\n", + "\n" + ] + } + ], "source": [ "one_liner = \"Peter Pan is a terrible boxer.\\nWhenever he throws a punch, it Neverlands\"\n", "print(one_liner)\n", + "new_str = one_liner.split(\" \")\n", + "print(new_str[-1])\n", "\n", "multi_liner = \"\"\"\n", "I was wondering why the frisbee kept getting bigger and bigger.\n", @@ -282,9 +333,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['']\n", + "Ellipsis\n" + ] + } + ], "source": [ "# get the last word of that one-liner\n", "\n", @@ -307,24 +367,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "SELECT * FROM my_table WHERE date >= '2022-07-01 20:22:07';\n", + "\n", + "\n", + "SELECT * FROM my_table WHERE date >= '2022-07-01 20:22:07';\n", + "\n" + ] + } + ], "source": [ "# replace the wildcard in the string with a value\n", "statement = \"\"\"\n", + "SELECT * FROM my_table WHERE date >= {};\n", + "\"\"\"\n", + "statement_v2 = \"\"\"\n", "SELECT * FROM my_table WHERE date >= $$start_date$$;\n", "\"\"\"\n", "start_date = \"'2022-07-01 20:22:07'\"\n", - "replaced_statement = ...\n", - "print(replaced_statement)" + "replaced_statement = statement.format(start_date)\n", + "replaced_statement_v2 = statement_v2.replace(\"$$start_date$$\", start_date)\n", + "\n", + "print(replaced_statement)\n", + "print(replaced_statement_v2)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "This is a string that needs to be split,\n", + "based in the comma character,\n", + "so it should result in three strings.\n", + "\n" + ] + } + ], "source": [ "# split the following string based on the comma\n", "s = \"\"\"\n", @@ -333,7 +424,7 @@ "so it should result in three strings.\n", "\"\"\"\n", "\n", - "split_strings = ...\n", + "split_strings = s.strip('strings')\n", "print(split_strings)" ] }, @@ -359,9 +450,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "x evaluated to False\n", + "False\n", + "True\n", + "True\n", + "False\n" + ] + } + ], "source": [ "# an empty (None) variable evaluates to False\n", "x = int()\n", @@ -370,19 +474,31 @@ " print('x evaluated to False')\n", "\n", "# what does 0 evaluate to?\n", + "zero = bool(0)\n", + "print(zero)\n", "\n", "# what does 1 evaluate to?\n", - "\n", + "one = bool(1)\n", + "print(one)\n", "# what does a random string evaluate to?\n", - "\n", + "string = bool('fjghggf')\n", + "print(string)\n", "# what does an empty string evaluate to?\n", - "\n" + "empty_string = bool('')\n", + "print(empty_string)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3.10.5 64-bit", + "display_name": "Python 3.9.12 ('base')", "language": "python", "name": "python3" }, @@ -396,12 +512,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.5" + "version": "3.9.12" }, "orig_nbformat": 4, "vscode": { "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" + "hash": "d31b83e9610685068a0fe73b54051d44dc1110027bef1c52e64b671e72faac45" } } }, diff --git a/2022/python_workshop/notebooks/03a_lists_and_tuples.ipynb b/2022/python_workshop/notebooks/03a_lists_and_tuples.ipynb index f979125..b524c94 100644 --- a/2022/python_workshop/notebooks/03a_lists_and_tuples.ipynb +++ b/2022/python_workshop/notebooks/03a_lists_and_tuples.ipynb @@ -39,9 +39,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Verstappen\n", + "\n" + ] + } + ], "source": [ "f1_drivers = [\"Leclerc\", \"Verstappen\"]\n", "print(f1_drivers[1])\n", @@ -58,9 +67,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Leclerc', 'Verstappen', 'Hamilton', 'Norris', 'Alonso', 'Bottas', 'Gasly', 'Vettel', 'Schumacher', 'Albon', 'Hulkenberg']\n" + ] + } + ], "source": [ "# using the append method - add one element\n", "f1_drivers.append(\"Hamilton\")\n", @@ -78,9 +95,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "removed Hulkenberg.\n", + "['Leclerc', 'Verstappen', 'Hamilton', 'Norris', 'Alonso', 'Bottas', 'Gasly', 'Vettel', 'Schumacher', 'Albon']\n", + "added Hulkenberg.\n", + "['Leclerc', 'Verstappen', 'Hamilton', 'Norris', 'Alonso', 'Bottas', 'Gasly', 'Vettel', 'Schumacher', 'Albon', 'Hulkenberg']\n", + "removed Hulkenberg again. won't add Hulkenberg back.\n", + "['Leclerc', 'Verstappen', 'Hamilton', 'Norris', 'Alonso', 'Bottas', 'Gasly', 'Vettel', 'Schumacher', 'Albon']\n" + ] + } + ], "source": [ "# we should probably remove Hulkenberg\n", "\n", @@ -106,9 +136,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ignore the last five drivers ['Leclerc', 'Verstappen', 'Hamilton', 'Norris', 'Alonso']\n", + "Revert the order ['Albon', 'Schumacher', 'Vettel', 'Gasly', 'Bottas', 'Alonso', 'Norris', 'Hamilton', 'Verstappen', 'Leclerc']\n", + "Print every other driver ['Leclerc', 'Hamilton', 'Alonso', 'Gasly', 'Schumacher']\n", + "['Leclerc', 'Verstappen', 'Hamilton', 'Norris', 'Alonso', 'Bottas', 'Gasly', 'Vettel', 'Schumacher', 'Albon']\n", + "Result ['Hamilton', 'Alonso', 'Gasly', 'Schumacher']\n" + ] + } + ], "source": [ "# List slicing - similar to what we have seen for strings, as strings are lists of characters.\n", "# list[m:n:p], where p is an optional parameter for step.\n", @@ -116,12 +158,13 @@ "print(\"Ignore the last five drivers {}\".format(f1_drivers[:-5]))\n", "print(\"Revert the order {}\".format(f1_drivers[::-1]))\n", "print(\"Print every other driver {}\".format(f1_drivers[::2]))\n", + "print(f1_drivers)\n", "\n", "# Can you figure out the slicing used to get the following result?\n", "# Result ['Hamilton', 'Alonso', 'Gasly', 'Schumacher']\n", "# hint: use the step parameter\n", "\n", - "print(\"Result {}\".format(...))" + "print(\"Result {}\".format(f1_drivers[2:-1:2]))" ] }, { @@ -133,7 +176,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -143,7 +186,7 @@ "# add all the teams to the list using the preferred method\n", "# the 2022 F1 teams are (for accurate results use this order!)\n", "# Ferrari, Redbull, Mercedes, McLaren, Alpine, Alfa Romeo, Alpha Tauri, Aston Martin, Haas, Williams\n", - "..." + "f1_teams.extend([\"Ferrari\", 'Redbull', 'Mercedes', 'McLaren', \"Alpine\", 'Alfa Romeo', 'Alpha Tauri', 'Aston Martin', 'Haas', 'Williams'])" ] }, { @@ -157,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -176,9 +219,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " TEAM_DRIVER TEAM_NAME\n", + "0 Leclerc Ferrari\n", + "1 Verstappen Redbull\n", + "2 Hamilton Mercedes\n", + "3 Norris McLaren\n", + "4 Alonso Alpine\n", + "5 Bottas Alfa Romeo\n", + "6 Gasly Alpha Tauri\n", + "7 Vettel Aston Martin\n", + "8 Schumacher Haas\n", + "9 Albon Williams\n" + ] + } + ], "source": [ "# create an empty dataframe, with columns for driver and team\n", "df = pd.DataFrame(columns=['TEAM_DRIVER', 'TEAM_NAME'])\n", @@ -192,9 +253,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[True, 1, 1.5, 'abc', [1, 2, 3]]\n" + ] + } + ], "source": [ "# lists also accept items of different data types\n", "\n", @@ -251,9 +320,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x1 != x2 --> {'abracadabra'} != {'c', 'r', 'b', 'd', 'a'}\n", + "\n", + "\n" + ] + } + ], "source": [ "x1 = {'abracadabra'}\n", "x2 = set('abracadabra')\n", @@ -284,9 +363,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ferrari\n", + "\n" + ] + } + ], "source": [ "a_tuple = ('Leclerc', 'Ferrari')\n", "print(a_tuple[1])\n", @@ -296,9 +384,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Leclerc',)\n", + "('Ferrari', 'Leclerc')\n" + ] + } + ], "source": [ "# similar with lists, you can slice tuples, too!\n", "print(a_tuple[:-1])\n", @@ -309,9 +406,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\python\\github\\bootcamp2022\\2022\\python_workshop\\notebooks\\03a_lists_and_tuples.ipynb Cell 20\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[39m# but cannot assign new values to the items\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m a_tuple[\u001b[39m0\u001b[39m] \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mVerstappen\u001b[39m\u001b[39m'\u001b[39m\n", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], "source": [ "# but cannot assign new values to the items\n", "a_tuple[0] = 'Verstappen'\n", @@ -328,9 +437,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "TEAM(TEAM_DRIVER='Leclerc', TEAM_NAME='Ferrari')\n", + "TEAM(TEAM_DRIVER='Verstappen', TEAM_NAME='Redbull')\n", + "TEAM(TEAM_DRIVER='Hamilton', TEAM_NAME='Mercedes')\n", + "TEAM(TEAM_DRIVER='Norris', TEAM_NAME='McLaren')\n", + "TEAM(TEAM_DRIVER='Alonso', TEAM_NAME='Alpine')\n", + "TEAM(TEAM_DRIVER='Bottas', TEAM_NAME='Alfa Romeo')\n", + "TEAM(TEAM_DRIVER='Gasly', TEAM_NAME='Alpha Tauri')\n", + "TEAM(TEAM_DRIVER='Vettel', TEAM_NAME='Aston Martin')\n", + "TEAM(TEAM_DRIVER='Schumacher', TEAM_NAME='Haas')\n", + "TEAM(TEAM_DRIVER='Albon', TEAM_NAME='Williams')\n" + ] + } + ], "source": [ "f1_tuples = df.itertuples(index=False, name=\"TEAM\")\n", "\n", @@ -338,6 +466,11 @@ "\n", "print(f1_tuples)\n", "\n", + "# t = next(f1_tuples, False)\n", + "# while t:\n", + "# print(t) \n", + "# t = next(f1_tuples, False)\n", + "\n", "# in order to get the values, we must call the next() method on the iterator\n", "while (t := next(f1_tuples, False)):\n", " print(t)" @@ -345,22 +478,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Leclerc', 'Ferrari'), ('Verstappen', 'Redbull'), ('Hamilton', 'Mercedes'), ('Norris', 'McLaren'), ('Alonso', 'Alpine'), ('Bottas', 'Alfa Romeo'), ('Gasly', 'Alpha Tauri'), ('Vettel', 'Aston Martin'), ('Schumacher', 'Haas'), ('Albon', 'Williams')]\n" + ] + } + ], "source": [ "# what if I want to store these tuples somewhere?\n", "# typles no longer named for this example\n", "\n", "competition_teams = list(df.itertuples(index=False, name=None))\n", - "print(competition_teams)" + "print(competition_teams)\n", + "driver_numbers = [16, 1, 44, 4, 14, 77, 10, 5, 47, 23]\n", + "\n", + " \n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Leclerc', 'Ferrari', 0), ('Verstappen', 'Redbull', 1), ('Hamilton', 'Mercedes', 2), ('Norris', 'McLaren', 3), ('Alonso', 'Alpine', 4), ('Bottas', 'Alfa Romeo', 5), ('Gasly', 'Alpha Tauri', 6), ('Vettel', 'Aston Martin', 7), ('Schumacher', 'Haas', 8), ('Albon', 'Williams', 9)]\n" + ] + } + ], "source": [ "# let's try to add the driver number for each tuple, by unpacking\n", "\n", @@ -371,13 +524,14 @@ "# for each team in the list\n", "for team in competition_teams:\n", " # get the index of the tuple\n", - " index_in_list = ...\n", - " # using the index, get the driver number\n", - " driver_number = ...\n", - " # create a new tuple by unpacking the team tuple and adding the driver number\n", - " new_tuple = ...\n", - " # add the new tuple to the updated list\n", - " updated_list...\n", + " # print(team)\n", + " index_in_list = competition_teams.index(team)\n", + " # # using the index, get the driver number\n", + " driver_number = index_in_list \n", + " # # create a new tuple by unpacking the team tuple and adding the driver number\n", + " new_tuple = (*team, driver_number)\n", + " # # add the new tuple to the updated list\n", + " updated_list.append(new_tuple)\n", "\n", "# update the list\n", "competition_teams = updated_list\n", @@ -444,7 +598,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3.10.5 64-bit", + "display_name": "Python 3.9.12 ('base')", "language": "python", "name": "python3" }, @@ -458,12 +612,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.5" + "version": "3.9.12" }, "orig_nbformat": 4, "vscode": { "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" + "hash": "d31b83e9610685068a0fe73b54051d44dc1110027bef1c52e64b671e72faac45" } } }, diff --git a/2022/python_workshop/notebooks/03b_dictionaries.ipynb b/2022/python_workshop/notebooks/03b_dictionaries.ipynb index e9fa900..78c68f0 100644 --- a/2022/python_workshop/notebooks/03b_dictionaries.ipynb +++ b/2022/python_workshop/notebooks/03b_dictionaries.ipynb @@ -29,7 +29,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -82,9 +82,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(f1_teams)" ] @@ -103,18 +114,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "retired\n", + "dict_items([('Sainz', 'Ferrari'), ('Leclerc', 'Ferrari'), ('Ricciardo', 'McLaren'), ('Russel', 'Mercedes'), ('Norris', 'McLaren'), ('Hamilton', 'Mercedes'), ('Perez', 'Redbull'), ('Verstappen', 'Redbull')])\n" + ] + } + ], "source": [ - "f1_teams['Raikkonen']" + "# f1_teams['Raikkonen']\n", + "print(f1_teams.get('Raikkonen', 'retired'))\n", + "print(f1_teams.items())" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added Kimi! {'Sainz': 'Ferrari', 'Leclerc': 'Ferrari', 'Ricciardo': 'McLaren', 'Russel': 'Mercedes', 'Norris': 'McLaren', 'Hamilton': 'Mercedes', 'Perez': 'Redbull', 'Verstappen': 'Redbull', 'Raikkonen': 'Ferrari'} \n", + "\n", + "Kimi has moved! {'Sainz': 'Ferrari', 'Leclerc': 'Ferrari', 'Ricciardo': 'McLaren', 'Russel': 'Mercedes', 'Norris': 'McLaren', 'Hamilton': 'Mercedes', 'Perez': 'Redbull', 'Verstappen': 'Redbull', 'Raikkonen': 'Alfa Romeo'} \n", + "\n", + "Kimi has retired! {'Sainz': 'Ferrari', 'Leclerc': 'Ferrari', 'Ricciardo': 'McLaren', 'Russel': 'Mercedes', 'Norris': 'McLaren', 'Hamilton': 'Mercedes', 'Perez': 'Redbull', 'Verstappen': 'Redbull'} \n", + "\n", + "Different kind of retirement {'Sainz': 'Ferrari', 'Leclerc': 'Ferrari', 'Verstappen': 'Redbull', 'Ricciardo': 'McLaren', 'Russel': 'Mercedes', 'Norris': 'McLaren', 'Hamilton': 'Mercedes', 'Perez': 'Redbull'}\n" + ] + } + ], "source": [ "# add a new entry\n", "f1_teams['Raikkonen'] = 'Ferrari'\n", @@ -148,9 +184,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{: 1, : 2, : 3}\n" + ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "d = {int: 1, float: 2, bool: 3}\n", "print(d)\n", @@ -172,9 +226,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "unhashable type: 'list'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\python\\github\\bootcamp2022\\2022\\python_workshop\\notebooks\\03b_dictionaries.ipynb Cell 11\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[39m# let's find out!\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m d \u001b[39m=\u001b[39m {\n\u001b[0;32m 3\u001b[0m [\u001b[39m1\u001b[39m, \u001b[39m1\u001b[39m]: \u001b[39m'\u001b[39m\u001b[39ma\u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m 4\u001b[0m }\n", + "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'" + ] + } + ], "source": [ "# let's find out!\n", "d = {\n", @@ -199,9 +265,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-8752291807205467963\n" + ] + }, + { + "ename": "TypeError", + "evalue": "unhashable type: 'list'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\python\\github\\bootcamp2022\\2022\\python_workshop\\notebooks\\03b_dictionaries.ipynb Cell 13\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mhash\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mhello\u001b[39m\u001b[39m\"\u001b[39m))\n\u001b[1;32m----> 2\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mhash\u001b[39;49m([\u001b[39m1\u001b[39;49m, \u001b[39m2\u001b[39;49m]))\n", + "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'" + ] + } + ], "source": [ "print(hash(\"hello\"))\n", "print(hash([1, 2]))" @@ -256,9 +341,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Leclerc': 'Ferrari', 'Verstappen': 'Redbull', 'Hamilton': 'Mercedes', 'Norris': 'McLaren', 'Alonso': 'Alpine', 'Bottas': 'Alfa Romeo', 'Gasly': 'AlphaTauri', 'Vettel': 'Aston Martin', 'Schumacher': 'Haas', 'Albon': 'Williams'}\n" + ] + } + ], "source": [ "drivers = ['Leclerc', 'Verstappen', 'Hamilton', 'Norris', 'Alonso', 'Bottas', 'Gasly', 'Vettel', 'Schumacher', 'Albon']\n", "teams = ['Ferrari', 'Redbull', 'Mercedes', 'McLaren', 'Alpine', 'Alfa Romeo', 'AlphaTauri', 'Aston Martin', 'Haas', 'Williams']\n", @@ -277,9 +370,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Leclerc': 'Ferrari', 'Verstappen': 'Redbull', 'Hamilton': 'Mercedes', 'Norris': 'McLaren', 'Alonso': 'Alpine', 'Bottas': 'Alfa Romeo', 'Gasly': 'AlphaTauri', 'Vettel': 'Aston Martin', 'Schumacher': 'Haas', 'Albon': 'Williams'}\n" + ] + } + ], "source": [ "new_dict = {\n", " driver: team for driver, team in zip(drivers, teams)\n", @@ -296,31 +397,68 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Sainz': 'Ferrari', 'Leclerc': 'Ferrari', 'Verstappen': 'Redbull', 'Ricciardo': 'McLaren', 'Russel': 'Mercedes', 'Norris': 'McLaren', 'Hamilton': 'Mercedes', 'Perez': 'Redbull', 'Alonso': 'Alpine', 'Ocon': 'Alpine', 'Zhou': 'Alfa Romeo', 'Bottas': 'Alfa Romeo', 'Schumacher': 'Haas', 'Magnussen': 'Haas', 'Tsunoda': 'AlphaTauri', 'Gasly': 'AlphaTauri', 'Vettel': 'Aston Martin', 'Stroll': 'Aston Martin', 'Albon': 'Williams', 'Latifi': 'Williams'}\n" + ] + } + ], "source": [ - "# combine the two dictionaries f1_teams and f1_more_teams\n" + "# combine the two dictionaries f1_teams and f1_more_teams\n", + "f1_teams.update(f1_more_teams)\n", + "print(f1_teams)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Ricciardo', 'Norris']" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# iterate through f1_teams and print only the McLaren drivers (just the drivers)\n" + "# iterate through f1_teams and print only the McLaren drivers (just the drivers)\n", + "[driver for driver, car in f1_teams.items() if car == \"McLaren\"]\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{16: ('Sainz', 'Ferrari'), 55: ('Leclerc', 'Ferrari'), 1: ('Verstappen', 'Redbull'), 11: ('Ricciardo', 'McLaren'), 44: ('Russel', 'Mercedes'), 63: ('Norris', 'McLaren'), 4: ('Hamilton', 'Mercedes'), 3: ('Perez', 'Redbull'), 14: ('Alonso', 'Alpine'), 31: ('Ocon', 'Alpine'), 24: ('Zhou', 'Alfa Romeo'), 77: ('Bottas', 'Alfa Romeo'), 47: ('Schumacher', 'Haas'), 20: ('Magnussen', 'Haas'), 22: ('Tsunoda', 'AlphaTauri'), 10: ('Gasly', 'AlphaTauri'), 5: ('Vettel', 'Aston Martin'), 18: ('Stroll', 'Aston Martin'), 23: ('Albon', 'Williams'), 6: ('Latifi', 'Williams')} \n", + "\n", + "\n", + "{'Ferrari': (55, 'Leclerc'), 'Redbull': (3, 'Perez'), 'McLaren': (63, 'Norris'), 'Mercedes': (4, 'Hamilton'), 'Alpine': (31, 'Ocon'), 'Alfa Romeo': (77, 'Bottas'), 'Haas': (20, 'Magnussen'), 'AlphaTauri': (10, 'Gasly'), 'Aston Martin': (18, 'Stroll'), 'Williams': (6, 'Latifi')}\n" + ] + } + ], "source": [ "# given the list of driver numbers, assign each driver their corresponding number\n", "f1_driver_numbers = [16, 55, 1, 11, 44, 63, 4, 3, 14, 31, 24, 77, 47, 20, 22, 10, 5, 18, 23, 6]\n", + "tuple_lst = ()\n", "\n", + "new_dict_1 = {numb: team for numb, team in zip(f1_driver_numbers, f1_teams.items())}\n", + "print(new_dict_1, '\\n\\n')\n", + "print({key: team_numb for key, team_numb in zip(f1_teams.values(), zip(f1_driver_numbers, f1_teams.keys()))})\n", "# expected result:\n", "# {16: ('Leclerc', 'Ferrari'),\n", "# 55: ('Sainz', 'Ferrari'),\n", @@ -357,9 +495,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a: {0: [1, 2, 3, [4]]} \n", + "b: {0: [1, 2, 3, [4]]}\n" + ] + } + ], "source": [ "# shallow copy\n", "a = {0: [1, 2, 3]}\n", @@ -371,9 +518,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a: {0: [1, 2, 3, [4]]} \n", + "c: {0: [1, 2, 3, [4]]} \n", + "d: {0: [1, 2, 3, [4]]}\n" + ] + } + ], "source": [ "# deep copy\n", "import copy\n", @@ -387,9 +544,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a: {0: [1, 2, 3, [4], 5]} \n", + "c: {0: [1, 2, 3, [4]]} \n", + "d: {0: [1, 2, 3, [4], 5]}\n" + ] + } + ], "source": [ "a[0].append(5)\n", "print(\"a:\", a, \"\\nc:\", c, \"\\nd:\", d)" @@ -405,9 +572,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'pepper': 0.2, 'onion': 0.55, 'apple': 0.4, 'orange': 0.35}\n", + "pepper -> 0.2\n", + "onion -> 0.55\n", + "apple -> 0.4\n", + "orange -> 0.35\n" + ] + } + ], "source": [ "fruit_prices = {'apple': 0.40, 'orange': 0.35}\n", "vegetable_prices = {'pepper': 0.20, 'onion': 0.55}\n", @@ -428,9 +607,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 0.4)\n", + "('orange', 0.35)\n", + "('banana', 0.25)\n", + "('pepper', 0.2)\n", + "('onion', 0.55)\n", + "('tomato', 0.42)\n" + ] + } + ], "source": [ "from itertools import chain\n", "\n", @@ -443,7 +635,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3.8.0 64-bit", + "display_name": "Python 3.9.12 ('base')", "language": "python", "name": "python3" }, @@ -457,12 +649,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.9.12" }, "orig_nbformat": 4, "vscode": { "interpreter": { - "hash": "83bca74902d8d95dc0f4d5ef6f135f4aa78da41c0c8e870768b6659e568e6a46" + "hash": "d31b83e9610685068a0fe73b54051d44dc1110027bef1c52e64b671e72faac45" } } }, diff --git a/2022/python_workshop/notebooks/06_generators.ipynb b/2022/python_workshop/notebooks/06_generators.ipynb index 359ac7b..54e953e 100644 --- a/2022/python_workshop/notebooks/06_generators.ipynb +++ b/2022/python_workshop/notebooks/06_generators.ipynb @@ -34,9 +34,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Row count is 5001\n" + ] + } + ], "source": [ "def csv_reader(file_name):\n", " file = open(file_name)\n", @@ -48,7 +56,8 @@ "row_count = 0\n", "\n", "# hint: we need to iterate through csv_content and increment the row count; how can we do that?\n", - "# ...\n", + "for _ in csv_content:\n", + " row_count += 1\n", "\n", "print(f\"Row count is {row_count}\")\n" ] @@ -69,9 +78,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "uuid,date,name,departure,arrival,ticket_price\n", + "\n", + "Row count is 5001\n" + ] + } + ], "source": [ "def csv_reader(file_name):\n", " for row in open(file_name, \"r\"):\n", @@ -81,7 +100,9 @@ "row_count = 0\n", "\n", "# hint: we need to iterate through csv_content and increment the row count; how can we do that?\n", - "# ...\n", + "while (t:= next(csv_content, False)):\n", + " row_count += 1\n", + "\n", "\n", "print(f\"Row count is {row_count}\")" ] @@ -111,9 +132,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a = range(5)\n", "list(a)" @@ -128,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -154,14 +186,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n" + ] + } + ], "source": [ "gen = infinite_sequence()\n", "\n", "print(next(gen))\n", "print(next(gen))\n", + "print(next(gen))\n", + "print(next(gen))\n", + "print(next(gen))\n", + "print(next(gen))\n", + "print(next(gen))\n", "print(next(gen))" ] }, @@ -189,15 +241,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 4, 9, 16]\n", + " at 0x000002AD995823C0>\n", + "0\n", + "1\n", + "4\n", + "9\n", + "16\n" + ] + }, + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\python\\github\\bootcamp2022\\2022\\python_workshop\\notebooks\\06_generators.ipynb Cell 14\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mnext\u001b[39m(generator_expression))\n\u001b[0;32m 10\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mnext\u001b[39m(generator_expression))\n\u001b[1;32m---> 11\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mnext\u001b[39;49m(generator_expression))\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], "source": [ "list_comprehension = [num**2 for num in range(5)]\n", "generator_expression = (num**2 for num in range(5))\n", "\n", "print(list_comprehension)\n", - "print(generator_expression)" + "print(generator_expression)\n", + "print(next(generator_expression))\n", + "print(next(generator_expression))\n", + "print(next(generator_expression))\n", + "print(next(generator_expression))\n", + "print(next(generator_expression))\n", + "print(next(generator_expression))\n", + "\n", + "\n" ] }, { @@ -285,117 +370,66 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " at 0x000002AD995862E0>\n", + "['uuid', 'date', 'name', 'departure', 'arrival', 'ticket_price']\n", + "The average ticket price: $44.714285714285715\n" + ] + } + ], "source": [ "# we will use mean on order to get the average price\n", "from statistics import mean\n", "\n", + "airport_log = open('./resources/airport_log.csv', 'r')\n", "# generate an iterator for the lines in the file\n", - "lines = ...\n", + "lines = (i for i in airport_log)\n", "\n", "# split each line into a list and put the values into an iteratos\n", - "list_line = ...\n", - "\n", + "list_line = (i.rstrip().split(',') for i in lines)\n", + "print(list_line)\n", "# use the next() to store the column names into a list\n", - "cols = ...\n", + "cols = next(list_line)\n", + "print(cols)\n", "\n", "# create dictionaries and unite them with zip()\n", + "\n", + "# while (l:= next(list_line, False)):\n", + "# lst_of_values.append(l)\n", + "\n", "# the keys are the column names stored in cols\n", "# the values are the rows is list form, list_line\n", - "airport_logs_dicts = ...\n", + "airport_logs_dicts = (dict(zip(cols, data)) for data in list_line)\n", "\n", + "# while (t:= next(airport_logs_dicts), False):\n", + "# print(t)\n", "# filter the rows\n", "# we are interested in tickets from CLJ to AMS\n", "clj_ams_prices = (\n", " int(airport_logs_dict[\"ticket_price\"])\n", " for airport_logs_dict in airport_logs_dicts\n", - " if ( ... )\n", + " if ( airport_logs_dict['departure'] == 'CLJ' and airport_logs_dict['arrival'] == 'AMS' )\n", ")\n", "\n", "# for testing purposes - check all the prices - comment this after testing\n", - "while (i := next(clj_ams_prices, False)):\n", - " print(i)\n", + "# while (i := next(clj_ams_prices, False)):\n", + "# print(i)\n", "\n", "# uncomment this after checking all the prices\n", - "# avg_ticket_price = mean(clj_ams_prices)\n", - "# print(f\"The average ticket price: ${avg_ticket_price}\")\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Generator vs List vs Tuple" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import sys\n", - "\n", - "a_list = []\n", - "for i in range(1, 1):\n", - " a_list.append(i)\n", - "\n", - "tup = tuple(a_list)\n", - "gen = (x for x in a_list)\n", - "\n", - "print(type(a_list))\n", - "print(type(tup))\n", - "print(type(gen))\n", - "\n", - "print('size of list is', sys.getsizeof(a_list))\n", - "print('size of tup is', sys.getsizeof(tup))\n", - "print('size of gen is', sys.getsizeof(gen))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "a_list = []\n", - "for i in range(1, 3):\n", - " a_list.append(i)\n", - "\n", - "tup = tuple(a_list)\n", - "gen = (x for x in a_list)\n", - "\n", - "print('gen')\n", - "for x in gen:\n", - " print(x)\n", - "\n", - "print('tup')\n", - "for x in tup:\n", - " print(x)\n", - "\n", - "print('list')\n", - "for x in a_list:\n", - " print(x)\n", - "\n", - "print('gen')\n", - "for x in gen:\n", - " print(x)\n", - "\n", - "print('tup')\n", - "for x in tup:\n", - " print(x)\n", - "\n", - "print('list')\n", - "for x in a_list:\n", - " print(x)" + "avg_ticket_price = mean(clj_ams_prices)\n", + "print(f\"The average ticket price: ${avg_ticket_price}\")\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3.10.5 64-bit", + "display_name": "Python 3.9.12 ('base')", "language": "python", "name": "python3" }, @@ -409,12 +443,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.5" + "version": "3.9.12" }, "orig_nbformat": 4, "vscode": { "interpreter": { - "hash": "98590ff4fe04c8543246b2a01debd3de3c5ca9b666f43f1fa87d5110c692004c" + "hash": "d31b83e9610685068a0fe73b54051d44dc1110027bef1c52e64b671e72faac45" } } }, diff --git a/2022/python_workshop/notebooks/07_decorators.ipynb b/2022/python_workshop/notebooks/07_decorators.ipynb index 663c8f6..aa2b619 100644 --- a/2022/python_workshop/notebooks/07_decorators.ipynb +++ b/2022/python_workshop/notebooks/07_decorators.ipynb @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -53,9 +53,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'Sup, my old friend\n" + ] + } + ], "source": [ "say_hello_to_my_little_friend(greeter)" ] @@ -79,9 +87,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Verse: If I was an astronaut, I'd be floating in mid-air\n", + "Verse: And a broken heart would just belong\n", + "Bridge: Gravity keeps pulling me down\n", + "Bridge: As long as you're on the ground, I'll stick around\n", + "Chorus: I'm up in space, man\n", + "Chorus: Up in space, man\n", + "Outro: I've searched around the universe\n", + "Outro: Been down some black holes\n" + ] + } + ], "source": [ "def song():\n", " print(\"Verse: If I was an astronaut, I'd be floating in mid-air\")\n", @@ -103,6 +126,9 @@ " chorus()\n", " outro()\n", "\n", + "song()\n", + "\n", + "\n", "# what happens when you call the song() function?\n", "# does the order in which the inner functions are defined matter?" ] @@ -119,7 +145,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -144,7 +170,7 @@ " elif part == \"outro\":\n", " return outro\n", " else:\n", - " return None" + " return None\n" ] }, { @@ -156,9 +182,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Bridge: Gravity keeps pulling me down\n", + "Bridge: As long as you're on the ground, I'll stick around\n" + ] + } + ], "source": [ "bridge = song(\"bridge\")\n", "\n", @@ -170,7 +206,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -180,12 +216,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Bridge: Gravity keeps pulling me down\n", + "Bridge: As long as you're on the ground, I'll stick around\n" + ] + } + ], "source": [ "# does the bridge() function still work?\n", - "bridge()" + "bridge()\n" ] }, { @@ -203,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -235,9 +280,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Something happening before the greeting.\n", + "Bună ziua.\n", + "Something happening after the greeting.\n" + ] + } + ], "source": [ "decorated_greeter()" ] @@ -257,16 +312,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Something happening before the greeting.\n", + "on lunch break, bye\n", + "Something happening after the greeting.\n" + ] + } + ], "source": [ "from datetime import datetime\n", "\n", "def greeter_decorator(func):\n", " def wrapper():\n", " print(\"Something happening before the greeting.\")\n", - " if datetime.now().hour == 13:\n", + " if datetime.now().hour == 12:\n", " print(\"on lunch break, bye\")\n", " else:\n", " func()\n", @@ -295,7 +360,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -306,9 +371,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Something happening before the greeting.\n", + "on lunch break, bye\n", + "Something happening after the greeting.\n" + ] + } + ], "source": [ "# recreate the anon_greeter(), this time decorated\n", "\n", @@ -334,7 +409,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -346,11 +421,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Something happening before the greeting.\n", + "on lunch break, bye\n", + "Something happening after the greeting.\n" + ] + } + ], "source": [ - "greeter(\"my old friend\")" + "greeter('my old friend')" ] }, { @@ -370,7 +455,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -387,7 +472,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ @@ -399,9 +484,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Something happening before the greeting.\n", + "Bună ziua, my old friend\n", + "Something happening after the greeting.\n" + ] + } + ], "source": [ "greeter(\"my old friend\")" ] @@ -417,13 +512,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I like repeating myself.\n", + "I like repeating myself.\n", + "None\n" + ] + } + ], "source": [ "## call the function twice\n", "from resources.methods.decorators import do_twice\n", "\n", + "@do_twice\n", "def repeat_after_me():\n", " print(\"I like repeating myself.\")\n", "\n", @@ -433,13 +539,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished 'waste_some_time' in 0.9532 seconds\n" + ] + } + ], "source": [ "## use a timer on this function\n", - "from ... import ...\n", + "from resources.methods.decorators import timer\n", "\n", + "@timer\n", "def waste_some_time(num_times):\n", " for _ in range(num_times):\n", " sum([i**2 for i in range(10000)])\n", @@ -449,15 +564,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished 'waste_some_time' in 0.9941 seconds\n" + ] + } + ], "source": [ "## use another decorator to slow down the above function\n", "## can you use multiple decorators on the same function?\n", "## does the order of the decorators matter?\n", - "from ... import ....\n", + "from resources.methods.decorators import slow_down_1sec, timer\n", "\n", + "@slow_down_1sec\n", + "@timer\n", "def waste_some_time(num_times):\n", " for _ in range(num_times):\n", " sum([i**2 for i in range(10000)])\n", @@ -477,7 +602,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3.10.5 64-bit", + "display_name": "Python 3.9.12 ('base')", "language": "python", "name": "python3" }, @@ -491,12 +616,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.5" + "version": "3.9.12" }, "orig_nbformat": 4, "vscode": { "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" + "hash": "d31b83e9610685068a0fe73b54051d44dc1110027bef1c52e64b671e72faac45" } } }, diff --git a/2022/python_workshop/notebooks/module_1.py b/2022/python_workshop/notebooks/module_1.py index f673107..a60bbc5 100644 --- a/2022/python_workshop/notebooks/module_1.py +++ b/2022/python_workshop/notebooks/module_1.py @@ -8,4 +8,3 @@ c = 3 - diff --git a/2022/python_workshop/notebooks/resources/methods/decorators.py b/2022/python_workshop/notebooks/resources/methods/decorators.py index c4ec33c..ff09052 100644 --- a/2022/python_workshop/notebooks/resources/methods/decorators.py +++ b/2022/python_workshop/notebooks/resources/methods/decorators.py @@ -7,8 +7,9 @@ def do_twice(func): @functools.wraps(func) def wrapper_do_twice(*args, **kwargs): - .... - return .... + func() + return func() + return wrapper_do_twice @@ -19,8 +20,8 @@ def timer(func): @functools.wraps(func) def wrapper_timer(*args, **kwargs): start = time.perf_counter() - value = ... - end = ... + value = func(*args, **kwargs) + end = time.perf_counter() run_time = end - start print(f"Finished {func.__name__!r} in {run_time:.4f} seconds") return value @@ -33,7 +34,7 @@ def slow_down_1sec(func): @functools.wraps(func) def wrapper_slow_down(*args, **kwargs): - ... + time.sleep(5) return func(*args, **kwargs) return wrapper_slow_down diff --git a/2022/python_workshop/train.ipynb b/2022/python_workshop/train.ipynb new file mode 100644 index 0000000..f342fc5 --- /dev/null +++ b/2022/python_workshop/train.ipynb @@ -0,0 +1,130 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "dict_1 = {'car': 'Toyota', 'model': ['Camry'], 'color': ['red']}\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "Shape of passed values is (3, 1), indices imply (3, 2)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\python\\github\\bootcamp2022\\2022\\python_workshop\\train.ipynb Cell 3\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m df \u001b[39m=\u001b[39m pd\u001b[39m.\u001b[39;49mDataFrame(data\u001b[39m=\u001b[39;49m[\u001b[39m'\u001b[39;49m\u001b[39mToyota\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39mHonda\u001b[39;49m\u001b[39m'\u001b[39;49m, [\u001b[39m'\u001b[39;49m\u001b[39mCamry\u001b[39;49m\u001b[39m'\u001b[39;49m]], columns\u001b[39m=\u001b[39;49m[\u001b[39m'\u001b[39;49m\u001b[39mcar\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39mmodel\u001b[39;49m\u001b[39m'\u001b[39;49m])\n", + "File \u001b[1;32mc:\\programs\\miniconda3\\lib\\site-packages\\pandas\\core\\frame.py:737\u001b[0m, in \u001b[0;36mDataFrame.__init__\u001b[1;34m(self, data, index, columns, dtype, copy)\u001b[0m\n\u001b[0;32m 729\u001b[0m mgr \u001b[39m=\u001b[39m arrays_to_mgr(\n\u001b[0;32m 730\u001b[0m arrays,\n\u001b[0;32m 731\u001b[0m columns,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 734\u001b[0m typ\u001b[39m=\u001b[39mmanager,\n\u001b[0;32m 735\u001b[0m )\n\u001b[0;32m 736\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m--> 737\u001b[0m mgr \u001b[39m=\u001b[39m ndarray_to_mgr(\n\u001b[0;32m 738\u001b[0m data,\n\u001b[0;32m 739\u001b[0m index,\n\u001b[0;32m 740\u001b[0m columns,\n\u001b[0;32m 741\u001b[0m dtype\u001b[39m=\u001b[39;49mdtype,\n\u001b[0;32m 742\u001b[0m copy\u001b[39m=\u001b[39;49mcopy,\n\u001b[0;32m 743\u001b[0m typ\u001b[39m=\u001b[39;49mmanager,\n\u001b[0;32m 744\u001b[0m )\n\u001b[0;32m 745\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 746\u001b[0m mgr \u001b[39m=\u001b[39m dict_to_mgr(\n\u001b[0;32m 747\u001b[0m {},\n\u001b[0;32m 748\u001b[0m index,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 751\u001b[0m typ\u001b[39m=\u001b[39mmanager,\n\u001b[0;32m 752\u001b[0m )\n", + "File \u001b[1;32mc:\\programs\\miniconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py:351\u001b[0m, in \u001b[0;36mndarray_to_mgr\u001b[1;34m(values, index, columns, dtype, copy, typ)\u001b[0m\n\u001b[0;32m 346\u001b[0m \u001b[39m# _prep_ndarray ensures that values.ndim == 2 at this point\u001b[39;00m\n\u001b[0;32m 347\u001b[0m index, columns \u001b[39m=\u001b[39m _get_axes(\n\u001b[0;32m 348\u001b[0m values\u001b[39m.\u001b[39mshape[\u001b[39m0\u001b[39m], values\u001b[39m.\u001b[39mshape[\u001b[39m1\u001b[39m], index\u001b[39m=\u001b[39mindex, columns\u001b[39m=\u001b[39mcolumns\n\u001b[0;32m 349\u001b[0m )\n\u001b[1;32m--> 351\u001b[0m _check_values_indices_shape_match(values, index, columns)\n\u001b[0;32m 353\u001b[0m \u001b[39mif\u001b[39;00m typ \u001b[39m==\u001b[39m \u001b[39m\"\u001b[39m\u001b[39marray\u001b[39m\u001b[39m\"\u001b[39m:\n\u001b[0;32m 355\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39missubclass\u001b[39m(values\u001b[39m.\u001b[39mdtype\u001b[39m.\u001b[39mtype, \u001b[39mstr\u001b[39m):\n", + "File \u001b[1;32mc:\\programs\\miniconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py:422\u001b[0m, in \u001b[0;36m_check_values_indices_shape_match\u001b[1;34m(values, index, columns)\u001b[0m\n\u001b[0;32m 420\u001b[0m passed \u001b[39m=\u001b[39m values\u001b[39m.\u001b[39mshape\n\u001b[0;32m 421\u001b[0m implied \u001b[39m=\u001b[39m (\u001b[39mlen\u001b[39m(index), \u001b[39mlen\u001b[39m(columns))\n\u001b[1;32m--> 422\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mShape of passed values is \u001b[39m\u001b[39m{\u001b[39;00mpassed\u001b[39m}\u001b[39;00m\u001b[39m, indices imply \u001b[39m\u001b[39m{\u001b[39;00mimplied\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m)\n", + "\u001b[1;31mValueError\u001b[0m: Shape of passed values is (3, 1), indices imply (3, 2)" + ] + } + ], + "source": [ + "df = pd.DataFrame(data=['Toyota', 'Honda', ['Camry']], columns=['car', 'model'])" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
car
0Toyota
1Honda
\n", + "
" + ], + "text/plain": [ + " car\n", + "0 Toyota\n", + "1 Honda" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.9.12 ('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.9.12" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "d31b83e9610685068a0fe73b54051d44dc1110027bef1c52e64b671e72faac45" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}