diff --git a/Student/Ruohan/lesson01/.cache/v/cache/lastfailed b/Student/Ruohan/lesson01/.cache/v/cache/lastfailed new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/Student/Ruohan/lesson01/.cache/v/cache/lastfailed @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/Student/Ruohan/lesson01/.ipynb_checkpoints/Concept-1-Procedural-vs-Functional-Programming-A-Simple-Calculator-checkpoint.ipynb b/Student/Ruohan/lesson01/.ipynb_checkpoints/Concept-1-Procedural-vs-Functional-Programming-A-Simple-Calculator-checkpoint.ipynb new file mode 100644 index 0000000..69462b3 --- /dev/null +++ b/Student/Ruohan/lesson01/.ipynb_checkpoints/Concept-1-Procedural-vs-Functional-Programming-A-Simple-Calculator-checkpoint.ipynb @@ -0,0 +1,187 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Pure Function\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def m(n: int) -> int:\n", + " return 2**n-1\n", + "\n", + "## This result depends only on the parameter, n. \n", + "## There are no changes to global variables and the function doesn't update any mutable data structures." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## A procedural approach!\n", + "\n", + "- Functions generally consist of multiple statements\n", + " - Assignments\n", + " - If-statements\n", + " - While loops\n", + " - Etc." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the barely functional calculator!\n", + "Enter an integer: 10\n", + "Enter an operator (+, -, *, or /): *\n", + "Enter an integer: 30\n", + "The result is: 300\n" + ] + } + ], + "source": [ + "OPERATORS = '+', '-', '*', '/'\n", + "\n", + "\n", + "def p_main():\n", + " \n", + " \"\"\"The main flow.\"\"\"\n", + "\n", + " print('Welcome to the barely functional calculator!')\n", + " number1 = p_get_number()\n", + " operator = p_get_operator()\n", + " number2 = p_get_number()\n", + " result = p_calculate(number1, operator, number2)\n", + " print('The result is: %s' % result)\n", + "\n", + "\n", + "def p_get_number():\n", + " \n", + " \"\"\"Reads an integer from the standard input and returns it.\n", + " If a non-integer value is entered, a warning is printed,\n", + " and a new value is read.\"\"\"\n", + " \n", + " while True:\n", + " s = input('Enter an integer: ')\n", + " try:\n", + " return int(s)\n", + " except ValueError:\n", + " print('That is not an integer!')\n", + " \n", + "\n", + "def p_get_operator():\n", + " \n", + " \"\"\"Reads an operator from the standard input and returns it.\n", + " Valid operators are: +, -, *, and /. If an invalid operator\n", + " is entered, a warning is printed, and a new value is read.\"\"\" \n", + " \n", + " while True:\n", + " s = input('Enter an operator (+, -, *, or /): ')\n", + " if s in OPERATORS:\n", + " return s\n", + " print('That is not an operator!')\n", + " \n", + " \n", + "def p_calculate(number1, operator, number2):\n", + " \n", + " \"\"\"Performs a calculation with two numbers and an operator,\n", + " and returns the result.\"\"\"\n", + " \n", + " if operator == '+':\n", + " return number1 + number2\n", + " if operator == '-':\n", + " return number1 - number2\n", + " if operator == '*':\n", + " return number1 * number2\n", + " if operator == '/':\n", + " return number1 / number2\n", + " raise Exception('Invalid operator!')\n", + "\n", + " \n", + "p_main()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## A functional approach!\n", + "\n", + "- Functions consist of only one expression\n", + "- How can we validate input? (One of the many things we will learn later!)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "OPERATORS = '+', '-', '*', '/'\n", + "\n", + "\n", + "def f_get_number():\n", + " return int(input('Enter an interger: '))\n", + "\n", + "\n", + "def f_get_operator():\n", + " return input('Enter an operator:')\n", + "\n", + "\n", + "def f_calculate(num1, num2, operator) :\n", + " return num1 + num2 if operator == '+'\\\n", + " else num1 - num2 elif operator == '-'\\\n", + " else num1 * num2 elif operator == '*'\\\n", + " else num1 / num2 elif operator == '/'\\\n", + " else None\n", + " \n", + "\n", + "\n", + "def f_main():\n", + " return f_calculate(f_get_number(), f_get_number(), f_get_operator())\n", + " \n", + "\n", + "print('The result is: %s' % f_main())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson01/.ipynb_checkpoints/Concept-5-Comprehensions-Example-checkpoint.ipynb b/Student/Ruohan/lesson01/.ipynb_checkpoints/Concept-5-Comprehensions-Example-checkpoint.ipynb new file mode 100644 index 0000000..0014fcd --- /dev/null +++ b/Student/Ruohan/lesson01/.ipynb_checkpoints/Concept-5-Comprehensions-Example-checkpoint.ipynb @@ -0,0 +1,194 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "list(range(10))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for x in range(10):\n", + " print (x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## MAP" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "items = [1, 2, 3, 4, 5]\n", + "squared = list(map(lambda x: x**2, items))\n", + "print(squared)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Filter\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "number_list = range(-10, 10)\n", + "less_than_zero = list(filter(lambda x: x < 0, number_list))\n", + "print(less_than_zero)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "number_list = range(-10, 10)\n", + "less_than_zero = list(filter(lambda x: x > 0, number_list))\n", + "print(less_than_zero)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "l = [2,3,4,5,6]\n", + "list(filter())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reduce" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from functools import reduce\n", + "product = reduce((lambda x, y: x * y), [1, 2, 3])\n", + "print(product)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## List Comprehension\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "multiples = [i for i in range(30) if i % 3 == 0]\n", + "print(multiples)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "squared = [x**2 for x in range(10)]\n", + "print(squared)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dict Comprehension\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "mcase = {'a': 5, 'b': 3, 'A': 7, 'Z': 6}\n", + "{v: k for k, v in mcase.items()}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Set Comprehension" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "squared = {x**2 for x in [0,1,1,2]}\n", + "print(squared)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## In the previous set example, can you explain the output?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson01/__pycache__/generator_solution.cpython-36.pyc b/Student/Ruohan/lesson01/__pycache__/generator_solution.cpython-36.pyc new file mode 100644 index 0000000..5e833ed Binary files /dev/null and b/Student/Ruohan/lesson01/__pycache__/generator_solution.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson01/__pycache__/test_generator.cpython-36-PYTEST.pyc b/Student/Ruohan/lesson01/__pycache__/test_generator.cpython-36-PYTEST.pyc new file mode 100644 index 0000000..ecaf930 Binary files /dev/null and b/Student/Ruohan/lesson01/__pycache__/test_generator.cpython-36-PYTEST.pyc differ diff --git a/Student/Ruohan/lesson01/comprehension.py b/Student/Ruohan/lesson01/comprehension.py new file mode 100644 index 0000000..377c28d --- /dev/null +++ b/Student/Ruohan/lesson01/comprehension.py @@ -0,0 +1,22 @@ +#! /usr/local/bin/python3 + +import pandas as pd +music = pd.read_csv('featuresdf.csv') + +# use pandas +gimme = music[music.danceability > 0.8] +gimme = gimme[music.loudness < -0.5] +gimme = gimme.sort_values(by = 'danceability', ascending = False) +gimme = gimme[['name', 'artists', 'danceability', 'loudness']] +print(gimme.head(5)) + + +#use comprehension +paly_list = [(name,artists,dancebility,loudness) \ + for name,artists,dancebility,loudness in \ + zip(music.name, music.artists, music.danceability, music.loudness) \ + if dancebility > 0.8 and loudness < -5.0] + +sorted_paly_list = sorted(paly_list, key = lambda x: x[2], reverse = True) + +print(sorted_paly_list[:5]) diff --git a/Student/Ruohan/lesson01/featuresdf.csv b/Student/Ruohan/lesson01/featuresdf.csv new file mode 100644 index 0000000..ece51ea --- /dev/null +++ b/Student/Ruohan/lesson01/featuresdf.csv @@ -0,0 +1,101 @@ +id,name,artists,danceability,energy,key,loudness,mode,speechiness,acousticness,instrumentalness,liveness,valence,tempo,duration_ms,time_signature +7qiZfU4dY1lWllzX7mPBI,Shape of You,Ed Sheeran,0.825,0.652,1.0,-3.183,0.0,0.0802,0.581,0.0,0.0931,0.931,95.977,233713.0,4.0 +5CtI0qwDJkDQGwXD1H1cL,Despacito - Remix,Luis Fonsi,0.694,0.815,2.0,-4.328,1.0,0.12,0.229,0.0,0.0924,0.813,88.931,228827.0,4.0 +4aWmUDTfIPGksMNLV2rQP,Despacito (Featuring Daddy Yankee),Luis Fonsi,0.66,0.786,2.0,-4.757,1.0,0.17,0.209,0.0,0.112,0.846,177.833,228200.0,4.0 +6RUKPb4LETWmmr3iAEQkt,Something Just Like This,The Chainsmokers,0.617,0.635,11.0,-6.769,0.0,0.0317,0.0498,1.44e-05,0.164,0.446,103.019,247160.0,4.0 +3DXncPQOG4VBw3QHh3S81,I'm the One,DJ Khaled,0.609,0.668,7.0,-4.284,1.0,0.0367,0.0552,0.0,0.167,0.811,80.924,288600.0,4.0 +7KXjTSCq5nL1LoYtL7XAw,HUMBLE.,Kendrick Lamar,0.904,0.611,1.0,-6.842,0.0,0.0888,0.000259,2.03e-05,0.0976,0.4,150.02,177000.0,4.0 +3eR23VReFzcdmS7TYCrhC,It Ain't Me (with Selena Gomez),Kygo,0.64,0.533,0.0,-6.596,1.0,0.0706,0.119,0.0,0.0864,0.515,99.968,220781.0,4.0 +3B54sVLJ402zGa6Xm4YGN,Unforgettable,French Montana,0.726,0.769,6.0,-5.043,1.0,0.123,0.0293,0.0101,0.104,0.733,97.985,233902.0,4.0 +0KKkJNfGyhkQ5aFogxQAP,That's What I Like,Bruno Mars,0.853,0.56,1.0,-4.961,1.0,0.0406,0.013,0.0,0.0944,0.86,134.066,206693.0,4.0 +3NdDpSvN911VPGivFlV5d,"I Don’t Wanna Live Forever (Fifty Shades Darker) - From ""Fifty Shades Darker (Original Motion Picture Soundtrack)""",ZAYN,0.735,0.451,0.0,-8.374,1.0,0.0585,0.0631,1.3e-05,0.325,0.0862,117.973,245200.0,4.0 +7GX5flRQZVHRAGd6B4TmD,XO TOUR Llif3,Lil Uzi Vert,0.732,0.75,11.0,-6.366,0.0,0.231,0.00264,0.0,0.109,0.401,155.096,182707.0,4.0 +72jbDTw1piOOj770jWNea,Paris,The Chainsmokers,0.653,0.658,2.0,-6.428,1.0,0.0304,0.0215,1.66e-06,0.0939,0.219,99.99,221507.0,4.0 +0dA2Mk56wEzDgegdC6R17,Stay (with Alessia Cara),Zedd,0.679,0.634,5.0,-5.024,0.0,0.0654,0.232,0.0,0.115,0.498,102.013,210091.0,4.0 +4iLqG9SeJSnt0cSPICSjx,Attention,Charlie Puth,0.774,0.626,3.0,-4.432,0.0,0.0432,0.0969,3.12e-05,0.0848,0.777,100.041,211475.0,4.0 +0VgkVdmE4gld66l8iyGjg,Mask Off,Future,0.833,0.434,2.0,-8.795,1.0,0.431,0.0102,0.0219,0.165,0.281,150.062,204600.0,4.0 +3a1lNhkSLSkpJE4MSHpDu,Congratulations,Post Malone,0.627,0.812,6.0,-4.215,1.0,0.0358,0.198,0.0,0.212,0.504,123.071,220293.0,4.0 +6kex4EBAj0WHXDKZMEJaa,Swalla (feat. Nicki Minaj & Ty Dolla $ign),Jason Derulo,0.696,0.817,1.0,-3.862,1.0,0.109,0.075,0.0,0.187,0.782,98.064,216409.0,4.0 +6PCUP3dWmTjcTtXY02oFd,Castle on the Hill,Ed Sheeran,0.461,0.834,2.0,-4.868,1.0,0.0989,0.0232,1.14e-05,0.14,0.471,135.007,261154.0,4.0 +5knuzwU65gJK7IF5yJsua,Rockabye (feat. Sean Paul & Anne-Marie),Clean Bandit,0.72,0.763,9.0,-4.068,0.0,0.0523,0.406,0.0,0.18,0.742,101.965,251088.0,4.0 +0CcQNd8CINkwQfe1RDtGV,Believer,Imagine Dragons,0.779,0.787,10.0,-4.305,0.0,0.108,0.0524,0.0,0.14,0.708,124.982,204347.0,4.0 +2rb5MvYT7ZIxbKW5hfcHx,Mi Gente,J Balvin,0.543,0.677,11.0,-4.915,0.0,0.0993,0.0148,6.21e-06,0.13,0.294,103.809,189440.0,4.0 +0tKcYR2II1VCQWT79i5Nr,Thunder,Imagine Dragons,0.6,0.81,0.0,-4.749,1.0,0.0479,0.00683,0.21,0.155,0.298,167.88,187147.0,4.0 +5uCax9HTNlzGybIStD3vD,Say You Won't Let Go,James Arthur,0.358,0.557,10.0,-7.398,1.0,0.059,0.695,0.0,0.0902,0.494,85.043,211467.0,4.0 +79cuOz3SPQTuFrp8WgftA,There's Nothing Holdin' Me Back,Shawn Mendes,0.857,0.8,2.0,-4.035,1.0,0.0583,0.381,0.0,0.0913,0.966,121.996,199440.0,4.0 +6De0lHrwBfPfrhorm9q1X,Me Rehúso,Danny Ocean,0.744,0.804,1.0,-6.327,1.0,0.0677,0.0231,0.0,0.0494,0.426,104.823,205715.0,4.0 +6D0b04NJIKfEMg040WioJ,Issues,Julia Michaels,0.706,0.427,8.0,-6.864,1.0,0.0879,0.413,0.0,0.0609,0.42,113.804,176320.0,4.0 +0afhq8XCExXpqazXczTSv,Galway Girl,Ed Sheeran,0.624,0.876,9.0,-3.374,1.0,0.1,0.0735,0.0,0.327,0.781,99.943,170827.0,4.0 +3ebXMykcMXOcLeJ9xZ17X,Scared to Be Lonely,Martin Garrix,0.584,0.54,1.0,-7.786,0.0,0.0576,0.0895,0.0,0.261,0.195,137.972,220883.0,4.0 +7BKLCZ1jbUBVqRi2FVlTV,Closer,The Chainsmokers,0.748,0.524,8.0,-5.599,1.0,0.0338,0.414,0.0,0.111,0.661,95.01,244960.0,4.0 +1x5sYLZiu9r5E43kMlt9f,Symphony (feat. Zara Larsson),Clean Bandit,0.707,0.629,0.0,-4.581,0.0,0.0563,0.259,1.6e-05,0.138,0.457,122.863,212459.0,4.0 +5GXAXm5YOmYT0kL5jHvYB,I Feel It Coming,The Weeknd,0.768,0.813,0.0,-5.94,0.0,0.128,0.427,0.0,0.102,0.579,92.994,269187.0,4.0 +5aAx2yezTd8zXrkmtKl66,Starboy,The Weeknd,0.681,0.594,7.0,-7.028,1.0,0.282,0.165,3.49e-06,0.134,0.535,186.054,230453.0,4.0 +1OAh8uOEOvTDqkKFsKksC,Wild Thoughts,DJ Khaled,0.671,0.672,0.0,-3.094,0.0,0.0688,0.0329,0.0,0.118,0.632,97.98,204173.0,4.0 +7tr2za8SQg2CI8EDgrdtN,Slide,Calvin Harris,0.736,0.795,1.0,-3.299,0.0,0.0545,0.498,1.21e-06,0.254,0.511,104.066,230813.0,4.0 +2ekn2ttSfGqwhhate0LSR,New Rules,Dua Lipa,0.771,0.696,9.0,-6.258,0.0,0.0755,0.00256,9.71e-06,0.179,0.656,116.054,208827.0,4.0 +5tz69p7tJuGPeMGwNTxYu,1-800-273-8255,Logic,0.629,0.572,5.0,-7.733,0.0,0.0387,0.57,0.0,0.192,0.386,100.015,250173.0,4.0 +7hDc8b7IXETo14hHIHdnh,Passionfruit,Drake,0.809,0.463,11.0,-11.377,1.0,0.0396,0.256,0.085,0.109,0.364,111.98,298941.0,4.0 +7wGoVu4Dady5GV0Sv4UIs,rockstar,Post Malone,0.577,0.522,5.0,-6.594,0.0,0.0984,0.13,9.03e-05,0.142,0.119,159.772,218320.0,4.0 +6EpRaXYhGOB3fj4V2uDkM,Strip That Down,Liam Payne,0.869,0.485,6.0,-5.595,1.0,0.0545,0.246,0.0,0.0765,0.527,106.028,204502.0,4.0 +3A7qX2QjDlPnazUsRk5y0,2U (feat. Justin Bieber),David Guetta,0.548,0.65,8.0,-5.827,0.0,0.0591,0.219,0.0,0.225,0.557,144.937,194897.0,4.0 +0tgVpDi06FyKpA1z0VMD4,Perfect,Ed Sheeran,0.599,0.448,8.0,-6.312,1.0,0.0232,0.163,0.0,0.106,0.168,95.05,263400.0,3.0 +78rIJddV4X0HkNAInEcYd,Call On Me - Ryan Riback Extended Remix,Starley,0.676,0.843,0.0,-4.068,1.0,0.0367,0.0623,0.000752,0.181,0.718,105.003,222041.0,4.0 +5bcTCxgc7xVfSaMV3RuVk,Feels,Calvin Harris,0.893,0.745,11.0,-3.105,0.0,0.0571,0.0642,0.0,0.0943,0.872,101.018,223413.0,4.0 +0NiXXAI876aGImAd6rTj8,Mama,Jonas Blue,0.746,0.793,11.0,-4.209,0.0,0.0412,0.11,0.0,0.0528,0.557,104.027,181615.0,4.0 +0qYTZCo5Bwh1nsUFGZP3z,Felices los 4,Maluma,0.755,0.789,5.0,-4.502,1.0,0.146,0.231,0.0,0.351,0.737,93.973,229849.0,4.0 +2EEeOnHehOozLq4aS0n6S,iSpy (feat. Lil Yachty),KYLE,0.746,0.653,7.0,-6.745,1.0,0.289,0.378,0.0,0.229,0.672,75.016,253107.0,4.0 +152lZdxL1OR0ZMW6KquMi,Location,Khalid,0.736,0.449,1.0,-11.462,0.0,0.425,0.33,0.000162,0.0898,0.326,80.126,219080.0,4.0 +6mICuAdrwEjh6Y6lroV2K,Chantaje,Shakira,0.852,0.773,8.0,-2.921,0.0,0.0776,0.187,3.05e-05,0.159,0.907,102.034,195840.0,4.0 +4Km5HrUvYTaSUfiSGPJeQ,Bad and Boujee (feat. Lil Uzi Vert),Migos,0.927,0.665,11.0,-5.313,1.0,0.244,0.061,0.0,0.123,0.175,127.076,343150.0,4.0 +0ofbQMrRDsUaVKq2mGLEA,Havana,Camila Cabello,0.768,0.517,7.0,-4.323,0.0,0.0312,0.186,3.8e-05,0.104,0.418,104.992,216897.0,4.0 +6HUnnBwYZqcED1eQztxMB,Solo Dance,Martin Jensen,0.744,0.836,6.0,-2.396,0.0,0.0507,0.0435,0.0,0.194,0.36,114.965,174933.0,4.0 +343YBumqHu19cGoGARUTs,Fake Love,Drake,0.927,0.488,9.0,-9.433,0.0,0.42,0.108,0.0,0.196,0.605,133.987,210937.0,4.0 +4pdPtRcBmOSQDlJ3Fk945,Let Me Love You,DJ Snake,0.476,0.718,8.0,-5.309,1.0,0.0576,0.0784,1.02e-05,0.122,0.142,199.864,205947.0,4.0 +3PEgB3fkiojxms35ntsTg,More Than You Know,Axwell /\ Ingrosso,0.644,0.743,5.0,-5.002,0.0,0.0355,0.034,0.0,0.257,0.544,123.074,203000.0,4.0 +1xznGGDReH1oQq0xzbwXa,One Dance,Drake,0.791,0.619,1.0,-5.886,1.0,0.0532,0.00784,0.00423,0.351,0.371,103.989,173987.0,4.0 +7nKBxz47S9SD79N086fuh,SUBEME LA RADIO,Enrique Iglesias,0.684,0.823,9.0,-3.297,0.0,0.0773,0.0744,0.0,0.111,0.647,91.048,208163.0,4.0 +1NDxZ7cFAo481dtYWdrUn,Pretty Girl - Cheat Codes X CADE Remix,Maggie Lindemann,0.703,0.868,7.0,-4.661,0.0,0.0291,0.15,0.132,0.104,0.733,121.03,193613.0,4.0 +3m660poUr1chesgkkjQM7,Sorry Not Sorry,Demi Lovato,0.704,0.633,11.0,-6.923,0.0,0.241,0.0214,0.0,0.29,0.863,144.021,203760.0,4.0 +3kxfsdsCpFgN412fpnW85,Redbone,Childish Gambino,0.743,0.359,1.0,-10.401,1.0,0.0794,0.199,0.00611,0.137,0.587,160.083,326933.0,4.0 +6b8Be6ljOzmkOmFslEb23,24K Magic,Bruno Mars,0.818,0.803,1.0,-4.282,1.0,0.0797,0.034,0.0,0.153,0.632,106.97,225983.0,4.0 +6HZILIRieu8S0iqY8kIKh,DNA.,Kendrick Lamar,0.637,0.514,1.0,-6.763,1.0,0.365,0.0047,0.0,0.094,0.402,139.931,185947.0,4.0 +3umS4y3uQDkqekNjVpiRU,El Amante,Nicky Jam,0.683,0.691,8.0,-5.535,1.0,0.0432,0.243,0.0,0.14,0.732,179.91,219507.0,4.0 +00lNx0OcTJrS3MKHcB80H,You Don't Know Me - Radio Edit,Jax Jones,0.876,0.669,11.0,-6.054,0.0,0.138,0.163,0.0,0.185,0.682,124.007,213947.0,4.0 +6520aj0B4FSKGVuKNsOCO,Chained To The Rhythm,Katy Perry,0.448,0.801,0.0,-5.363,1.0,0.165,0.0733,0.0,0.146,0.462,189.798,237734.0,4.0 +1louJpMmzEicAn7lzDalP,No Promises (feat. Demi Lovato),Cheat Codes,0.741,0.667,10.0,-5.445,1.0,0.134,0.0575,0.0,0.106,0.595,112.956,223504.0,4.0 +2QbFClFyhMMtiurUjuQlA,Don't Wanna Know (feat. Kendrick Lamar),Maroon 5,0.775,0.617,7.0,-6.166,1.0,0.0701,0.341,0.0,0.0985,0.485,100.048,214265.0,4.0 +5hYTyyh2odQKphUbMqc5g,"How Far I'll Go - From ""Moana""",Alessia Cara,0.314,0.555,9.0,-9.601,1.0,0.37,0.157,0.000108,0.067,0.159,179.666,175517.0,4.0 +38yBBH2jacvDxrznF7h08,Slow Hands,Niall Horan,0.734,0.418,0.0,-6.678,1.0,0.0425,0.0129,0.0,0.0579,0.868,85.909,188174.0,4.0 +2cnKEkpVUSV4wnjQiTWfH,Escápate Conmigo,Wisin,0.747,0.864,8.0,-3.181,0.0,0.0599,0.0245,4.46e-05,0.0853,0.754,92.028,232787.0,4.0 +0SGkqnVQo9KPytSri1H6c,Bounce Back,Big Sean,0.77,0.567,2.0,-5.698,1.0,0.175,0.105,0.0,0.125,0.26,81.477,222360.0,4.0 +5Ohxk2dO5COHF1krpoPig,Sign of the Times,Harry Styles,0.516,0.595,5.0,-4.63,1.0,0.0313,0.0275,0.0,0.109,0.222,119.972,340707.0,4.0 +6gBFPUFcJLzWGx4lenP6h,goosebumps,Travis Scott,0.841,0.728,7.0,-3.37,1.0,0.0484,0.0847,0.0,0.149,0.43,130.049,243837.0,4.0 +5Z3GHaZ6ec9bsiI5Benrb,Young Dumb & Broke,Khalid,0.798,0.539,1.0,-6.351,1.0,0.0421,0.199,1.66e-05,0.165,0.394,136.949,202547.0,4.0 +6jA8HL9i4QGzsj6fjoxp8,There for You,Martin Garrix,0.611,0.644,6.0,-7.607,0.0,0.0553,0.124,0.0,0.124,0.13,105.969,221904.0,4.0 +21TdkDRXuAB3k90ujRU1e,Cold (feat. Future),Maroon 5,0.697,0.716,9.0,-6.288,0.0,0.113,0.118,0.0,0.0424,0.506,99.905,234308.0,4.0 +7vGuf3Y35N4wmASOKLUVV,Silence,Marshmello,0.52,0.761,4.0,-3.093,1.0,0.0853,0.256,4.96e-06,0.17,0.286,141.971,180823.0,4.0 +1mXVgsBdtIVeCLJnSnmtd,Too Good At Goodbyes,Sam Smith,0.698,0.375,5.0,-8.279,1.0,0.0491,0.652,0.0,0.173,0.534,91.92,201000.0,4.0 +3EmmCZoqpWOTY1g2GBwJo,Just Hold On,Steve Aoki,0.647,0.932,11.0,-3.515,1.0,0.0824,0.00383,1.5e-06,0.0574,0.374,114.991,198774.0,4.0 +6uFsE1JgZ20EXyU0JQZbU,Look What You Made Me Do,Taylor Swift,0.773,0.68,9.0,-6.378,0.0,0.141,0.213,1.57e-05,0.122,0.497,128.062,211859.0,4.0 +0CokSRCu5hZgPxcZBaEzV,Glorious (feat. Skylar Grey),Macklemore,0.731,0.794,0.0,-5.126,0.0,0.0522,0.0323,2.59e-05,0.112,0.356,139.994,220454.0,4.0 +6875MeXyCW0wLyT72Eetm,Starving,Hailee Steinfeld,0.721,0.626,4.0,-4.2,1.0,0.123,0.402,0.0,0.102,0.558,99.914,181933.0,4.0 +3AEZUABDXNtecAOSC1qTf,Reggaetón Lento (Bailemos),CNCO,0.761,0.838,4.0,-3.073,0.0,0.0502,0.4,0.0,0.176,0.71,93.974,222560.0,4.0 +3E2Zh20GDCR9B1EYjfXWy,Weak,AJR,0.673,0.637,5.0,-4.518,1.0,0.0429,0.137,0.0,0.184,0.678,123.98,201160.0,4.0 +4pLwZjInHj3SimIyN9SnO,Side To Side,Ariana Grande,0.648,0.738,6.0,-5.883,0.0,0.247,0.0408,0.0,0.292,0.603,159.145,226160.0,4.0 +3QwBODjSEzelZyVjxPOHd,Otra Vez (feat. J Balvin),Zion & Lennox,0.832,0.772,10.0,-5.429,1.0,0.1,0.0559,0.000486,0.44,0.704,96.016,209453.0,4.0 +1wjzFQodRWrPcQ0AnYnvQ,I Like Me Better,Lauv,0.752,0.505,9.0,-7.621,1.0,0.253,0.535,2.55e-06,0.104,0.419,91.97,197437.0,4.0 +04DwTuZ2VBdJCCC5TROn7,In the Name of Love,Martin Garrix,0.49,0.485,4.0,-6.237,0.0,0.0406,0.0592,0.0,0.337,0.196,133.889,195840.0,4.0 +6DNtNfH8hXkqOX1sjqmI7,Cold Water (feat. Justin Bieber & MØ),Major Lazer,0.608,0.798,6.0,-5.092,0.0,0.0432,0.0736,0.0,0.156,0.501,92.943,185352.0,4.0 +1UZOjK1BwmwWU14Erba9C,Malibu,Miley Cyrus,0.573,0.781,8.0,-6.406,1.0,0.0555,0.0767,2.64e-05,0.0813,0.343,139.934,231907.0,4.0 +4b4KcovePX8Ke2cLIQTLM,All Night,The Vamps,0.544,0.809,8.0,-5.098,1.0,0.0363,0.0038,0.0,0.323,0.448,145.017,197640.0,4.0 +1a5Yu5L18qNxVhXx38njO,Hear Me Now,Alok,0.789,0.442,11.0,-7.844,1.0,0.0421,0.586,0.00366,0.0927,0.45,121.971,192846.0,4.0 +4c2W3VKsOFoIg2SFaO6DY,Your Song,Rita Ora,0.855,0.624,1.0,-4.093,1.0,0.0488,0.158,0.0,0.0513,0.962,117.959,180757.0,4.0 +22eADXu8DfOAUEDw4vU8q,Ahora Dice,Chris Jeday,0.708,0.693,6.0,-5.516,1.0,0.138,0.246,0.0,0.129,0.427,143.965,271080.0,4.0 +7nZmah2llfvLDiUjm0kiy,Friends (with BloodPop®),Justin Bieber,0.744,0.739,8.0,-5.35,1.0,0.0387,0.00459,0.0,0.306,0.649,104.99,189467.0,4.0 +2fQrGHiQOvpL9UgPvtYy6,Bank Account,21 Savage,0.884,0.346,8.0,-8.228,0.0,0.351,0.0151,7.04e-06,0.0871,0.376,75.016,220307.0,4.0 +1PSBzsahR2AKwLJgx8ehB,Bad Things (with Camila Cabello),Machine Gun Kelly,0.675,0.69,2.0,-4.761,1.0,0.132,0.21,0.0,0.287,0.272,137.817,239293.0,4.0 +0QsvXIfqM0zZoerQfsI9l,Don't Let Me Down,The Chainsmokers,0.542,0.859,11.0,-5.651,1.0,0.197,0.16,0.00466,0.137,0.403,159.797,208053.0,4.0 +7mldq42yDuxiUNn08nvzH,Body Like A Back Road,Sam Hunt,0.731,0.469,5.0,-7.226,1.0,0.0326,0.463,1.04e-06,0.103,0.631,98.963,165387.0,4.0 +7i2DJ88J7jQ8K7zqFX2fW,Now Or Never,Halsey,0.658,0.588,6.0,-4.902,0.0,0.0367,0.105,1.28e-06,0.125,0.434,110.075,214802.0,4.0 +1j4kHkkpqZRBwE0A4CN4Y,Dusk Till Dawn - Radio Edit,ZAYN,0.258,0.437,11.0,-6.593,0.0,0.039,0.101,1.27e-06,0.106,0.0967,180.043,239000.0,4.0 diff --git a/Student/Ruohan/lesson01/generator_solution.py b/Student/Ruohan/lesson01/generator_solution.py new file mode 100644 index 0000000..c3b3bc9 --- /dev/null +++ b/Student/Ruohan/lesson01/generator_solution.py @@ -0,0 +1,48 @@ + +import math + +def intsum(): + i = 0 + j = 0 + while True: + yield i + j += 1 + i += j + + +def intsum2(): + i = 0 + j = 0 + while True: + yield i + j += 1 + i += j + + +def doubler(): + i = 1 + while True: + yield i + i *= 2 + + +def fib(): + i0 = 1 + i1 = 0 + i = i0 + while True: + i = i0 + i1 + i0, i1 = i1, i + yield i + + +def prime(): + n = 2 + while True: + Iscomposite = False + for i in range(2,int(math.sqrt(n))+1): + if n % i == 0: + Iscomposite = True + if not Iscomposite: + yield n + n += 1 diff --git a/Student/Ruohan/lesson01/iterator_1.py b/Student/Ruohan/lesson01/iterator_1.py new file mode 100644 index 0000000..f436054 --- /dev/null +++ b/Student/Ruohan/lesson01/iterator_1.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +""" +Simple iterator examples +""" + + +class IterateMe_1: + """ + About as simple an iterator as you can get: + + returns the sequence of numbers from zero to 4 + ( like range(4) ) + """ + + def __init__(self, stop=5): + self.current = -1 + self.stop = stop + + def __iter__(self): + return self + + def __next__(self): + self.current += 1 + if self.current < self.stop: + return self.current + else: + raise StopIteration + + +class IterateMe_2: + + def __init__(self, start=-1, stop=5, step=1): + self.current = start - step + self.stop = stop + self.step = step + + def __iter__(self): + return self + + def __next__(self): + self.current += self.step + if self.current < self.stop: + return self.current + else: + raise StopIteration + + +if __name__ == "__main__": + + print("Testing the iterator2") + it = IterateMe_2(2, 20, 2) + for i in it: + if i > 10: + break + print(i) diff --git a/Student/Ruohan/lesson01/test_generator.py b/Student/Ruohan/lesson01/test_generator.py new file mode 100644 index 0000000..57c55eb --- /dev/null +++ b/Student/Ruohan/lesson01/test_generator.py @@ -0,0 +1,61 @@ +""" +test_generator.py + +tests the solution to the generator lab + +can be run with py.test or nosetests +""" + +import generator_solution as gen + + +def test_intsum(): + + g = gen.intsum() + + assert next(g) == 0 + assert next(g) == 1 + assert next(g) == 3 + assert next(g) == 6 + assert next(g) == 10 + assert next(g) == 15 + + +def test_intsum2(): + + g = gen.intsum2() + + assert next(g) == 0 + assert next(g) == 1 + assert next(g) == 3 + assert next(g) == 6 + assert next(g) == 10 + assert next(g) == 15 + + +def test_doubler(): + + g = gen.doubler() + + assert next(g) == 1 + assert next(g) == 2 + assert next(g) == 4 + assert next(g) == 8 + assert next(g) == 16 + assert next(g) == 32 + + for i in range(10): + j = next(g) + + assert j == 2**15 + + +def test_fib(): + g = gen.fib() + assert [next(g) for i in range(9)] == [1, 1, 2, 3, 5, 8, 13, 21, 34] + + +def test_prime(): + g = gen.prime() + for val in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]: + assert next(g) == val diff --git a/Student/Ruohan/lesson02/.ipynb_checkpoints/activity-checkpoint.ipynb b/Student/Ruohan/lesson02/.ipynb_checkpoints/activity-checkpoint.ipynb new file mode 100644 index 0000000..67fa213 --- /dev/null +++ b/Student/Ruohan/lesson02/.ipynb_checkpoints/activity-checkpoint.ipynb @@ -0,0 +1,292 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def check_prime(number):\n", + " for divisor in range(2, int(number ** 0.5) + 1):\n", + " if number % divisor == 0:\n", + " return False\n", + " return True \n", + "class Primes:\n", + " def __init__(self, max):\n", + " self.max = max\n", + " self.number = 1\n", + " def __iter__(self):\n", + " return self\n", + " def __next__(self):\n", + " self.number += 1\n", + " if self.number >= self.max:\n", + " raise StopIteration\n", + " elif check_prime(self.number):\n", + " return self.number\n", + " else:\n", + " return self.__next__()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "285" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum([x*x for x in range(10)])" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "([x*x for x in range(10)])" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x*x for x in range(10)]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + " at 0x10841b8e0>" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(x*x for x in range(10))" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'outside': 1, 'inside': 2}\n", + "{'outside': 1, 'inside': 2}\n" + ] + } + ], + "source": [ + "def outside():\n", + " d = {\"outside\": 1}\n", + " def inside():\n", + " d[\"inside\"] = 2\n", + " print(d)\n", + " inside()\n", + " print(d)\n", + " \n", + "outside()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 4, 6]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import heapq\n", + "heap =[]\n", + "heapq.heappush(heap,1)\n", + "heapq.heappush(heap,2)\n", + "heapq.heappush(heap,4)\n", + "heapq.heappush(heap,6)\n", + "heapq.heappush(heap,7)\n", + "heapq.heappush(heap,9)\n", + "heapq.heappush(heap,10)\n", + "heapq.heappush(heap,12) \n", + "heapq.nsmallest(4, heap)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 4, 5, 6, 8]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import functools\n", + "import heapq\n", + "heap =[]\n", + "push = functools.partial(heapq.heappush, heap)\n", + "smallest = functools.partial(heapq.nsmallest, iterable=heap)\n", + "push(1)\n", + "push(3)\n", + "push(5)\n", + "push(6)\n", + "push(8)\n", + "push(11)\n", + "push(4)\n", + "push(16)\n", + "push(17)\n", + "smallest(6)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "x = 'abc'\n", + "y = '123'\n", + "zip(x,y)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from itertools import islice\n", + "islice('ABCDEFG', 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['A', 'B']\n" + ] + } + ], + "source": [ + "print(list(islice('ABCDEFDG',2)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson02/activity.ipynb b/Student/Ruohan/lesson02/activity.ipynb new file mode 100644 index 0000000..4ad63bb --- /dev/null +++ b/Student/Ruohan/lesson02/activity.ipynb @@ -0,0 +1,312 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def check_prime(number):\n", + " for divisor in range(2, int(number ** 0.5) + 1):\n", + " if number % divisor == 0:\n", + " return False\n", + " return True \n", + "class Primes:\n", + " def __init__(self, max):\n", + " self.max = max\n", + " self.number = 1\n", + " def __iter__(self):\n", + " return self\n", + " def __next__(self):\n", + " self.number += 1\n", + " if self.number >= self.max:\n", + " raise StopIteration\n", + " elif check_prime(self.number):\n", + " return self.number\n", + " else:\n", + " return self.__next__()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "285" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum([x*x for x in range(10)])" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "([x*x for x in range(10)])" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[x*x for x in range(10)]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + " at 0x10841b8e0>" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(x*x for x in range(10))" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'outside': 1, 'inside': 2}\n", + "{'outside': 1, 'inside': 2}\n" + ] + } + ], + "source": [ + "def outside():\n", + " d = {\"outside\": 1}\n", + " def inside():\n", + " d[\"inside\"] = 2\n", + " print(d)\n", + " inside()\n", + " print(d)\n", + " \n", + "outside()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 4, 6]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import heapq\n", + "heap =[]\n", + "heapq.heappush(heap,1)\n", + "heapq.heappush(heap,2)\n", + "heapq.heappush(heap,4)\n", + "heapq.heappush(heap,6)\n", + "heapq.heappush(heap,7)\n", + "heapq.heappush(heap,9)\n", + "heapq.heappush(heap,10)\n", + "heapq.heappush(heap,12) \n", + "heapq.nsmallest(4, heap)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 4, 5, 6, 8]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import functools\n", + "import heapq\n", + "heap =[]\n", + "push = functools.partial(heapq.heappush, heap)\n", + "smallest = functools.partial(heapq.nsmallest, iterable=heap)\n", + "push(1)\n", + "push(3)\n", + "push(5)\n", + "push(6)\n", + "push(8)\n", + "push(11)\n", + "push(4)\n", + "push(16)\n", + "push(17)\n", + "smallest(6)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "x = 'abc'\n", + "y = '123'\n", + "zip(x,y)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from itertools import islice\n", + "islice('ABCDEFG', 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['A', 'B']\n" + ] + } + ], + "source": [ + "print(list(islice('ABCDEFDG',2)))" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "def outside():\n", + " d = {'outside': 1}\n", + " def inside():\n", + " nonlocal d\n", + " d = {'inside': 2}\n", + " inside()\n", + " print('outside')\n", + " print(d)\n", + " \n", + "outside()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "{}" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson02/closures_spotify.py b/Student/Ruohan/lesson02/closures_spotify.py new file mode 100644 index 0000000..7f7c9c2 --- /dev/null +++ b/Student/Ruohan/lesson02/closures_spotify.py @@ -0,0 +1,14 @@ +#! /usr/local/bin/python3 + +import pandas as pd +music = pd.read_csv('featuresdf.csv') + +def high_engergy_check(critical_value = 0.8): + value = critical_value + def high_energy_track(file = music): + nonlocal value + return file[['name', 'artists', 'energy']][file['energy'] > value] + return high_energy_track + +high_energy = high_engergy_check(0.8) +print(high_energy(music)) diff --git a/Student/Ruohan/lesson02/featuresdf.csv b/Student/Ruohan/lesson02/featuresdf.csv new file mode 100644 index 0000000..ece51ea --- /dev/null +++ b/Student/Ruohan/lesson02/featuresdf.csv @@ -0,0 +1,101 @@ +id,name,artists,danceability,energy,key,loudness,mode,speechiness,acousticness,instrumentalness,liveness,valence,tempo,duration_ms,time_signature +7qiZfU4dY1lWllzX7mPBI,Shape of You,Ed Sheeran,0.825,0.652,1.0,-3.183,0.0,0.0802,0.581,0.0,0.0931,0.931,95.977,233713.0,4.0 +5CtI0qwDJkDQGwXD1H1cL,Despacito - Remix,Luis Fonsi,0.694,0.815,2.0,-4.328,1.0,0.12,0.229,0.0,0.0924,0.813,88.931,228827.0,4.0 +4aWmUDTfIPGksMNLV2rQP,Despacito (Featuring Daddy Yankee),Luis Fonsi,0.66,0.786,2.0,-4.757,1.0,0.17,0.209,0.0,0.112,0.846,177.833,228200.0,4.0 +6RUKPb4LETWmmr3iAEQkt,Something Just Like This,The Chainsmokers,0.617,0.635,11.0,-6.769,0.0,0.0317,0.0498,1.44e-05,0.164,0.446,103.019,247160.0,4.0 +3DXncPQOG4VBw3QHh3S81,I'm the One,DJ Khaled,0.609,0.668,7.0,-4.284,1.0,0.0367,0.0552,0.0,0.167,0.811,80.924,288600.0,4.0 +7KXjTSCq5nL1LoYtL7XAw,HUMBLE.,Kendrick Lamar,0.904,0.611,1.0,-6.842,0.0,0.0888,0.000259,2.03e-05,0.0976,0.4,150.02,177000.0,4.0 +3eR23VReFzcdmS7TYCrhC,It Ain't Me (with Selena Gomez),Kygo,0.64,0.533,0.0,-6.596,1.0,0.0706,0.119,0.0,0.0864,0.515,99.968,220781.0,4.0 +3B54sVLJ402zGa6Xm4YGN,Unforgettable,French Montana,0.726,0.769,6.0,-5.043,1.0,0.123,0.0293,0.0101,0.104,0.733,97.985,233902.0,4.0 +0KKkJNfGyhkQ5aFogxQAP,That's What I Like,Bruno Mars,0.853,0.56,1.0,-4.961,1.0,0.0406,0.013,0.0,0.0944,0.86,134.066,206693.0,4.0 +3NdDpSvN911VPGivFlV5d,"I Don’t Wanna Live Forever (Fifty Shades Darker) - From ""Fifty Shades Darker (Original Motion Picture Soundtrack)""",ZAYN,0.735,0.451,0.0,-8.374,1.0,0.0585,0.0631,1.3e-05,0.325,0.0862,117.973,245200.0,4.0 +7GX5flRQZVHRAGd6B4TmD,XO TOUR Llif3,Lil Uzi Vert,0.732,0.75,11.0,-6.366,0.0,0.231,0.00264,0.0,0.109,0.401,155.096,182707.0,4.0 +72jbDTw1piOOj770jWNea,Paris,The Chainsmokers,0.653,0.658,2.0,-6.428,1.0,0.0304,0.0215,1.66e-06,0.0939,0.219,99.99,221507.0,4.0 +0dA2Mk56wEzDgegdC6R17,Stay (with Alessia Cara),Zedd,0.679,0.634,5.0,-5.024,0.0,0.0654,0.232,0.0,0.115,0.498,102.013,210091.0,4.0 +4iLqG9SeJSnt0cSPICSjx,Attention,Charlie Puth,0.774,0.626,3.0,-4.432,0.0,0.0432,0.0969,3.12e-05,0.0848,0.777,100.041,211475.0,4.0 +0VgkVdmE4gld66l8iyGjg,Mask Off,Future,0.833,0.434,2.0,-8.795,1.0,0.431,0.0102,0.0219,0.165,0.281,150.062,204600.0,4.0 +3a1lNhkSLSkpJE4MSHpDu,Congratulations,Post Malone,0.627,0.812,6.0,-4.215,1.0,0.0358,0.198,0.0,0.212,0.504,123.071,220293.0,4.0 +6kex4EBAj0WHXDKZMEJaa,Swalla (feat. Nicki Minaj & Ty Dolla $ign),Jason Derulo,0.696,0.817,1.0,-3.862,1.0,0.109,0.075,0.0,0.187,0.782,98.064,216409.0,4.0 +6PCUP3dWmTjcTtXY02oFd,Castle on the Hill,Ed Sheeran,0.461,0.834,2.0,-4.868,1.0,0.0989,0.0232,1.14e-05,0.14,0.471,135.007,261154.0,4.0 +5knuzwU65gJK7IF5yJsua,Rockabye (feat. Sean Paul & Anne-Marie),Clean Bandit,0.72,0.763,9.0,-4.068,0.0,0.0523,0.406,0.0,0.18,0.742,101.965,251088.0,4.0 +0CcQNd8CINkwQfe1RDtGV,Believer,Imagine Dragons,0.779,0.787,10.0,-4.305,0.0,0.108,0.0524,0.0,0.14,0.708,124.982,204347.0,4.0 +2rb5MvYT7ZIxbKW5hfcHx,Mi Gente,J Balvin,0.543,0.677,11.0,-4.915,0.0,0.0993,0.0148,6.21e-06,0.13,0.294,103.809,189440.0,4.0 +0tKcYR2II1VCQWT79i5Nr,Thunder,Imagine Dragons,0.6,0.81,0.0,-4.749,1.0,0.0479,0.00683,0.21,0.155,0.298,167.88,187147.0,4.0 +5uCax9HTNlzGybIStD3vD,Say You Won't Let Go,James Arthur,0.358,0.557,10.0,-7.398,1.0,0.059,0.695,0.0,0.0902,0.494,85.043,211467.0,4.0 +79cuOz3SPQTuFrp8WgftA,There's Nothing Holdin' Me Back,Shawn Mendes,0.857,0.8,2.0,-4.035,1.0,0.0583,0.381,0.0,0.0913,0.966,121.996,199440.0,4.0 +6De0lHrwBfPfrhorm9q1X,Me Rehúso,Danny Ocean,0.744,0.804,1.0,-6.327,1.0,0.0677,0.0231,0.0,0.0494,0.426,104.823,205715.0,4.0 +6D0b04NJIKfEMg040WioJ,Issues,Julia Michaels,0.706,0.427,8.0,-6.864,1.0,0.0879,0.413,0.0,0.0609,0.42,113.804,176320.0,4.0 +0afhq8XCExXpqazXczTSv,Galway Girl,Ed Sheeran,0.624,0.876,9.0,-3.374,1.0,0.1,0.0735,0.0,0.327,0.781,99.943,170827.0,4.0 +3ebXMykcMXOcLeJ9xZ17X,Scared to Be Lonely,Martin Garrix,0.584,0.54,1.0,-7.786,0.0,0.0576,0.0895,0.0,0.261,0.195,137.972,220883.0,4.0 +7BKLCZ1jbUBVqRi2FVlTV,Closer,The Chainsmokers,0.748,0.524,8.0,-5.599,1.0,0.0338,0.414,0.0,0.111,0.661,95.01,244960.0,4.0 +1x5sYLZiu9r5E43kMlt9f,Symphony (feat. Zara Larsson),Clean Bandit,0.707,0.629,0.0,-4.581,0.0,0.0563,0.259,1.6e-05,0.138,0.457,122.863,212459.0,4.0 +5GXAXm5YOmYT0kL5jHvYB,I Feel It Coming,The Weeknd,0.768,0.813,0.0,-5.94,0.0,0.128,0.427,0.0,0.102,0.579,92.994,269187.0,4.0 +5aAx2yezTd8zXrkmtKl66,Starboy,The Weeknd,0.681,0.594,7.0,-7.028,1.0,0.282,0.165,3.49e-06,0.134,0.535,186.054,230453.0,4.0 +1OAh8uOEOvTDqkKFsKksC,Wild Thoughts,DJ Khaled,0.671,0.672,0.0,-3.094,0.0,0.0688,0.0329,0.0,0.118,0.632,97.98,204173.0,4.0 +7tr2za8SQg2CI8EDgrdtN,Slide,Calvin Harris,0.736,0.795,1.0,-3.299,0.0,0.0545,0.498,1.21e-06,0.254,0.511,104.066,230813.0,4.0 +2ekn2ttSfGqwhhate0LSR,New Rules,Dua Lipa,0.771,0.696,9.0,-6.258,0.0,0.0755,0.00256,9.71e-06,0.179,0.656,116.054,208827.0,4.0 +5tz69p7tJuGPeMGwNTxYu,1-800-273-8255,Logic,0.629,0.572,5.0,-7.733,0.0,0.0387,0.57,0.0,0.192,0.386,100.015,250173.0,4.0 +7hDc8b7IXETo14hHIHdnh,Passionfruit,Drake,0.809,0.463,11.0,-11.377,1.0,0.0396,0.256,0.085,0.109,0.364,111.98,298941.0,4.0 +7wGoVu4Dady5GV0Sv4UIs,rockstar,Post Malone,0.577,0.522,5.0,-6.594,0.0,0.0984,0.13,9.03e-05,0.142,0.119,159.772,218320.0,4.0 +6EpRaXYhGOB3fj4V2uDkM,Strip That Down,Liam Payne,0.869,0.485,6.0,-5.595,1.0,0.0545,0.246,0.0,0.0765,0.527,106.028,204502.0,4.0 +3A7qX2QjDlPnazUsRk5y0,2U (feat. Justin Bieber),David Guetta,0.548,0.65,8.0,-5.827,0.0,0.0591,0.219,0.0,0.225,0.557,144.937,194897.0,4.0 +0tgVpDi06FyKpA1z0VMD4,Perfect,Ed Sheeran,0.599,0.448,8.0,-6.312,1.0,0.0232,0.163,0.0,0.106,0.168,95.05,263400.0,3.0 +78rIJddV4X0HkNAInEcYd,Call On Me - Ryan Riback Extended Remix,Starley,0.676,0.843,0.0,-4.068,1.0,0.0367,0.0623,0.000752,0.181,0.718,105.003,222041.0,4.0 +5bcTCxgc7xVfSaMV3RuVk,Feels,Calvin Harris,0.893,0.745,11.0,-3.105,0.0,0.0571,0.0642,0.0,0.0943,0.872,101.018,223413.0,4.0 +0NiXXAI876aGImAd6rTj8,Mama,Jonas Blue,0.746,0.793,11.0,-4.209,0.0,0.0412,0.11,0.0,0.0528,0.557,104.027,181615.0,4.0 +0qYTZCo5Bwh1nsUFGZP3z,Felices los 4,Maluma,0.755,0.789,5.0,-4.502,1.0,0.146,0.231,0.0,0.351,0.737,93.973,229849.0,4.0 +2EEeOnHehOozLq4aS0n6S,iSpy (feat. Lil Yachty),KYLE,0.746,0.653,7.0,-6.745,1.0,0.289,0.378,0.0,0.229,0.672,75.016,253107.0,4.0 +152lZdxL1OR0ZMW6KquMi,Location,Khalid,0.736,0.449,1.0,-11.462,0.0,0.425,0.33,0.000162,0.0898,0.326,80.126,219080.0,4.0 +6mICuAdrwEjh6Y6lroV2K,Chantaje,Shakira,0.852,0.773,8.0,-2.921,0.0,0.0776,0.187,3.05e-05,0.159,0.907,102.034,195840.0,4.0 +4Km5HrUvYTaSUfiSGPJeQ,Bad and Boujee (feat. Lil Uzi Vert),Migos,0.927,0.665,11.0,-5.313,1.0,0.244,0.061,0.0,0.123,0.175,127.076,343150.0,4.0 +0ofbQMrRDsUaVKq2mGLEA,Havana,Camila Cabello,0.768,0.517,7.0,-4.323,0.0,0.0312,0.186,3.8e-05,0.104,0.418,104.992,216897.0,4.0 +6HUnnBwYZqcED1eQztxMB,Solo Dance,Martin Jensen,0.744,0.836,6.0,-2.396,0.0,0.0507,0.0435,0.0,0.194,0.36,114.965,174933.0,4.0 +343YBumqHu19cGoGARUTs,Fake Love,Drake,0.927,0.488,9.0,-9.433,0.0,0.42,0.108,0.0,0.196,0.605,133.987,210937.0,4.0 +4pdPtRcBmOSQDlJ3Fk945,Let Me Love You,DJ Snake,0.476,0.718,8.0,-5.309,1.0,0.0576,0.0784,1.02e-05,0.122,0.142,199.864,205947.0,4.0 +3PEgB3fkiojxms35ntsTg,More Than You Know,Axwell /\ Ingrosso,0.644,0.743,5.0,-5.002,0.0,0.0355,0.034,0.0,0.257,0.544,123.074,203000.0,4.0 +1xznGGDReH1oQq0xzbwXa,One Dance,Drake,0.791,0.619,1.0,-5.886,1.0,0.0532,0.00784,0.00423,0.351,0.371,103.989,173987.0,4.0 +7nKBxz47S9SD79N086fuh,SUBEME LA RADIO,Enrique Iglesias,0.684,0.823,9.0,-3.297,0.0,0.0773,0.0744,0.0,0.111,0.647,91.048,208163.0,4.0 +1NDxZ7cFAo481dtYWdrUn,Pretty Girl - Cheat Codes X CADE Remix,Maggie Lindemann,0.703,0.868,7.0,-4.661,0.0,0.0291,0.15,0.132,0.104,0.733,121.03,193613.0,4.0 +3m660poUr1chesgkkjQM7,Sorry Not Sorry,Demi Lovato,0.704,0.633,11.0,-6.923,0.0,0.241,0.0214,0.0,0.29,0.863,144.021,203760.0,4.0 +3kxfsdsCpFgN412fpnW85,Redbone,Childish Gambino,0.743,0.359,1.0,-10.401,1.0,0.0794,0.199,0.00611,0.137,0.587,160.083,326933.0,4.0 +6b8Be6ljOzmkOmFslEb23,24K Magic,Bruno Mars,0.818,0.803,1.0,-4.282,1.0,0.0797,0.034,0.0,0.153,0.632,106.97,225983.0,4.0 +6HZILIRieu8S0iqY8kIKh,DNA.,Kendrick Lamar,0.637,0.514,1.0,-6.763,1.0,0.365,0.0047,0.0,0.094,0.402,139.931,185947.0,4.0 +3umS4y3uQDkqekNjVpiRU,El Amante,Nicky Jam,0.683,0.691,8.0,-5.535,1.0,0.0432,0.243,0.0,0.14,0.732,179.91,219507.0,4.0 +00lNx0OcTJrS3MKHcB80H,You Don't Know Me - Radio Edit,Jax Jones,0.876,0.669,11.0,-6.054,0.0,0.138,0.163,0.0,0.185,0.682,124.007,213947.0,4.0 +6520aj0B4FSKGVuKNsOCO,Chained To The Rhythm,Katy Perry,0.448,0.801,0.0,-5.363,1.0,0.165,0.0733,0.0,0.146,0.462,189.798,237734.0,4.0 +1louJpMmzEicAn7lzDalP,No Promises (feat. Demi Lovato),Cheat Codes,0.741,0.667,10.0,-5.445,1.0,0.134,0.0575,0.0,0.106,0.595,112.956,223504.0,4.0 +2QbFClFyhMMtiurUjuQlA,Don't Wanna Know (feat. Kendrick Lamar),Maroon 5,0.775,0.617,7.0,-6.166,1.0,0.0701,0.341,0.0,0.0985,0.485,100.048,214265.0,4.0 +5hYTyyh2odQKphUbMqc5g,"How Far I'll Go - From ""Moana""",Alessia Cara,0.314,0.555,9.0,-9.601,1.0,0.37,0.157,0.000108,0.067,0.159,179.666,175517.0,4.0 +38yBBH2jacvDxrznF7h08,Slow Hands,Niall Horan,0.734,0.418,0.0,-6.678,1.0,0.0425,0.0129,0.0,0.0579,0.868,85.909,188174.0,4.0 +2cnKEkpVUSV4wnjQiTWfH,Escápate Conmigo,Wisin,0.747,0.864,8.0,-3.181,0.0,0.0599,0.0245,4.46e-05,0.0853,0.754,92.028,232787.0,4.0 +0SGkqnVQo9KPytSri1H6c,Bounce Back,Big Sean,0.77,0.567,2.0,-5.698,1.0,0.175,0.105,0.0,0.125,0.26,81.477,222360.0,4.0 +5Ohxk2dO5COHF1krpoPig,Sign of the Times,Harry Styles,0.516,0.595,5.0,-4.63,1.0,0.0313,0.0275,0.0,0.109,0.222,119.972,340707.0,4.0 +6gBFPUFcJLzWGx4lenP6h,goosebumps,Travis Scott,0.841,0.728,7.0,-3.37,1.0,0.0484,0.0847,0.0,0.149,0.43,130.049,243837.0,4.0 +5Z3GHaZ6ec9bsiI5Benrb,Young Dumb & Broke,Khalid,0.798,0.539,1.0,-6.351,1.0,0.0421,0.199,1.66e-05,0.165,0.394,136.949,202547.0,4.0 +6jA8HL9i4QGzsj6fjoxp8,There for You,Martin Garrix,0.611,0.644,6.0,-7.607,0.0,0.0553,0.124,0.0,0.124,0.13,105.969,221904.0,4.0 +21TdkDRXuAB3k90ujRU1e,Cold (feat. Future),Maroon 5,0.697,0.716,9.0,-6.288,0.0,0.113,0.118,0.0,0.0424,0.506,99.905,234308.0,4.0 +7vGuf3Y35N4wmASOKLUVV,Silence,Marshmello,0.52,0.761,4.0,-3.093,1.0,0.0853,0.256,4.96e-06,0.17,0.286,141.971,180823.0,4.0 +1mXVgsBdtIVeCLJnSnmtd,Too Good At Goodbyes,Sam Smith,0.698,0.375,5.0,-8.279,1.0,0.0491,0.652,0.0,0.173,0.534,91.92,201000.0,4.0 +3EmmCZoqpWOTY1g2GBwJo,Just Hold On,Steve Aoki,0.647,0.932,11.0,-3.515,1.0,0.0824,0.00383,1.5e-06,0.0574,0.374,114.991,198774.0,4.0 +6uFsE1JgZ20EXyU0JQZbU,Look What You Made Me Do,Taylor Swift,0.773,0.68,9.0,-6.378,0.0,0.141,0.213,1.57e-05,0.122,0.497,128.062,211859.0,4.0 +0CokSRCu5hZgPxcZBaEzV,Glorious (feat. Skylar Grey),Macklemore,0.731,0.794,0.0,-5.126,0.0,0.0522,0.0323,2.59e-05,0.112,0.356,139.994,220454.0,4.0 +6875MeXyCW0wLyT72Eetm,Starving,Hailee Steinfeld,0.721,0.626,4.0,-4.2,1.0,0.123,0.402,0.0,0.102,0.558,99.914,181933.0,4.0 +3AEZUABDXNtecAOSC1qTf,Reggaetón Lento (Bailemos),CNCO,0.761,0.838,4.0,-3.073,0.0,0.0502,0.4,0.0,0.176,0.71,93.974,222560.0,4.0 +3E2Zh20GDCR9B1EYjfXWy,Weak,AJR,0.673,0.637,5.0,-4.518,1.0,0.0429,0.137,0.0,0.184,0.678,123.98,201160.0,4.0 +4pLwZjInHj3SimIyN9SnO,Side To Side,Ariana Grande,0.648,0.738,6.0,-5.883,0.0,0.247,0.0408,0.0,0.292,0.603,159.145,226160.0,4.0 +3QwBODjSEzelZyVjxPOHd,Otra Vez (feat. J Balvin),Zion & Lennox,0.832,0.772,10.0,-5.429,1.0,0.1,0.0559,0.000486,0.44,0.704,96.016,209453.0,4.0 +1wjzFQodRWrPcQ0AnYnvQ,I Like Me Better,Lauv,0.752,0.505,9.0,-7.621,1.0,0.253,0.535,2.55e-06,0.104,0.419,91.97,197437.0,4.0 +04DwTuZ2VBdJCCC5TROn7,In the Name of Love,Martin Garrix,0.49,0.485,4.0,-6.237,0.0,0.0406,0.0592,0.0,0.337,0.196,133.889,195840.0,4.0 +6DNtNfH8hXkqOX1sjqmI7,Cold Water (feat. Justin Bieber & MØ),Major Lazer,0.608,0.798,6.0,-5.092,0.0,0.0432,0.0736,0.0,0.156,0.501,92.943,185352.0,4.0 +1UZOjK1BwmwWU14Erba9C,Malibu,Miley Cyrus,0.573,0.781,8.0,-6.406,1.0,0.0555,0.0767,2.64e-05,0.0813,0.343,139.934,231907.0,4.0 +4b4KcovePX8Ke2cLIQTLM,All Night,The Vamps,0.544,0.809,8.0,-5.098,1.0,0.0363,0.0038,0.0,0.323,0.448,145.017,197640.0,4.0 +1a5Yu5L18qNxVhXx38njO,Hear Me Now,Alok,0.789,0.442,11.0,-7.844,1.0,0.0421,0.586,0.00366,0.0927,0.45,121.971,192846.0,4.0 +4c2W3VKsOFoIg2SFaO6DY,Your Song,Rita Ora,0.855,0.624,1.0,-4.093,1.0,0.0488,0.158,0.0,0.0513,0.962,117.959,180757.0,4.0 +22eADXu8DfOAUEDw4vU8q,Ahora Dice,Chris Jeday,0.708,0.693,6.0,-5.516,1.0,0.138,0.246,0.0,0.129,0.427,143.965,271080.0,4.0 +7nZmah2llfvLDiUjm0kiy,Friends (with BloodPop®),Justin Bieber,0.744,0.739,8.0,-5.35,1.0,0.0387,0.00459,0.0,0.306,0.649,104.99,189467.0,4.0 +2fQrGHiQOvpL9UgPvtYy6,Bank Account,21 Savage,0.884,0.346,8.0,-8.228,0.0,0.351,0.0151,7.04e-06,0.0871,0.376,75.016,220307.0,4.0 +1PSBzsahR2AKwLJgx8ehB,Bad Things (with Camila Cabello),Machine Gun Kelly,0.675,0.69,2.0,-4.761,1.0,0.132,0.21,0.0,0.287,0.272,137.817,239293.0,4.0 +0QsvXIfqM0zZoerQfsI9l,Don't Let Me Down,The Chainsmokers,0.542,0.859,11.0,-5.651,1.0,0.197,0.16,0.00466,0.137,0.403,159.797,208053.0,4.0 +7mldq42yDuxiUNn08nvzH,Body Like A Back Road,Sam Hunt,0.731,0.469,5.0,-7.226,1.0,0.0326,0.463,1.04e-06,0.103,0.631,98.963,165387.0,4.0 +7i2DJ88J7jQ8K7zqFX2fW,Now Or Never,Halsey,0.658,0.588,6.0,-4.902,0.0,0.0367,0.105,1.28e-06,0.125,0.434,110.075,214802.0,4.0 +1j4kHkkpqZRBwE0A4CN4Y,Dusk Till Dawn - Radio Edit,ZAYN,0.258,0.437,11.0,-6.593,0.0,0.039,0.101,1.27e-06,0.106,0.0967,180.043,239000.0,4.0 diff --git a/Student/Ruohan/lesson02/generators_spotify.py b/Student/Ruohan/lesson02/generators_spotify.py new file mode 100644 index 0000000..04aa29a --- /dev/null +++ b/Student/Ruohan/lesson02/generators_spotify.py @@ -0,0 +1,10 @@ +#! /usr/local/bin/python3 + +import pandas as pd +music = pd.read_csv('featuresdf.csv') + +title_artists = zip(music.name, music.artists) + +gen1 = (Ed_data for Ed_data in title_artists if Ed_data[1] == 'Ed Sheeran') +play_list_ES = list(gen1) +print(play_list_ES) diff --git a/Student/Ruohan/lesson03/.ipynb_checkpoints/Activity-checkpoint.ipynb b/Student/Ruohan/lesson03/.ipynb_checkpoints/Activity-checkpoint.ipynb new file mode 100644 index 0000000..14382fd --- /dev/null +++ b/Student/Ruohan/lesson03/.ipynb_checkpoints/Activity-checkpoint.ipynb @@ -0,0 +1,134 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def add(a, b):\n", + " print(\"this is a add function\")\n", + " return a + b\n", + "\n", + "def sub(a, b):\n", + " print(\"this is a substract function\")\n", + " return a - b\n", + "\n", + "def mul(a, b):\n", + " print(\"this is a multiply function\")\n", + " return a * b" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'logged' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mdecorators\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlogged\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecorators\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;31m##the same as\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'logged' is not defined" + ] + } + ], + "source": [ + "def add(a, b):\n", + " print('add function')\n", + " return x + y\n", + "\n", + "decorators.add = logged(decorators.add)\n", + " ##the same as \n", + " \n", + "@logged\n", + "def add(a, b):\n", + " print('add function')\n", + " return x + y" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "inside my_decorator.__init__()\n", + "inside aFunction()\n", + "Finished decorating aFunction()\n", + "inside my_decorator.__call__()\n" + ] + } + ], + "source": [ + "class my_decorator(object):\n", + "\n", + " def __init__(self, f):\n", + " print(\"inside my_decorator.__init__()\")\n", + " f() # Prove that function definition has completed\n", + "\n", + " def __call__(self):\n", + " print(\"inside my_decorator.__call__()\")\n", + "\n", + "@my_decorator\n", + "def aFunction():\n", + " print(\"inside aFunction()\")\n", + "\n", + "print(\"Finished decorating aFunction()\")\n", + "\n", + "aFunction()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class my_decorator(object):\n", + "\n", + " def __init__(self, f):\n", + " print(\"inside my_decorator.__init__()\")\n", + " f() # Prove that function definition has completed\n", + "\n", + " def __call__(self):\n", + " print(\"inside my_decorator.__call__()\")\n", + "\n", + "@my_decorator\n", + "def aFunction():\n", + " print(\"inside aFunction()\")\n", + "\n", + "print(\"Finished decorating aFunction()\")\n", + "\n", + "aFunction()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson03/.ipynb_checkpoints/Lesson3-checkpoint.ipynb b/Student/Ruohan/lesson03/.ipynb_checkpoints/Lesson3-checkpoint.ipynb new file mode 100644 index 0000000..1efb8cf --- /dev/null +++ b/Student/Ruohan/lesson03/.ipynb_checkpoints/Lesson3-checkpoint.ipynb @@ -0,0 +1,522 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def add(x,y):\n", + " print(\"add func\")\n", + " return x+y\n", + "decorators.add = logged(decorators.add)\n", + " ## Same as\n", + " \n", + "\n", + "@logged\n", + "def add(x,y):\n", + " print(\"add func\")\n", + " return x+y\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Decorators\n", + "\n", + "def null_decorator(func):\n", + " return func\n", + "\n", + "def greet():\n", + " return 'Hello!'\n", + "\n", + "greet = null_decorator(greet)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "greet()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Above same as this ..\n", + "\n", + "@null_decorator\n", + "def greet():\n", + " return 'Hello!'\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "greet()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Decorators\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from functools import wraps\n", + "def nullable(function):\n", + " @wraps(function)\n", + " def null_wrapper(arg):\n", + " return None if arg is None else function(arg)\n", + " return null_wrapper" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "nlog = nullable(math.log)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "some_data = [10, 100, None, 50, 60]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "scaled = map(nlog, some_data) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "list(scaled)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Enter Context Manager" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "## Context Manager\n", + "\n", + "f = open(\"x.txt\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<_io.TextIOWrapper name='x.txt' mode='r' encoding='UTF-8'>" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f.__enter__()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'h'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f.read(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "## now lets run exit to the close the file\n", + "f.__exit__(None, None, None)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## try to read once again\n", + "f.read(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Context Manager Summary \n", + "## so to open a file, process its contents, and make sure to close it, \n", + "## you can simply do:\n", + "\n", + "with open(\"x.txt\") as f:\n", + " data = f.read()\n", + " do something with data\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Acquiring a lock during threading\n", + "\n", + "import threading\n", + "lock=threading.Lock()\n", + "lock.acquire()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Use the lock\")\n", + "lock.release()\n", + "\n", + "## you acquire a resource, use it and then close it. \n", + "## but it is easy to close or release the resources\n", + "## Context Manager\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Context Manager\n", + "\n", + "class Context(object):\n", + "\n", + " def __init__(self, handle_error):\n", + " print ('__init__(%s)' % handle_error)\n", + " self.handle_error = handle_error\n", + " def __enter__(self):\n", + " print ('__enter__()')\n", + " return self\n", + " def __exit__(self, exc_type, exc_val, exc_tb):\n", + " print ('__exit__(%s, %s, %s)' % (exc_type, exc_val, exc_tb))\n", + " return self.handle_error" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "with Context(True) as foo:\n", + " print ('This is in the context')\n", + " ## run once and the uncomment following line\n", + " ##raise RuntimeError('this is the error message')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## In the above example your code is bracketed by enter and exit\n", + "\n", + "## Because the exit method returns True, the raised error is ‘handled’.\n", + "## What if we try with False?\n", + "\n", + "with Context(False) as foo:\n", + " print ('This is in the context')\n", + " ## run this with and without commenting following line\n", + " ##raise RuntimeError('this is the error message')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## contextlib.contextmanager() \n", + "## will turn a generator function into context manager." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from contextlib import contextmanager\n", + "\n", + "@contextmanager\n", + "def context(boolean):\n", + " print (\"__init__ code here\")\n", + " try:\n", + " print (\"__enter__ code goes here\")\n", + " yield object()\n", + " except Exception as e:\n", + " print (\"errors handled here\")\n", + " if not boolean:\n", + " raise\n", + " finally:\n", + " print (\"__exit__ cleanup goes here\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import contextlib\n", + "import http.client\n", + "with contextlib.closing(\n", + " http.client.HTTPConnection(\"www.example.com\")) as host:\n", + " host.request(\"GET\", \"/path/to/resources/12345/\")\n", + " response= host.getresponse()\n", + " print(response.read())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from __future__ import print_function\n", + "import contextlib\n", + "\n", + "@contextlib.contextmanager\n", + "def manager():\n", + " \"\"\"Easiest way to get a custom context manager...\"\"\"\n", + " try:\n", + " print('Entered')\n", + " yield\n", + " finally:\n", + " print('Closed')\n", + "\n", + "\n", + "def gen():\n", + " \"\"\"Just a generator with a context manager inside.\n", + "\n", + " When the context is entered, we'll see \"Entered\" on the console\n", + " and when exited, we'll see \"Closed\" on the console.\n", + " \"\"\"\n", + " man = manager()\n", + " with man:\n", + " for i in range(10):\n", + " yield i\n", + "\n", + "\n", + "# Test what happens when we consume a generator.\n", + "list(gen())\n", + "\n", + "def fn():\n", + " g = gen()\n", + " next(g)\n", + " # g.close()\n", + "\n", + "# Test what happens when the generator gets garbage collected inside\n", + "# a function\n", + "print('Start of Function')\n", + "fn()\n", + "print('End of Function')\n", + "\n", + "# Test what happens when a generator gets garbage collected outside\n", + "# a function. IIRC, this isn't _guaranteed_ to happen in all cases.\n", + "g = gen()\n", + "next(g)\n", + "# g.close()\n", + "print('EOF')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Context Manager Examples - Advanced Usecase. \n", + "## yield without argument is semantically equivalent to yield None\n", + "\n", + "from contextlib import contextmanager\n", + "import sys\n", + "\n", + "@contextmanager\n", + "def redirected(**kwds):\n", + " stream_names = [\"stdin\", \"stdout\", \"stderr\"]\n", + " old_streams = {}\n", + " try:\n", + " for sname in stream_names:\n", + " stream = kwds.get(sname, None)\n", + " if stream is not None and stream != getattr(sys, sname):\n", + " old_streams[sname] = getattr(sys, sname)\n", + " setattr(sys, sname, stream)\n", + " yield\n", + " finally:\n", + " for sname, stream in old_streams.items():\n", + " setattr(sys, sname, stream)\n", + "\n", + "with redirected(stdout=open(\"/tmp/uw-py220-log-context-mgr.txt\", \"w\")):\n", + " # these print statements will go to /tmp/log.txt\n", + " print (\"Test entry 1\")\n", + " print (\"Test entry 2\")\n", + "# back to the normal stdout\n", + "print (\"Back to normal stdout again\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Yield in Context Manager\n", + "\n", + "yield expression returns control to the whatever is using the generator. \n", + "The generator pauses at this point, which means that the @contextmanager \n", + "decorator knows that the code is done with the setup part.\n", + "\n", + "In other words, everything you want to do in the context manager __enter__ \n", + "phase has to take place before the yield.\n", + "\n", + "Once your context exits (so the block under the with statement is done), \n", + "the @contextmanager decorator is called for the __exit__ part of the \n", + "context manager protocol and will do one of two things:\n", + "\n", + "If there was no exception, it'll resume your generator. So your generator \n", + "unpauses at the yield line, and you enter the cleanup phase, the part\n", + "\n", + "If there was an exception, the decorator uses generator.throw() to raise \n", + "that exception in the generator. It'll be as if the yield line caused \n", + "that exception. Because you have a finally clause, it'll be executed \n", + "before your generator exits because of the exception." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Recursion\n", + "\n", + "## Recursion to use up all stack space. \n", + "## RuntimeError: maximum recursion depth exceeded\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def f():\n", + " print(\"Hello\")\n", + " f()\n", + "\n", + "f()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson03/Activity.ipynb b/Student/Ruohan/lesson03/Activity.ipynb new file mode 100644 index 0000000..77f5174 --- /dev/null +++ b/Student/Ruohan/lesson03/Activity.ipynb @@ -0,0 +1,226 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def add(a, b):\n", + " print(\"this is a add function\")\n", + " return a + b\n", + "\n", + "def sub(a, b):\n", + " print(\"this is a substract function\")\n", + " return a - b\n", + "\n", + "def mul(a, b):\n", + " print(\"this is a multiply function\")\n", + " return a * b" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'logged' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mdecorators\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlogged\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecorators\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;31m##the same as\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'logged' is not defined" + ] + } + ], + "source": [ + "def add(a, b):\n", + " print('add function')\n", + " return x + y\n", + "\n", + "decorators.add = logged(decorators.add)\n", + " ##the same as \n", + " \n", + "@logged\n", + "def add(a, b):\n", + " print('add function')\n", + " return x + y" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "inside my_decorator.__init__()\n", + "inside aFunction()\n", + "Finished decorating aFunction()\n", + "inside my_decorator.__call__()\n" + ] + } + ], + "source": [ + "class my_decorator(object):\n", + "\n", + " def __init__(self, f):\n", + " print(\"inside my_decorator.__init__()\")\n", + " f() # Prove that function definition has completed\n", + "\n", + " def __call__(self):\n", + " print(\"inside my_decorator.__call__()\")\n", + "\n", + "@my_decorator\n", + "def aFunction():\n", + " print(\"inside aFunction()\")\n", + "\n", + "print(\"Finished decorating aFunction()\")\n", + "\n", + "aFunction()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Entering func1\n", + "inside func1()\n", + "Exited func1\n", + "Entering func2\n", + "inside func2()\n", + "Exited func2\n" + ] + } + ], + "source": [ + "class entry_exit(object):\n", + "\n", + " def __init__(self, f):\n", + " self.f = f\n", + "\n", + " def __call__(self):\n", + " print(\"Entering\", self.f.__name__)\n", + " self.f()\n", + " print(\"Exited\", self.f.__name__)\n", + "\n", + "@entry_exit\n", + "def func1():\n", + " print(\"inside func1()\")\n", + "\n", + "@entry_exit\n", + "def func2():\n", + " print(\"inside func2()\")\n", + "\n", + "func1()\n", + "func2()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inside __init__()\n", + "Inside __call__()\n", + "After decoration\n", + "Preparing to call sayHello()\n", + "Inside wrapped_f()\n", + "Decorator arguments: hello world 42\n", + "sayHello arguments: say hello argument list\n", + "After f(*args)\n", + "after first sayHello() call\n", + "Inside wrapped_f()\n", + "Decorator arguments: hello world 42\n", + "sayHello arguments: a different set of arguments\n", + "After f(*args)\n", + "after second sayHello() call\n" + ] + } + ], + "source": [ + "class decorator_with_arguments(object):\n", + "\n", + " def __init__(self, arg1, arg2, arg3):\n", + " \"\"\"\n", + " If there are decorator arguments, the function\n", + " to be decorated is not passed to the constructor!\n", + " \"\"\"\n", + " print(\"Inside __init__()\")\n", + " self.arg1 = arg1\n", + " self.arg2 = arg2\n", + " self.arg3 = arg3\n", + "\n", + " def __call__(self, f):\n", + " \"\"\"\n", + " If there are decorator arguments, __call__() is only called\n", + " once, as part of the decoration process! You can only give\n", + " it a single argument, which is the function object.\n", + " \"\"\"\n", + " print(\"Inside __call__()\")\n", + " def wrapped_f(*args):\n", + " print(\"Inside wrapped_f()\")\n", + " print(\"Decorator arguments:\", self.arg1, self.arg2, self.arg3)\n", + " f(*args)\n", + " print(\"After f(*args)\")\n", + " return wrapped_f\n", + "\n", + "@decorator_with_arguments(\"hello\", \"world\", 42)\n", + "def sayHello(a1, a2, a3, a4):\n", + " print('sayHello arguments:', a1, a2, a3, a4)\n", + "\n", + "print(\"After decoration\")\n", + "\n", + "print(\"Preparing to call sayHello()\")\n", + "sayHello(\"say\", \"hello\", \"argument\", \"list\")\n", + "print(\"after first sayHello() call\")\n", + "sayHello(\"a\", \"different\", \"set of\", \"arguments\")\n", + "print(\"after second sayHello() call\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "f = " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson03/Lesson3.ipynb b/Student/Ruohan/lesson03/Lesson3.ipynb new file mode 100644 index 0000000..c13fac3 --- /dev/null +++ b/Student/Ruohan/lesson03/Lesson3.ipynb @@ -0,0 +1,579 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def add(x,y):\n", + " print(\"add func\")\n", + " return x+y\n", + "decorators.add = logged(decorators.add)\n", + " ## Same as\n", + " \n", + "\n", + "@logged\n", + "def add(x,y):\n", + " print(\"add func\")\n", + " return x+y\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Decorators\n", + "\n", + "def null_decorator(func):\n", + " return func\n", + "\n", + "def greet():\n", + " return 'Hello!'\n", + "\n", + "greet = null_decorator(greet)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "greet()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Above same as this ..\n", + "\n", + "@null_decorator\n", + "def greet():\n", + " return 'Hello!'\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "greet()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Decorators\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from functools import wraps\n", + "def nullable(function):\n", + " @wraps(function)\n", + " def null_wrapper(arg):\n", + " return None if arg is None else function(arg)\n", + " return null_wrapper" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "nlog = nullable(math.log)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "some_data = [10, 100, None, 50, 60]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "scaled = map(nlog, some_data) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "list(scaled)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Enter Context Manager" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "## Context Manager\n", + "\n", + "f = open(\"x.txt\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<_io.TextIOWrapper name='x.txt' mode='r' encoding='UTF-8'>" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f.__enter__()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'h'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f.read(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "## now lets run exit to the close the file\n", + "f.__exit__(None, None, None)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "I/O operation on closed file.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m## try to read once again\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: I/O operation on closed file." + ] + } + ], + "source": [ + "## try to read once again\n", + "f.read(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Context Manager Summary \n", + "## so to open a file, process its contents, and make sure to close it, \n", + "## you can simply do:\n", + "\n", + "with open(\"x.txt\") as f:\n", + " data = f.read()\n", + " do something with data\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Acquiring a lock during threading\n", + "\n", + "import threading\n", + "lock=threading.Lock()\n", + "lock.acquire()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Use the lock\n" + ] + } + ], + "source": [ + "print(\"Use the lock\")\n", + "lock.release()\n", + "\n", + "## you acquire a resource, use it and then close it. \n", + "## but it is easy to close or release the resources\n", + "## Context Manager\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "## Context Manager\n", + "\n", + "class Context(object):\n", + "\n", + " def __init__(self, handle_error):\n", + " print ('__init__(%s)' % handle_error)\n", + " self.handle_error = handle_error\n", + " def __enter__(self):\n", + " print ('__enter__()')\n", + " return self\n", + " def __exit__(self, exc_type, exc_val, exc_tb):\n", + " print ('__exit__(%s, %s, %s)' % (exc_type, exc_val, exc_tb))\n", + " return self.handle_error" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "__init__(True)\n", + "__enter__()\n", + "This is in the context\n", + "__exit__(None, None, None)\n" + ] + } + ], + "source": [ + "with Context(True) as foo:\n", + " print ('This is in the context')\n", + " ## run once and the uncomment following line\n", + " ##raise RuntimeError('this is the error message')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## In the above example your code is bracketed by enter and exit\n", + "\n", + "## Because the exit method returns True, the raised error is ‘handled’.\n", + "## What if we try with False?\n", + "\n", + "with Context(False) as foo:\n", + " print ('This is in the context')\n", + " ## run this with and without commenting following line\n", + " ##raise RuntimeError('this is the error message')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## contextlib.contextmanager() \n", + "## will turn a generator function into context manager." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from contextlib import contextmanager\n", + "\n", + "@contextmanager\n", + "def context(boolean):\n", + " print (\"__init__ code here\")\n", + " try:\n", + " print (\"__enter__ code goes here\")\n", + " yield object()\n", + " except Exception as e:\n", + " print (\"errors handled here\")\n", + " if not boolean:\n", + " raise\n", + " finally:\n", + " print (\"__exit__ cleanup goes here\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import contextlib\n", + "import http.client\n", + "with contextlib.closing(\n", + " http.client.HTTPConnection(\"www.example.com\")) as host:\n", + " host.request(\"GET\", \"/path/to/resources/12345/\")\n", + " response= host.getresponse()\n", + " print(response.read())" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Entered\n", + "Closed\n", + "Start of Function\n", + "Entered\n", + "Closed\n", + "End of Function\n", + "Entered\n", + "EOF\n" + ] + } + ], + "source": [ + "from __future__ import print_function\n", + "import contextlib\n", + "\n", + "@contextlib.contextmanager\n", + "def manager():\n", + " \"\"\"Easiest way to get a custom context manager...\"\"\"\n", + " try:\n", + " print('Entered')\n", + " yield\n", + " finally:\n", + " print('Closed')\n", + "\n", + "\n", + "def gen():\n", + " \"\"\"Just a generator with a context manager inside.\n", + "\n", + " When the context is entered, we'll see \"Entered\" on the console\n", + " and when exited, we'll see \"Closed\" on the console.\n", + " \"\"\"\n", + " man = manager()\n", + " with man:\n", + " for i in range(10):\n", + " yield i\n", + "\n", + "\n", + "# Test what happens when we consume a generator.\n", + "list(gen())\n", + "\n", + "def fn():\n", + " g = gen()\n", + " next(g)\n", + " # g.close()\n", + "\n", + "# Test what happens when the generator gets garbage collected inside\n", + "# a function\n", + "print('Start of Function')\n", + "fn()\n", + "print('End of Function')\n", + "\n", + "# Test what happens when a generator gets garbage collected outside\n", + "# a function. IIRC, this isn't _guaranteed_ to happen in all cases.\n", + "g = gen()\n", + "next(g)\n", + "# g.close()\n", + "print('EOF')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Context Manager Examples - Advanced Usecase. \n", + "## yield without argument is semantically equivalent to yield None\n", + "\n", + "from contextlib import contextmanager\n", + "import sys\n", + "\n", + "@contextmanager\n", + "def redirected(**kwds):\n", + " stream_names = [\"stdin\", \"stdout\", \"stderr\"]\n", + " old_streams = {}\n", + " try:\n", + " for sname in stream_names:\n", + " stream = kwds.get(sname, None)\n", + " if stream is not None and stream != getattr(sys, sname):\n", + " old_streams[sname] = getattr(sys, sname)\n", + " setattr(sys, sname, stream)\n", + " yield\n", + " finally:\n", + " for sname, stream in old_streams.items():\n", + " setattr(sys, sname, stream)\n", + "\n", + "with redirected(stdout=open(\"/tmp/uw-py220-log-context-mgr.txt\", \"w\")):\n", + " # these print statements will go to /tmp/log.txt\n", + " print (\"Test entry 1\")\n", + " print (\"Test entry 2\")\n", + "# back to the normal stdout\n", + "print (\"Back to normal stdout again\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Yield in Context Manager\n", + "\n", + "yield expression returns control to the whatever is using the generator. \n", + "The generator pauses at this point, which means that the @contextmanager \n", + "decorator knows that the code is done with the setup part.\n", + "\n", + "In other words, everything you want to do in the context manager __enter__ \n", + "phase has to take place before the yield.\n", + "\n", + "Once your context exits (so the block under the with statement is done), \n", + "the @contextmanager decorator is called for the __exit__ part of the \n", + "context manager protocol and will do one of two things:\n", + "\n", + "If there was no exception, it'll resume your generator. So your generator \n", + "unpauses at the yield line, and you enter the cleanup phase, the part\n", + "\n", + "If there was an exception, the decorator uses generator.throw() to raise \n", + "that exception in the generator. It'll be as if the yield line caused \n", + "that exception. Because you have a finally clause, it'll be executed \n", + "before your generator exits because of the exception." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Recursion\n", + "\n", + "## Recursion to use up all stack space. \n", + "## RuntimeError: maximum recursion depth exceeded\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def f():\n", + " print(\"Hello\")\n", + " f()\n", + "\n", + "f()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson03/btpermute.py b/Student/Ruohan/lesson03/btpermute.py new file mode 100644 index 0000000..677d892 --- /dev/null +++ b/Student/Ruohan/lesson03/btpermute.py @@ -0,0 +1,8 @@ +def permute(list, s): + if list == 1: + return s + else: + return [ y + x + for y in permute(1, s) + for x in permute(list - 1, s) + ] diff --git a/Student/Ruohan/lesson03/class_decorator-ver2.py b/Student/Ruohan/lesson03/class_decorator-ver2.py new file mode 100644 index 0000000..d360cdb --- /dev/null +++ b/Student/Ruohan/lesson03/class_decorator-ver2.py @@ -0,0 +1,20 @@ +class entry_exit(object): + + def __init__(self, f): + self.f = f + + def __call__(self): + print("Entering", self.f.__name__) + self.f() + print("Exited", self.f.__name__) + +@entry_exit +def func1(): + print("inside func1()") + +@entry_exit +def func2(): + print("inside func2()") + +func1() +func2() diff --git a/Student/Ruohan/lesson03/class_decorator.py b/Student/Ruohan/lesson03/class_decorator.py new file mode 100644 index 0000000..fea763f --- /dev/null +++ b/Student/Ruohan/lesson03/class_decorator.py @@ -0,0 +1,16 @@ +class my_decorator(object): + + def __init__(self, f): + print("inside my_decorator.__init__()") + f() # Prove that function definition has completed + + def __call__(self): + print("inside my_decorator.__call__()") + +@my_decorator +def aFunction(): + print("inside aFunction()") + +print("Finished decorating aFunction()") + +aFunction() diff --git a/Student/Ruohan/lesson03/class_decorator_with_arguments.py b/Student/Ruohan/lesson03/class_decorator_with_arguments.py new file mode 100644 index 0000000..1337ee8 --- /dev/null +++ b/Student/Ruohan/lesson03/class_decorator_with_arguments.py @@ -0,0 +1,37 @@ +class decorator_with_arguments(object): + + def __init__(self, arg1, arg2, arg3): + """ + If there are decorator arguments, the function + to be decorated is not passed to the constructor! + """ + print("Inside __init__()") + self.arg1 = arg1 + self.arg2 = arg2 + self.arg3 = arg3 + + def __call__(self, f): + """ + If there are decorator arguments, __call__() is only called + once, as part of the decoration process! You can only give + it a single argument, which is the function object. + """ + print("Inside __call__()") + def wrapped_f(*args): + print("Inside wrapped_f()") + print("Decorator arguments:", self.arg1, self.arg2, self.arg3) + f(*args) + print("After f(*args)") + return wrapped_f + +@decorator_with_arguments("hello", "world", 42) +def sayHello(a1, a2, a3, a4): + print('sayHello arguments:', a1, a2, a3, a4) + +print("After decoration") + +print("Preparing to call sayHello()") +sayHello("say", "hello", "argument", "list") +print("after first sayHello() call") +sayHello("a", "different", "set of", "arguments") +print("after second sayHello() call") diff --git a/Student/Ruohan/lesson03/decorators.py b/Student/Ruohan/lesson03/decorators.py new file mode 100644 index 0000000..51ab099 --- /dev/null +++ b/Student/Ruohan/lesson03/decorators.py @@ -0,0 +1,137 @@ +## start with this ... + +## make decorators.py file + +def add(x,y): + return x+y + +def sub(x,y): + return x-y + +def mul(x,y): + return x*y + +### calling this in the repl + +>>> import decorators.py + + +## Add prints + +def add(x,y): + print("Calling add") + return x+y + +def sub(x,y): + print("Calling sub") + return x-y + +def mul(x,y): + print("Calling mul") + return x*y + +### calling this in the repl + +>>> import decorators.py + +## Now, you have to do something else or change all these edtis, +## we would have to go back and change our code in 100s of place + +## this is turning into a maintenance nightmare +## also the code is repeated - which goes against the concept of programming + +## Lets take this code and generalize this + +## You are going to give me a function and I will write a wrapper around it + +## file name logcall.py + +def logged(func): + # adding logging + def wrapper(*args, **kwargs): + print('Calling', func.__name__) + return func(*args, **kwargs) + return wrapper + +## Once that is done, then +## import the logcall.py + +## create decorators.py file + +from logcall import logged + +add = logged(add) +sub = logged(sub) + +## loging has been isolated in place +## but we have manually wrap the logged function +## like this + + +def add(x,y): + return x+y +add=logged(add) + +def sub(x,y): + return x-y +sub=logged(sub) + +def mul(x,y): + return x*y +mul=logged(mul) + +## this is the idea behind decoration +## go back to theory and then come to next phase - + + +## This is still not ideal as we would have to do this for all our n number +## of functions + +def add(x,y): + return x+y +add=logged(add) + +## can be turned into + +@logged +def add(x,y): + return x+y + +## we would have to add @logged for all the 500 functions .... +## These wrapper functions we create do not look anything else original +## functions - # type this + +>>> decorators.add +>>> decorators.sub +>>> help (decorators.add) + +## it doesn't show anything about the original function - just says wrapper. + +## So, when you define a wrapper, you need to get some documentation over +## from original function + +## This is done using wraps from functools. +## so in the logcall.py, add wraps at the top + +from functools import wraps + +@wraps(func) +def logged(func): + # adding logging + def wrapper(*args, **kwargs): + print('Calling', func.__name__) + return func(*args, **kwargs) + + return wrapper + +## end logcall file + +## adding wraps is equivilant to +## wraper.__name__=func.__name__ +## wrapper.__doc__=func.__doc__ + +## now try + +>>> decorators.add +>>> decorators.sub +>>> help (decorators.add) diff --git a/Student/Ruohan/lesson03/factorial.py b/Student/Ruohan/lesson03/factorial.py new file mode 100644 index 0000000..8ef75c5 --- /dev/null +++ b/Student/Ruohan/lesson03/factorial.py @@ -0,0 +1,10 @@ +#! /usr/local/bin/python3 + +def factorial(n): + if n == 0: + return 1 + else: + return n * factorial(n - 1) + +#compute factorial of 7 +print(factorial(7)) diff --git a/Student/Ruohan/lesson03/locke.py b/Student/Ruohan/lesson03/locke.py new file mode 100644 index 0000000..2ea9644 --- /dev/null +++ b/Student/Ruohan/lesson03/locke.py @@ -0,0 +1,37 @@ +#! /usr/local/bin/python3 + +class Locke(object): + + def __init__(self, num, error = None): + self.capacity = num + self.handle_error = error + + def __enter__(self): + if not self.handle_error: + print("Stopping the pumps.\nOpening the doors.\nClosing the doors.\nRestarting the pumps.\n") + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + if not self.handle_error: + print("Stopping the pumps.\nOpening the doors.\nClosing the doors.\nRestarting the pumps.\n") + return self.handle_error + + def move_boats_through(self, boats): + if boats > self.capacity: + raise ValueError('The boats beyoned the capacity') + else: + + return self + + +small_locke = Locke(5) +large_locke = Locke(10) +boats = 8 + +# Too many boats through a small locke will raise an exception +with small_locke as locke: + locke.move_boats_through(boats) + +# A lock with sufficient capacity can move boats without incident. +with large_locke as locke: + locke.move_boats_through(boats) diff --git a/Student/Ruohan/lesson03/x.txt b/Student/Ruohan/lesson03/x.txt new file mode 100644 index 0000000..95d09f2 --- /dev/null +++ b/Student/Ruohan/lesson03/x.txt @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/Student/Ruohan/lesson04/.ipynb_checkpoints/Lesson 4-checkpoint.ipynb b/Student/Ruohan/lesson04/.ipynb_checkpoints/Lesson 4-checkpoint.ipynb new file mode 100644 index 0000000..cd0ac4b --- /dev/null +++ b/Student/Ruohan/lesson04/.ipynb_checkpoints/Lesson 4-checkpoint.ipynb @@ -0,0 +1,569 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise #1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b\n", + "a\n" + ] + } + ], + "source": [ + "def a(func):\n", + " print('a')\n", + " return func\n", + "\n", + "def b(func):\n", + " print('b')\n", + " return func\n", + "\n", + "@a\n", + "@b\n", + "def foo():\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Did you know b printed before a?\n", + "## That's because the decorator closest to def will be called first\n", + "## Then its return value is passed to the 1st decorator\n", + "## Eventually, the return value of the topmost decorator will be \n", + "## assigned to the function's name in the containing scope" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise #2\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Staticmethod -\n", + "class bla:\n", + " \n", + "@staticmethod\n", + "def f(arg1, arg2, arg3):\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Demo of @classmethod and @staticmethod\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "class Date(object):\n", + "\n", + " def __init__(self, day=0, month=0, year=0):\n", + " self.day = day\n", + " self.month = month\n", + " self.year = year\n", + "\n", + " @classmethod\n", + " def from_string(cls, date_as_string):\n", + " day, month, year = map(int, date_as_string.split('-'))\n", + " date1 = cls(day, month, year)\n", + " return date1\n", + "\n", + " @staticmethod\n", + " def is_date_valid(date_as_string):\n", + " day, month, year = map(int, date_as_string.split('-'))\n", + " return day <= 31 and month <= 12 and year <= 3999\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "date2 = Date.from_string('06-04-2018')\n", + "is_date = Date.is_date_valid('06-05-2018')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<__main__.Date object at 0x1083eefd0>\n" + ] + } + ], + "source": [ + "print (date2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m Python four namespace: local, enclosed, global, builtin\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "Python four namespace: local, enclosed, global, builtin" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## namespace Example\n", + "class C:\n", + " a_class_attribute = 0\n", + " def __init__(self):\n", + " self.an_instance_attribute = 0\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Note this is only checks the format of the date string not the accuracy\n", + "## of the date .\n", + "print(is_date)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Exercise #3\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Awesome(object):\n", + " def fu(self,x):\n", + " print (\"About to execute fu(%s,%s)\"%(self,x))\n", + "\n", + " @classmethod\n", + " def class_fu(cls,x):\n", + " print (\"About to execute class_fu(%s,%s)\"%(cls,x))\n", + "\n", + " @staticmethod\n", + " def static_fu(x):\n", + " print (\"executing static_fu(%s)\"%x) \n", + "\n", + "myobj=Awesome() " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## usual way an object instance calls a method. \n", + "## The object instance, myobj, is implicitly passed as the first argument.\n", + "## \n", + "myobj.fu(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## @staticmethod\n", + "\n", + "## Lets do how @staticmethod works\n", + "## neither self (the object instance) nor cls (the class) is implicitly \n", + "## passed as the first argument. They behave like plain functions except \n", + "## that you can call them from an instance or the class:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myobj.static_fu(1)\n", + "# executing static_fu(1)\n", + "\n", + "Awesome.static_fu('I am awesome')\n", + "# executing static_fu('I am Awesome')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Staticmethods are used to group functions which have some logical \n", + "## connection with a class to the class.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "##Classmethod\n", + "## the class of the object instance is implicitly passed as the first \n", + "## argument instead of self.\n", + "\n", + "myobj.class_fu(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## You can also call class_fu using the class. \n", + "## If you define something to be a classmethod, it is probably \n", + "## because you intend to call it from the class rather than from a \n", + "## class instance. \n", + "\n", + "## Note the following\n", + "## Awesome.fu(1) would have raised a TypeError, \n", + "## but Awesome.class_fu(1) works just fine:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "Awesome.fu(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Awesome.class_fu(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## End of classmethod\n", + "\n", + "## To note\n", + "## fu expects 2 arguments, while myobj.fu only expects 1 argument.\n", + "## myobj is bound to fu\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(myobj.fu)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## With myobj.class_fu, myobj is not bound to class_fu, rather the class \n", + "## Awesome is bound to class_fu.\n", + "\n", + "print(Awesome.class_fu)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Here, with a staticmethod, even though it is a method, \n", + "## myobj.static_fu just returns with no arguments \n", + "## bound. static_fu expects 1 argument, and myobj.static_fu expects \n", + "## 1 argument too.\n", + "\n", + "print(myobj.static_fu)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise 4\n", + "\n", + "## getattr () example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Person():\n", + " name = 'Wayne'\n", + " def say(self, what):\n", + " print(self.name, what)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "getattr(Person, 'name')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "attr_name = 'name'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "person = Person()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "getattr(person, attr_name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "getattr(person, 'say')('Welcome to Python 220')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## getattr will raise AttributeError if attribute \n", + "## with the given name does not exist in the object:\n", + "getattr(person, 'age')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise 5\n", + "\n", + "## setattr()\n", + "\n", + "setattr(person, 'name', 'Tamara')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## accessing the instance attribute\n", + "person.name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## accessing the class attribute\n", + "Person.name\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise 6\n", + "\n", + "## type Class\n", + "## applying \"type\" to an object returns the class of which the object \n", + "## is an instance of\n", + "x = [1, 2, 3]\n", + "y = \"Hello Py 220 Class\"\n", + "print(type(x), type(y))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## object x is an instance of class list\n", + "## object y is an instance of class str\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## If you apply type on the name of a \n", + "## class itself, you get the class \"type\" returned.\n", + "\n", + "print(type(list), type(str))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Above command is same as \n", + "\n", + "print(type(x), type(y))\n", + "print(type(type(x)), type(type(y)))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## A user-defined class (or the class \"object\") is an instance of the \n", + "## class \"type\". So, we can see, that classes are created from type. \n", + "## In Python3 there is no difference between \"classes\" and \"types\". \n", + "## They are in most cases used as synonyms.\n", + "\n", + "\n", + "## The fact that classes are instances of a class \"type\" allows us to \n", + "## program metaclasses. We can create classes, which inherit from the class \n", + "## \"type\". So, a metaclass is a subclass of the class \"type\".\n", + "\n", + "## Enter Metaclasses\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson04/Lesson 4.ipynb b/Student/Ruohan/lesson04/Lesson 4.ipynb new file mode 100644 index 0000000..6694e43 --- /dev/null +++ b/Student/Ruohan/lesson04/Lesson 4.ipynb @@ -0,0 +1,719 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise #1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b\n", + "a\n" + ] + } + ], + "source": [ + "def a(func):\n", + " print('a')\n", + " return func\n", + "\n", + "def b(func):\n", + " print('b')\n", + " return func\n", + "\n", + "@a\n", + "@b\n", + "def foo():\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Did you know b printed before a?\n", + "## That's because the decorator closest to def will be called first\n", + "## Then its return value is passed to the 1st decorator\n", + "## Eventually, the return value of the topmost decorator will be \n", + "## assigned to the function's name in the containing scope" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise #2\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Staticmethod -\n", + "class bla:\n", + " \n", + "@staticmethod\n", + "def f(arg1, arg2, arg3):\n", + " pass\n", + "\n", + "@class\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Demo of @classmethod and @staticmethod\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "class Date(object):\n", + "\n", + " def __init__(self, day=0, month=0, year=0):\n", + " self.day = day\n", + " self.month = month\n", + " self.year = year\n", + "\n", + " @classmethod\n", + " def from_string(cls, date_as_string):\n", + " day, month, year = map(int, date_as_string.split('-'))\n", + " date1 = cls(day, month, year)\n", + " return date1\n", + "\n", + " @staticmethod\n", + " def is_date_valid(date_as_string):\n", + " day, month, year = map(int, date_as_string.split('-'))\n", + " return day <= 31 and month <= 12 and year <= 3999\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "date2 = Date.from_string('06-04-2018')\n", + "is_date = Date.is_date_valid('06-05-2018')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<__main__.Date object at 0x1083eefd0>\n" + ] + } + ], + "source": [ + "print (date2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m Python four namespace: local, enclosed, global, builtin\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "Python four namespace: local, enclosed, global, builtin" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "## namespace Example\n", + "class C:\n", + " a_class_attribute = 0\n", + " def __init__(self):\n", + " self.an_instance_attribute = 0\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "c = C()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '__weakref__',\n", + " 'a_class_attribute',\n", + " 'an_instance_attribute']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'an_instance_attribute': 0}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vars(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "mappingproxy({'__dict__': ,\n", + " '__doc__': None,\n", + " '__init__': ,\n", + " '__module__': '__main__',\n", + " '__weakref__': ,\n", + " 'a_class_attribute': 0})" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vars(C)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '__weakref__',\n", + " 'a_class_attribute']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(C)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Note this is only checks the format of the date string not the accuracy\n", + "## of the date .\n", + "print(is_date)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Exercise #3\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Awesome(object):\n", + " def fu(self,x):\n", + " print (\"About to execute fu(%s,%s)\"%(self,x))\n", + "\n", + " @classmethod\n", + " def class_fu(cls,x):\n", + " print (\"About to execute class_fu(%s,%s)\"%(cls,x))\n", + "\n", + " @staticmethod\n", + " def static_fu(x):\n", + " print (\"executing static_fu(%s)\"%x) \n", + "\n", + "myobj=Awesome() " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## usual way an object instance calls a method. \n", + "## The object instance, myobj, is implicitly passed as the first argument.\n", + "## \n", + "myobj.fu(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## @staticmethod\n", + "\n", + "## Lets do how @staticmethod works\n", + "## neither self (the object instance) nor cls (the class) is implicitly \n", + "## passed as the first argument. They behave like plain functions except \n", + "## that you can call them from an instance or the class:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myobj.static_fu(1)\n", + "# executing static_fu(1)\n", + "\n", + "Awesome.static_fu('I am awesome')\n", + "# executing static_fu('I am Awesome')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Staticmethods are used to group functions which have some logical \n", + "## connection with a class to the class.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "##Classmethod\n", + "## the class of the object instance is implicitly passed as the first \n", + "## argument instead of self.\n", + "\n", + "myobj.class_fu(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## You can also call class_fu using the class. \n", + "## If you define something to be a classmethod, it is probably \n", + "## because you intend to call it from the class rather than from a \n", + "## class instance. \n", + "\n", + "## Note the following\n", + "## Awesome.fu(1) would have raised a TypeError, \n", + "## but Awesome.class_fu(1) works just fine:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "Awesome.fu(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Awesome.class_fu(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## End of classmethod\n", + "\n", + "## To note\n", + "## fu expects 2 arguments, while myobj.fu only expects 1 argument.\n", + "## myobj is bound to fu\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(myobj.fu)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## With myobj.class_fu, myobj is not bound to class_fu, rather the class \n", + "## Awesome is bound to class_fu.\n", + "\n", + "print(Awesome.class_fu)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Here, with a staticmethod, even though it is a method, \n", + "## myobj.static_fu just returns with no arguments \n", + "## bound. static_fu expects 1 argument, and myobj.static_fu expects \n", + "## 1 argument too.\n", + "\n", + "print(myobj.static_fu)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise 4\n", + "\n", + "## getattr () example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Person():\n", + " name = 'Wayne'\n", + " def say(self, what):\n", + " print(self.name, what)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "getattr(Person, 'name')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "attr_name = 'name'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "person = Person()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "getattr(person, attr_name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "getattr(person, 'say')('Welcome to Python 220')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## getattr will raise AttributeError if attribute \n", + "## with the given name does not exist in the object:\n", + "getattr(person, 'age')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise 5\n", + "\n", + "## setattr()\n", + "\n", + "setattr(person, 'name', 'Tamara')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## accessing the instance attribute\n", + "person.name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## accessing the class attribute\n", + "Person.name\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Exercise 6\n", + "\n", + "## type Class\n", + "## applying \"type\" to an object returns the class of which the object \n", + "## is an instance of\n", + "x = [1, 2, 3]\n", + "y = \"Hello Py 220 Class\"\n", + "print(type(x), type(y))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## object x is an instance of class list\n", + "## object y is an instance of class str\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## If you apply type on the name of a \n", + "## class itself, you get the class \"type\" returned.\n", + "\n", + "print(type(list), type(str))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Above command is same as \n", + "\n", + "print(type(x), type(y))\n", + "print(type(type(x)), type(type(y)))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## A user-defined class (or the class \"object\") is an instance of the \n", + "## class \"type\". So, we can see, that classes are created from type. \n", + "## In Python3 there is no difference between \"classes\" and \"types\". \n", + "## They are in most cases used as synonyms.\n", + "\n", + "\n", + "## The fact that classes are instances of a class \"type\" allows us to \n", + "## program metaclasses. We can create classes, which inherit from the class \n", + "## \"type\". So, a metaclass is a subclass of the class \"type\".\n", + "\n", + "## Enter Metaclasses\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson04/json/.cache/v/cache/lastfailed b/Student/Ruohan/lesson04/json/.cache/v/cache/lastfailed new file mode 100644 index 0000000..051b538 --- /dev/null +++ b/Student/Ruohan/lesson04/json/.cache/v/cache/lastfailed @@ -0,0 +1,12 @@ +{ + "json_save/test/test_savables.py::test_bad_dicts[val4]": true, + "json_save/test/test_savables.py::test_basics": true, + "json_save/test/test_savables.py::test_basics[Tuple-val5]": true, + "json_save/test/test_savables.py::test_containers[Dict-val0]": true, + "json_save/test/test_savables.py::test_dicts[Dict-val0]": true, + "json_save/test/test_savables.py::test_dicts[Dict-val1]": true, + "json_save/test/test_savables.py::test_dicts[Dict-val2]": true, + "json_save/test/test_savables.py::test_dicts[Dict-val3]": true, + "json_save/test/test_savables.py::test_dicts[val10]": true, + "json_save/test/test_savables.py::test_dicts[val7]": true +} \ No newline at end of file diff --git a/Student/Ruohan/lesson04/json/README.txt b/Student/Ruohan/lesson04/json/README.txt new file mode 100644 index 0000000..a80b252 --- /dev/null +++ b/Student/Ruohan/lesson04/json/README.txt @@ -0,0 +1,4 @@ +This is a simple meta-class based system for saving objects in the JSON format. + +It can make any arbitrary class savable and re-loadable from JSON. + diff --git a/Student/Ruohan/lesson04/json/examples/example_dec.py b/Student/Ruohan/lesson04/json/examples/example_dec.py new file mode 100755 index 0000000..263b6c7 --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/example_dec.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +""" +Examples of using json_save +""" + +import json_save.json_save_dec as js + + +# Examples using the decorator + +@js.json_save +class MyClass: + + x = js.Int() + y = js.Float() + lst = js.List() + + def __init__(self, x, lst): + self.x = x + self.lst = lst + + +@js.json_save +class OtherSaveable: + + foo = js.String() + bar = js.Int() + + def __init__(self, foo, bar): + self.foo = foo + self.bar = bar + + +# create one: +print("about to create a instance") +mc = MyClass(5, [3, 5, 7, 9]) + +print(mc) + +jc = mc.to_json_compat() + +# re-create it from the dict: +mc2 = MyClass.from_json_dict(jc) + +print(mc2 == "fred") + +assert mc2 == mc + +print(mc.to_json()) + +# now try it nested... +mc_nest = MyClass(34, [OtherSaveable("this", 2), + OtherSaveable("that", 64), + ]) + +mc_nest_comp = mc_nest.to_json_compat() +print(mc_nest_comp) + +# can we re-create it? +mc_nest2 = MyClass.from_json_dict(mc_nest_comp) + +print(mc_nest) +print(mc_nest2) + +assert mc_nest == mc_nest2 diff --git a/Student/Ruohan/lesson04/json/examples/example_meta.py b/Student/Ruohan/lesson04/json/examples/example_meta.py new file mode 100755 index 0000000..13719ab --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/example_meta.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +""" +Examples of using json_save +""" + +import json_save.json_save_meta as js + +# Metaclass examples + +class MyClass(js.JsonSaveable): + + x = js.Int() + y = js.Float() + lst = js.List() + + def __init__(self, x, lst): + self.x = x + self.lst = lst + + +class OtherSaveable(js.JsonSaveable): + + foo = js.String() + bar = js.Int() + + def __init__(self, foo, bar): + self.foo = foo + self.bar = bar + +# create one: +print("about to create a instance") +mc = MyClass(5, [3, 5, 7, 9]) + +print(mc) + +jc = mc.to_json_compat() + +# re-create it from the dict: +mc2 = MyClass.from_json_dict(jc) + +print(mc2 == "fred") + +assert mc2 == mc + +print(mc.to_json()) + +# now try it nested... +mc_nest = MyClass(34, [OtherSaveable("this", 2), + OtherSaveable("that", 64), + ]) + +mc_nest_comp = mc_nest.to_json_compat() +print(mc_nest_comp) + +# can we re-create it? +mc_nest2 = MyClass.from_json_dict(mc_nest_comp) + +print(mc_nest) +print(mc_nest2) + +assert mc_nest == mc_nest2 + diff --git a/Student/Ruohan/lesson04/json/examples/json_save/__init__.py b/Student/Ruohan/lesson04/json/examples/json_save/__init__.py new file mode 100644 index 0000000..db59bd9 --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/json_save/__init__.py @@ -0,0 +1,8 @@ +""" +json_save package + +Pulling in names from the other packages. +""" + +__version__ = "0.4.0" + diff --git a/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/__init__.cpython-36.pyc b/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..fb9d73a Binary files /dev/null and b/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/__init__.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/json_save_dec.cpython-36.pyc b/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/json_save_dec.cpython-36.pyc new file mode 100644 index 0000000..8058371 Binary files /dev/null and b/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/json_save_dec.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/json_save_meta.cpython-36.pyc b/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/json_save_meta.cpython-36.pyc new file mode 100644 index 0000000..b032cda Binary files /dev/null and b/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/json_save_meta.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/saveables.cpython-36.pyc b/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/saveables.cpython-36.pyc new file mode 100644 index 0000000..9b718b8 Binary files /dev/null and b/Student/Ruohan/lesson04/json/examples/json_save/__pycache__/saveables.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson04/json/examples/json_save/json_save_dec.py b/Student/Ruohan/lesson04/json/examples/json_save/json_save_dec.py new file mode 100644 index 0000000..5af4de9 --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/json_save/json_save_dec.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python + +""" +json_save implemented as a decorator +""" + +import json +from pathlib import Path + +from .json_save_meta import * + + +# assorted methods that will need to be added to the decorated class: +def _to_json_compat(self): + """ + converts this object to a json-compatible dict. + + returns the dict + """ + # add and __obj_type attribute, so it can be reconstructed + dic = {"__obj_type": self.__class__.__qualname__} + for attr, typ in self._attrs_to_save.items(): + dic[attr] = typ.to_json_compat(getattr(self, attr)) + return dic + + +def __eq__(self, other): + """ + default equality method that checks if all of the saved attributes + are equal + """ + for attr in self._attrs_to_save: + try: + if getattr(self, attr) != getattr(other, attr): + return False + except AttributeError: + return False + return True + +@classmethod +def _from_json_dict(cls, dic): + """ + creates an instance of this class populated by the contents of + the json compatible dict + + the object is created with __new__ before setting the attributes + + NOTE: __init__ is not called. + There should not be any extra initialization required in __init__ + """ + # create a new object + obj = cls.__new__(cls) + for attr, typ in cls._attrs_to_save.items(): + setattr(obj, attr, typ.to_python(dic[attr])) + return obj + + +def __new__(cls, *args, **kwargs): + """ + This adds instance attributes to assure they are all there, even if + they are not set in the subclasses __init__ + + it's in __new__ so that it will get called before the decorated class' + __init__ -- the __init__ will override anything here. + """ + # create the instance by calling the base class __new__ + obj = cls.__base__.__new__(cls) + # using super() did not work here -- why?? + # set the instance attributes to defaults + for attr, typ in cls._attrs_to_save.items(): + setattr(obj, attr, typ.default) + return obj + + +def _to_json(self, fp=None, indent=4): + """ + Converts the object to JSON + + :param fp=None: an open file_like object to write the json to. + If it is None, then a string with the JSON + will be returned as a string + + :param indent=4: The indentation level desired in the JSON + """ + if fp is None: + return json.dumps(self.to_json_compat(), indent=indent) + else: + json.dump(self.to_json_compat(), fp, indent=indent) + + +# now the actual decorator +def json_save(cls): + """ + json_save decorator + + makes decorated classes Saveable to json + """ + # make sure this is decorating a class object + if type(cls) is not type: + raise TypeError("json_save can only be used on classes") + + # find the saveable attributes + # these will the attributes that get saved and reconstructed from json. + # each class object gets its own dict + attr_dict = vars(cls) + cls._attrs_to_save = {} + for key, attr in attr_dict.items(): + if isinstance(attr, Saveable): + cls._attrs_to_save[key] = attr + if not cls._attrs_to_save: + raise TypeError(f"{cls.__name__} class has no saveable attributes.\n" + " Note that Savable attributes must be instances") + # register this class so we can re-construct instances. + Saveable.ALL_SAVEABLES[cls.__qualname__] = cls + + # add the methods: + cls.__new__ = __new__ + cls.to_json_compat = _to_json_compat + cls.__eq__ = __eq__ + cls.from_json_dict = _from_json_dict + cls.to_json = _to_json + + return cls + + +# utilities for loading arbitrary objects from json +def from_json_dict(j_dict): + """ + factory function that creates an arbitrary JsonSaveable + object from a json-compatible dict. + """ + # determine the class it is. + obj_type = j_dict["__obj_type"] + obj = Saveable.ALL_SAVEABLES[obj_type].from_json_dict(j_dict) + return obj + + +def from_json(_json): + """ + Factory function that re-creates a JsonSaveable object + from a json string or file + """ + if isinstance(_json, (str, Path)): + return from_json_dict(json.loads(_json)) + else: # assume a file-like object + return from_json_dict(json.load(_json)) diff --git a/Student/Ruohan/lesson04/json/examples/json_save/json_save_meta.py b/Student/Ruohan/lesson04/json/examples/json_save/json_save_meta.py new file mode 100644 index 0000000..5b8dfa8 --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/json_save/json_save_meta.py @@ -0,0 +1,211 @@ +#!/usr/bin/env python3 + +""" +json_save + +metaclass based system for saving objects in a JSON format + +This could be useful, but it's kept simple to show the use of metaclasses + +The idea is that you subclass from JsonSavable, and then you get an object +that be saved and reloaded to/from JSON +""" + +import json + +# import * is a bad idea in general, but helpful for a modules that's part +# of a package, where you control the names. +from .saveables import * + + +class MetaJsonSaveable(type): + """ + The metaclass for creating JsonSavable classes + + Deriving from type makes it a metaclass. + + Note: the __init__ gets run at compile time, not run time. + (module import time) + """ + def __init__(cls, name, bases, attr_dict): + # it gets the class object as the first param. + # and then the same parameters as the type() factory function + + # you want to call the regular type initilizer: + super().__init__(name, bases, attr_dict) + + # here's where we work with the class attributes: + # these will the attributes that get saved and reconstructed from json. + # each class object gets its own dict + cls._attrs_to_save = {} + for key, attr in attr_dict.items(): + if isinstance(attr, Saveable): + cls._attrs_to_save[key] = attr + # special case JsonSaveable -- no attrs to save yet + if cls.__name__ != "JsonSaveable" and (not cls._attrs_to_save): + raise TypeError(f"{cls.__name__} class has no saveable attributes.\n" + " Note that Savable attributes must be instances") + + # register this class so we can re-construct instances. + Saveable.ALL_SAVEABLES[attr_dict["__qualname__"]] = cls + + +class JsonSaveable(metaclass=MetaJsonSaveable): + """ + mixin for JsonSavable objects + """ + def __new__(cls, *args, **kwargs): + """ + This adds instance attributes to assure they are all there, even if + they are not set in the subclasses __init__ + """ + # create the instance + obj = super().__new__(cls) + # set the instance attributes to defaults + for attr, typ in cls._attrs_to_save.items(): + setattr(obj, attr, typ.default) + return obj + + def __eq__(self, other): + """ + default equality method that checks if all of the saved attributes + are equal + """ + for attr in self._attrs_to_save: + try: + if getattr(self, attr) != getattr(other, attr): + return False + except AttributeError: + return False + return True + + def to_json_compat(self): + """ + converts this object to a json-compatible dict. + + returns the dict + """ + # add and __obj_type attribute, so it can be reconstructed + dic = {"__obj_type": self.__class__.__qualname__} + for attr, typ in self._attrs_to_save.items(): + dic[attr] = typ.to_json_compat(getattr(self, attr)) + return dic + + @classmethod + def from_json_dict(cls, dic): + """ + creates an instance of this class populated by the contents of + the json compatible dict + + the object is created with __new__ before setting the attributes + + NOTE: __init__ is not called. + There should not be any extra initialization required in __init__ + """ + # create a new object + obj = cls.__new__(cls) + for attr, typ in cls._attrs_to_save.items(): + setattr(obj, attr, typ.to_python(dic[attr])) + # make sure it gets initialized + # obj.__init__() + return obj + + def to_json(self, fp=None, indent=4): + """ + Converts the object to JSON + + :param fp=None: an open file_like object to write the json to. + If it is None, then a string with the JSON + will be returned as a string + + :param indent=4: The indentation level desired in the JSON + """ + if fp is None: + return json.dumps(self.to_json_compat(), indent=indent) + else: + json.dump(self.to_json_compat(), fp, indent=indent) + + def __str__(self): + msg = ["{} object, with attributes:".format(self.__class__.__qualname__)] + for attr in self._attrs_to_save.keys(): + msg.append("{}: {}".format(attr, getattr(self, attr))) + return "\n".join(msg) + + +def from_json_dict(j_dict): + """ + factory function that creates an arbitrary JsonSavable + object from a json-compatible dict. + """ + # determine the class it is. + obj_type = j_dict["__obj_type"] + obj = Saveable.ALL_SAVEABLES[obj_type].from_json_dict(j_dict) + return obj + + +def from_json(_json): + """ + factory function that re-creates a JsonSavable object + from a json string or file + """ + if isinstance(_json, str): + return from_json_dict(json.loads(_json)) + else: # assume a file-like object + return from_json_dict(json.load(_json)) + + +if __name__ == "__main__": + + # Example of using it. + class MyClass(JsonSaveable): + + x = Int() + y = Float() + l = List() + + def __init__(self, x, lst): + self.x = x + self.lst = lst + + + class OtherSaveable(JsonSavable): + + foo = String() + bar = Int() + + def __init__(self, foo, bar): + self.foo = foo + self.bar = bar + + # create one: + print("about to create a instance") + mc = MyClass(5, [3, 5, 7, 9]) + + print(mc) + + jc = mc.to_json_compat() + + # re-create it from the dict: + mc2 = MyClass.from_json_dict(jc) + + print(mc2 == "fred") + + assert mc2 == mc + + print(mc.to_json()) + + # now try it nested... + mc_nest = MyClass(34, [OtherSaveable("this", 2), + OtherSaveable("that", 64), + ]) + + mc_nest_comp = mc_nest.to_json_compat() + print(mc_nest_comp) + + # can we re-create it? + mc_nest2 = MyClass.from_json_dict(mc_nest_comp) + + print(mc_nest) + print(mc_nest2) + + assert mc_nest == mc_nest2 diff --git a/Student/Ruohan/lesson04/json/examples/json_save/saveables.py b/Student/Ruohan/lesson04/json/examples/json_save/saveables.py new file mode 100644 index 0000000..16ac75f --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/json_save/saveables.py @@ -0,0 +1,218 @@ +#!/usr/bin/env python + +""" +The Saveable objects used by both the metaclass and decorator approach. +""" +import ast + +# import json + +__all__ = ['Bool', + 'Dict', + 'Float', + 'Int', + 'List', + 'Saveable', + 'String', + 'Tuple', + ] + + +class Saveable(): + """ + Base class for all saveable types + """ + default = None + ALL_SAVEABLES = {} + + @staticmethod + def to_json_compat(val): + """ + returns a json-compatible version of val + + should be overridden in saveable types that are not json compatible. + """ + return val + + @staticmethod + def to_python(val): + """ + convert from a json compatible version to the python version + + Must be overridden if not a one-to-one match + + This is where validation could be added as well. + """ + return val + + +class String(Saveable): + """ + A Saveable string + + Strings are the same in JSON as Python, so nothing to do here + """ + default = "" + + +class Bool(Saveable): + """ + A Saveable boolean + + Booleans are pretty much the same in JSON as Python, so nothing to do here + """ + default = False + + +class Int(Saveable): + + """ + A Saveable integer + + Integers are a little different in JSON than Python. Strictly speaking + JSON only has "numbers", which can be integer or float, so a little to + do here to make sure we get an int in Python. + """ + + default = 0 + + @staticmethod + def to_python(val): + """ + Convert a number to a python integer + """ + return int(val) + + +class Float(Saveable): + """ + A Saveable floating point number + + floats are a little different in JSON than Python. Strictly speaking + JSON only has "numbers", which can be integer or float, so a little to + do here to make sure we get a float in Python. + """ + + default = 0.0 + + @staticmethod + def to_python(val): + """ + Convert a number to a python float + """ + return float(val) + +# Container types: these need to hold Saveable objects. + + +class Tuple(Saveable): + """ + This assumes that whatever is in the tuple is Saveable or a "usual" + type: numbers, strings. + """ + default = () + + @staticmethod + def to_python(val): + """ + Convert a list to a tuple -- json only has one array type, + which matches to a list. + """ + # simply uses the List to_python method -- that part is the same. + return tuple(List.to_python(val)) + + +class List(Saveable): + """ + This assumes that whatever is in the list is Saveable or a "usual" + type: numbers, strings. + """ + default = [] + + @staticmethod + def to_json_compat(val): + lst = [] + for item in val: + try: + lst.append(item.to_json_compat()) + except AttributeError: + lst.append(item) + return lst + + @staticmethod + def to_python(val): + """ + Convert an array to a list. + + Complicated because list may contain non-json-compatible objects + """ + # try to reconstitute using the obj method + new_list = [] + for item in val: + try: + obj_type = item["__obj_type"] + obj = Saveable.ALL_SAVEABLES[obj_type].from_json_dict(item) + new_list.append(obj) + except (TypeError, KeyError): + new_list.append(item) + return new_list + + +class Dict(Saveable): + """ + This assumes that whatever in the dict is Saveable as well. + + This supports non-string keys, but all keys must be the same type. + """ + default = {} + + @staticmethod + def to_json_compat(val): + d = {} + # first key, arbitrarily + key_type = type(next(iter(val.keys()))) + if key_type is not str: + # need to add key_type to json + d['__key_not_string'] = True + key_not_string = True + else: + key_not_string = False + for key, item in val.items(): + kis = type(key) is str + if ((kis and key_not_string) or (not (kis or key_not_string))): + raise TypeError("dict keys must be all strings or no strings") + if key_type is not str: + # convert key to string + s_key = repr(key) + # make sure it can be reconstituted + if ast.literal_eval(s_key) != key: + raise ValueError(f"json save cannot save dicts with key:{key}") + else: + s_key = key + try: + d[s_key] = item.to_json_compat() + except AttributeError: + d[s_key] = item + return d + + @staticmethod + def to_python(val): + """ + Convert a json object to a dict + + Complicated because object may contain non-json-compatible objects + """ + + # try to reconstitute using the obj method + new_dict = {} + key_not_string = val.pop('__key_not_string', False) + for key, item in val.items(): + if key_not_string: + key = ast.literal_eval(key) + try: + obj_type = item["__obj_type"] + obj = Saveable.ALL_SAVEABLES[obj_type].from_json_dict(item) + new_dict[key] = obj + except (KeyError, TypeError): + new_dict[key] = item + return new_dict diff --git a/Student/Ruohan/lesson04/json/examples/json_save/test/__init__.py b/Student/Ruohan/lesson04/json/examples/json_save/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Student/Ruohan/lesson04/json/examples/json_save/test/temp.json b/Student/Ruohan/lesson04/json/examples/json_save/test/temp.json new file mode 100644 index 0000000..760c652 --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/json_save/test/temp.json @@ -0,0 +1,21 @@ +{ + "__obj_type": "ClassWithList", + "x": 34, + "lst": [ + { + "__obj_type": "SimpleClass", + "a": 3, + "b": 4.5 + }, + { + "__obj_type": "SimpleClass", + "a": 100, + "b": 5.2 + }, + { + "__obj_type": "SimpleClass", + "a": 34, + "b": 89.1 + } + ] +} \ No newline at end of file diff --git a/Student/Ruohan/lesson04/json/examples/json_save/test/test_json_save_dec.py b/Student/Ruohan/lesson04/json/examples/json_save/test/test_json_save_dec.py new file mode 100644 index 0000000..f587517 --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/json_save/test/test_json_save_dec.py @@ -0,0 +1,256 @@ +#!/usr/bin/env python + +""" +test code for the decorator version of json_save +""" + +import pytest + +import json_save.json_save_dec as js + + +# Some simple classes to test: + +@js.json_save +class NoInit: + """ + A class with saveable attribute, but no __init__ + """ + x = js.Int() + y = js.String() + + +@js.json_save +class SimpleClass: + + a = js.Int() + b = js.Float() + + def __init__(self, a=None, b=None): + if a is not None: + self.a = a + if b is not None: + self.b = b + + +@js.json_save +class ClassWithList: + + x = js.Int() + lst = js.List() + + def __init__(self, x, lst): + self.x = x + self.lst = lst + + +@js.json_save +class ClassWithDict: + x = js.Int() + d = js.Dict() + + def __init__(self, x, d): + self.x = x + self.d = d + + +@pytest.fixture +def nested_example(): + l = [SimpleClass(3, 4.5), + SimpleClass(100, 5.2), + SimpleClass(34, 89.1), + ] + + return ClassWithList(34, l) + +@pytest.fixture +def nested_dict(): + d = {'this': SimpleClass(3, 4.5), + 'that': SimpleClass(100, 5.2), + 'other': SimpleClass(34, 89.1), + } + + return ClassWithDict(34, d) + + +# now the actual test code + +def test_hasattr(): + """ + checks that the default attributes get set if they are not created by an __init__ + """ + ts = NoInit() + # has the instance attributes even though no __init__ exists + # they should be the default values + assert ts.x == 0 + assert ts.y == "" + + +def test_attrs(): + ts = SimpleClass() + + attrs = ts._attrs_to_save + assert list(attrs.keys()) == ['a', 'b'] + + +def test_simple_save(): + + ts = SimpleClass() + ts.a = 5 + ts.b = 3.14 + + saved = ts.to_json_compat() + assert saved['a'] == 5 + assert saved['b'] == 3.14 + assert saved['__obj_type'] == 'SimpleClass' + + +def test_list_attr(): + + cwl = ClassWithList(10, [1, 5, 2, 8]) + + saved = cwl.to_json_compat() + assert saved['x'] == 10 + assert saved['lst'] == [1, 5, 2, 8] + assert saved['__obj_type'] == 'ClassWithList' + + +def test_nested(nested_example): + + saved = nested_example.to_json_compat() + + assert saved['x'] == 34 + assert len(saved['lst']) == 3 + for obj in saved['lst']: + assert obj['__obj_type'] == 'SimpleClass' + + +def test_save_load_simple(): + sc = SimpleClass(5, 3.14) + + jc = sc.to_json_compat() + + # re-create it from the dict: + sc2 = SimpleClass.from_json_dict(jc) + + assert sc == sc2 + + +def test_save_load_nested(nested_example): + + jc = nested_example.to_json_compat() + + # re-create it from the dict: + nested_example2 = ClassWithList.from_json_dict(jc) + + assert nested_example == nested_example2 + + +def test_from_json_dict(nested_example): + + j_dict = nested_example.to_json_compat() + + reconstructed = js.from_json_dict(j_dict) + + assert reconstructed == nested_example + + +def test_from_json(nested_example): + """ + can it be re-created from an actual json string? + """ + + json_str = nested_example.to_json() + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_example + + +def test_from_json_file(nested_example): + """ + can it be re-created from an actual json file? + """ + + json_str = nested_example.to_json() + with open("temp.json", 'w') as tempfile: + tempfile.write(nested_example.to_json()) + + with open("temp.json") as tempfile: + reconstructed = js.from_json(tempfile) + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_example + + +def test_dict(): + """ + a simple class with a dict attribute + """ + cwd = ClassWithDict(45, {"this": 34, "that": 12}) + + # see if it can be reconstructed + + jc = cwd.to_json_compat() + + # re-create it from the dict: + cwd2 = ClassWithDict.from_json_dict(jc) + + assert cwd == cwd2 + + +def test_from_json_dict2(nested_dict): + """ + can it be re-created from an actual json string? + """ + + json_str = nested_dict.to_json() + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_dict + + +def test_eq(): + sc1 = SimpleClass(3, 4.5) + sc2 = SimpleClass(3, 4.5) + + assert sc1 == sc2 + + +def test_not_eq(): + sc1 = SimpleClass(3, 4.5) + sc2 = SimpleClass(3, 4.4) + + assert sc1 != sc2 + + +def test_not_eq_reconstruct(): + sc1 = SimpleClass.from_json_dict(SimpleClass(3, 4.5).to_json_compat()) + sc2 = SimpleClass.from_json_dict(SimpleClass(2, 4.5).to_json_compat()) + + assert sc1 != sc2 + assert sc2 != sc1 + + +def test_not_valid(): + """ + You should get an error trying to make a savable class with + no savable attributes. + """ + with pytest.raises(TypeError): + @js.json_save + class NotValid(): + pass + + +def test_not_valid_class_not_instance(): + """ + You should get an error trying to make a savable class with + no savable attributes. + """ + with pytest.raises(TypeError): + @js.json_save + class NotValid(): + a = js.Int + b = js.Float diff --git a/Student/Ruohan/lesson04/json/examples/json_save/test/test_json_save_meta.py b/Student/Ruohan/lesson04/json/examples/json_save/test/test_json_save_meta.py new file mode 100644 index 0000000..2f42c22 --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/json_save/test/test_json_save_meta.py @@ -0,0 +1,235 @@ +#!/usr/bin/env python3 + +""" +tests for json_save +""" + +import json_save.json_save_meta as js + +import pytest + + +class NoInit(js.JsonSaveable): + x = js.Int() + y = js.String() + + +# A few simple examples to test +class SimpleClass(js.JsonSaveable): + + a = js.Int() + b = js.Float() + + def __init__(self, a=None, b=None): + if a is not None: + self.a = a + if b is not None: + self.b = b + + +class ClassWithList(js.JsonSaveable): + + x = js.Int() + lst = js.List() + + def __init__(self, x, lst): + self.x = x + self.lst = lst + + +class ClassWithDict(js.JsonSaveable): + x = js.Int() + d = js.Dict() + + def __init__(self, x, d): + self.x = x + self.d = d + + +@pytest.fixture +def nested_example(): + lst = [SimpleClass(3, 4.5), + SimpleClass(100, 5.2), + SimpleClass(34, 89.1), + ] + + return ClassWithList(34, lst) + + +@pytest.fixture +def nested_dict(): + d = {'this': SimpleClass(3, 4.5), + 'that': SimpleClass(100, 5.2), + 'other': SimpleClass(34, 89.1), + } + + return ClassWithDict(34, d) + + +def test_hasattr(): + ts = NoInit() + # has the attributes even though no __init__ exists + # they should be the default values + assert ts.x == 0 + assert ts.y == "" + + +def test_simple_save(): + + ts = SimpleClass() + ts.a = 5 + ts.b = 3.14 + + saved = ts.to_json_compat() + assert saved['a'] == 5 + assert saved['b'] == 3.14 + assert saved['__obj_type'] == 'SimpleClass' + + +def test_list_attr(): + + cwl = ClassWithList(10, [1, 5, 2, 8]) + + saved = cwl.to_json_compat() + assert saved['x'] == 10 + assert saved['lst'] == [1, 5, 2, 8] + assert saved['__obj_type'] == 'ClassWithList' + + +def test_nested(nested_example): + + saved = nested_example.to_json_compat() + + assert saved['x'] == 34 + assert len(saved['lst']) == 3 + for obj in saved['lst']: + assert obj['__obj_type'] == 'SimpleClass' + + +def test_save_load_simple(): + sc = SimpleClass(5, 3.14) + + jc = sc.to_json_compat() + + # re-create it from the dict: + sc2 = SimpleClass.from_json_dict(jc) + + assert sc == sc2 + + +def test_save_load_nested(nested_example): + + jc = nested_example.to_json_compat() + + # re-create it from the dict: + nested_example2 = ClassWithList.from_json_dict(jc) + + assert nested_example == nested_example2 + + +def test_from_json_dict(nested_example): + + j_dict = nested_example.to_json_compat() + + reconstructed = js.from_json_dict(j_dict) + + assert reconstructed == nested_example + + +def test_from_json(nested_example): + """ + can it be re-created from an actual json string? + """ + + json_str = nested_example.to_json() + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_example + + +def test_from_json_file(nested_example): + """ + can it be re-created from an actual json file? + """ + + json_str = nested_example.to_json() + with open("temp.json", 'w') as tempfile: + tempfile.write(nested_example.to_json()) + + with open("temp.json") as tempfile: + reconstructed = js.from_json(tempfile) + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_example + + +def test_dict(): + """ + a simple class with a dict attribute + """ + cwd = ClassWithDict(45, {"this": 34, "that": 12}) + + # see if it can be reconstructed + + jc = cwd.to_json_compat() + + # re-create it from the dict: + cwd2 = ClassWithDict.from_json_dict(jc) + + assert cwd == cwd2 + + +def test_from_json_dict2(nested_dict): + """ + can it be re-created from an actual json string? + """ + + json_str = nested_dict.to_json() + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_dict + +def test_eq(): + sc1 = SimpleClass(3, 4.5) + sc2 = SimpleClass(3, 4.5) + + assert sc1 == sc2 + + +def test_not_eq(): + sc1 = SimpleClass(3, 4.5) + sc2 = SimpleClass(3, 4.4) + + assert sc1 != sc2 + + +def test_not_eq_reconstruct(): + sc1 = SimpleClass.from_json_dict(SimpleClass(3, 4.5).to_json_compat()) + sc2 = SimpleClass.from_json_dict(SimpleClass(2, 4.5).to_json_compat()) + + assert sc1 != sc2 + assert sc2 != sc1 + + +def test_not_valid(): + """ + You should get an error trying to make a savable class with + no savable attributes. + """ + with pytest.raises(TypeError): + class NotValid(js.JsonSaveable): + pass + + +def test_not_valid_class_not_instance(): + """ + You should get an error trying to make a savable class with + no savable attributes. + """ + with pytest.raises(TypeError): + class NotValid(js.JsonSaveable): + a = js.Int + b = js.Float diff --git a/Student/Ruohan/lesson04/json/examples/json_save/test/test_savables.py b/Student/Ruohan/lesson04/json/examples/json_save/test/test_savables.py new file mode 100644 index 0000000..831374c --- /dev/null +++ b/Student/Ruohan/lesson04/json/examples/json_save/test/test_savables.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +""" +tests for the savable objects +""" +import pytest + +import json + +from json_save.saveables import * + +# The simple, almost json <-> python ones: +# Type, default, example +basics = [(String, "This is a string"), + (Int, 23), + (Float, 3.1458), + (Bool, True), + (Bool, False), + (List, [2, 3, 4]), + (Tuple, (1, 2, 3.4, "this")), + (List, [[1, 2, 3], [4, 5, 6]]), + (List, [{"3": 34}, {"4": 5}]), # list with dicts in it. + (Dict, {"this": {"3": 34}, "that": {"4": 5}}) # dict with dicts + ] + + +@pytest.mark.parametrize(('Type', 'val'), basics) +def test_basics(Type, val): + js = json.dumps(Type.to_json_compat(val)) + val2 = Type.to_python(json.loads(js)) + assert val == val2 + assert type(val) == type(val2) + + +nested = [(List, [(1, 2), (3, 4), (5, 6)]), # tuple in list + (Tuple, ((1, 2), (3, 4), (5, 6))), # tuple in tuple + ] + + +# This maybe should be fixed in the future?? +@pytest.mark.xfail(reason="nested not-standard types not supported") +@pytest.mark.parametrize(('Type', 'val'), nested) +def test_nested(Type, val): + print("original value:", val) + js = json.dumps(Type.to_json_compat(val)) + print("js is:", js) + val2 = Type.to_python(json.loads(js)) + print("new value is:", val2) + assert val == val2 + assert type(val) == type(val2) + + + + +dicts = [{"this": 14, "that": 1.23}, + {34: 15, 23: 5}, + {3.4: "float_key", 1.2: "float_key"}, + {(1, 2, 3): "tuple_key"}, + {(3, 4, 5): "tuple_int", ("this", "that"): "tuple_str"}, + {4: "int_key", 1.23: "float_key", (1, 2, 3): "tuple_key"}, + ] + + +@pytest.mark.parametrize('val', dicts) +def test_dicts(val): + js = json.dumps(Dict.to_json_compat(val)) + val2 = Dict.to_python(json.loads(js)) + assert val == val2 + assert type(val) == type(val2) + # check that the types of the keys is the same + for k1, k2 in zip(val.keys(), val2.keys()): + assert type(k1) is type(k2) + + +# These are dicts that can't be saved +# -- mixing string and non-string keys +bad_dicts = [{"this": "string_key", 4: "int_key"}, + {3: "int_key", "this": "string_key"}, + {None: "none_key", "this": "string_key"}, + {"this": "string_key", None: "none_key"}, + ] + + +@pytest.mark.parametrize("val", bad_dicts) +def test_bad_dicts(val): + with pytest.raises(TypeError): + Dict.to_json_compat(val) diff --git a/Student/Ruohan/lesson04/json/json_save/__init__.py b/Student/Ruohan/lesson04/json/json_save/__init__.py new file mode 100644 index 0000000..db59bd9 --- /dev/null +++ b/Student/Ruohan/lesson04/json/json_save/__init__.py @@ -0,0 +1,8 @@ +""" +json_save package + +Pulling in names from the other packages. +""" + +__version__ = "0.4.0" + diff --git a/Student/Ruohan/lesson04/json/json_save/json_save_dec.py b/Student/Ruohan/lesson04/json/json_save/json_save_dec.py new file mode 100644 index 0000000..5af4de9 --- /dev/null +++ b/Student/Ruohan/lesson04/json/json_save/json_save_dec.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python + +""" +json_save implemented as a decorator +""" + +import json +from pathlib import Path + +from .json_save_meta import * + + +# assorted methods that will need to be added to the decorated class: +def _to_json_compat(self): + """ + converts this object to a json-compatible dict. + + returns the dict + """ + # add and __obj_type attribute, so it can be reconstructed + dic = {"__obj_type": self.__class__.__qualname__} + for attr, typ in self._attrs_to_save.items(): + dic[attr] = typ.to_json_compat(getattr(self, attr)) + return dic + + +def __eq__(self, other): + """ + default equality method that checks if all of the saved attributes + are equal + """ + for attr in self._attrs_to_save: + try: + if getattr(self, attr) != getattr(other, attr): + return False + except AttributeError: + return False + return True + +@classmethod +def _from_json_dict(cls, dic): + """ + creates an instance of this class populated by the contents of + the json compatible dict + + the object is created with __new__ before setting the attributes + + NOTE: __init__ is not called. + There should not be any extra initialization required in __init__ + """ + # create a new object + obj = cls.__new__(cls) + for attr, typ in cls._attrs_to_save.items(): + setattr(obj, attr, typ.to_python(dic[attr])) + return obj + + +def __new__(cls, *args, **kwargs): + """ + This adds instance attributes to assure they are all there, even if + they are not set in the subclasses __init__ + + it's in __new__ so that it will get called before the decorated class' + __init__ -- the __init__ will override anything here. + """ + # create the instance by calling the base class __new__ + obj = cls.__base__.__new__(cls) + # using super() did not work here -- why?? + # set the instance attributes to defaults + for attr, typ in cls._attrs_to_save.items(): + setattr(obj, attr, typ.default) + return obj + + +def _to_json(self, fp=None, indent=4): + """ + Converts the object to JSON + + :param fp=None: an open file_like object to write the json to. + If it is None, then a string with the JSON + will be returned as a string + + :param indent=4: The indentation level desired in the JSON + """ + if fp is None: + return json.dumps(self.to_json_compat(), indent=indent) + else: + json.dump(self.to_json_compat(), fp, indent=indent) + + +# now the actual decorator +def json_save(cls): + """ + json_save decorator + + makes decorated classes Saveable to json + """ + # make sure this is decorating a class object + if type(cls) is not type: + raise TypeError("json_save can only be used on classes") + + # find the saveable attributes + # these will the attributes that get saved and reconstructed from json. + # each class object gets its own dict + attr_dict = vars(cls) + cls._attrs_to_save = {} + for key, attr in attr_dict.items(): + if isinstance(attr, Saveable): + cls._attrs_to_save[key] = attr + if not cls._attrs_to_save: + raise TypeError(f"{cls.__name__} class has no saveable attributes.\n" + " Note that Savable attributes must be instances") + # register this class so we can re-construct instances. + Saveable.ALL_SAVEABLES[cls.__qualname__] = cls + + # add the methods: + cls.__new__ = __new__ + cls.to_json_compat = _to_json_compat + cls.__eq__ = __eq__ + cls.from_json_dict = _from_json_dict + cls.to_json = _to_json + + return cls + + +# utilities for loading arbitrary objects from json +def from_json_dict(j_dict): + """ + factory function that creates an arbitrary JsonSaveable + object from a json-compatible dict. + """ + # determine the class it is. + obj_type = j_dict["__obj_type"] + obj = Saveable.ALL_SAVEABLES[obj_type].from_json_dict(j_dict) + return obj + + +def from_json(_json): + """ + Factory function that re-creates a JsonSaveable object + from a json string or file + """ + if isinstance(_json, (str, Path)): + return from_json_dict(json.loads(_json)) + else: # assume a file-like object + return from_json_dict(json.load(_json)) diff --git a/Student/Ruohan/lesson04/json/json_save/json_save_meta.py b/Student/Ruohan/lesson04/json/json_save/json_save_meta.py new file mode 100644 index 0000000..5b8dfa8 --- /dev/null +++ b/Student/Ruohan/lesson04/json/json_save/json_save_meta.py @@ -0,0 +1,211 @@ +#!/usr/bin/env python3 + +""" +json_save + +metaclass based system for saving objects in a JSON format + +This could be useful, but it's kept simple to show the use of metaclasses + +The idea is that you subclass from JsonSavable, and then you get an object +that be saved and reloaded to/from JSON +""" + +import json + +# import * is a bad idea in general, but helpful for a modules that's part +# of a package, where you control the names. +from .saveables import * + + +class MetaJsonSaveable(type): + """ + The metaclass for creating JsonSavable classes + + Deriving from type makes it a metaclass. + + Note: the __init__ gets run at compile time, not run time. + (module import time) + """ + def __init__(cls, name, bases, attr_dict): + # it gets the class object as the first param. + # and then the same parameters as the type() factory function + + # you want to call the regular type initilizer: + super().__init__(name, bases, attr_dict) + + # here's where we work with the class attributes: + # these will the attributes that get saved and reconstructed from json. + # each class object gets its own dict + cls._attrs_to_save = {} + for key, attr in attr_dict.items(): + if isinstance(attr, Saveable): + cls._attrs_to_save[key] = attr + # special case JsonSaveable -- no attrs to save yet + if cls.__name__ != "JsonSaveable" and (not cls._attrs_to_save): + raise TypeError(f"{cls.__name__} class has no saveable attributes.\n" + " Note that Savable attributes must be instances") + + # register this class so we can re-construct instances. + Saveable.ALL_SAVEABLES[attr_dict["__qualname__"]] = cls + + +class JsonSaveable(metaclass=MetaJsonSaveable): + """ + mixin for JsonSavable objects + """ + def __new__(cls, *args, **kwargs): + """ + This adds instance attributes to assure they are all there, even if + they are not set in the subclasses __init__ + """ + # create the instance + obj = super().__new__(cls) + # set the instance attributes to defaults + for attr, typ in cls._attrs_to_save.items(): + setattr(obj, attr, typ.default) + return obj + + def __eq__(self, other): + """ + default equality method that checks if all of the saved attributes + are equal + """ + for attr in self._attrs_to_save: + try: + if getattr(self, attr) != getattr(other, attr): + return False + except AttributeError: + return False + return True + + def to_json_compat(self): + """ + converts this object to a json-compatible dict. + + returns the dict + """ + # add and __obj_type attribute, so it can be reconstructed + dic = {"__obj_type": self.__class__.__qualname__} + for attr, typ in self._attrs_to_save.items(): + dic[attr] = typ.to_json_compat(getattr(self, attr)) + return dic + + @classmethod + def from_json_dict(cls, dic): + """ + creates an instance of this class populated by the contents of + the json compatible dict + + the object is created with __new__ before setting the attributes + + NOTE: __init__ is not called. + There should not be any extra initialization required in __init__ + """ + # create a new object + obj = cls.__new__(cls) + for attr, typ in cls._attrs_to_save.items(): + setattr(obj, attr, typ.to_python(dic[attr])) + # make sure it gets initialized + # obj.__init__() + return obj + + def to_json(self, fp=None, indent=4): + """ + Converts the object to JSON + + :param fp=None: an open file_like object to write the json to. + If it is None, then a string with the JSON + will be returned as a string + + :param indent=4: The indentation level desired in the JSON + """ + if fp is None: + return json.dumps(self.to_json_compat(), indent=indent) + else: + json.dump(self.to_json_compat(), fp, indent=indent) + + def __str__(self): + msg = ["{} object, with attributes:".format(self.__class__.__qualname__)] + for attr in self._attrs_to_save.keys(): + msg.append("{}: {}".format(attr, getattr(self, attr))) + return "\n".join(msg) + + +def from_json_dict(j_dict): + """ + factory function that creates an arbitrary JsonSavable + object from a json-compatible dict. + """ + # determine the class it is. + obj_type = j_dict["__obj_type"] + obj = Saveable.ALL_SAVEABLES[obj_type].from_json_dict(j_dict) + return obj + + +def from_json(_json): + """ + factory function that re-creates a JsonSavable object + from a json string or file + """ + if isinstance(_json, str): + return from_json_dict(json.loads(_json)) + else: # assume a file-like object + return from_json_dict(json.load(_json)) + + +if __name__ == "__main__": + + # Example of using it. + class MyClass(JsonSaveable): + + x = Int() + y = Float() + l = List() + + def __init__(self, x, lst): + self.x = x + self.lst = lst + + + class OtherSaveable(JsonSavable): + + foo = String() + bar = Int() + + def __init__(self, foo, bar): + self.foo = foo + self.bar = bar + + # create one: + print("about to create a instance") + mc = MyClass(5, [3, 5, 7, 9]) + + print(mc) + + jc = mc.to_json_compat() + + # re-create it from the dict: + mc2 = MyClass.from_json_dict(jc) + + print(mc2 == "fred") + + assert mc2 == mc + + print(mc.to_json()) + + # now try it nested... + mc_nest = MyClass(34, [OtherSaveable("this", 2), + OtherSaveable("that", 64), + ]) + + mc_nest_comp = mc_nest.to_json_compat() + print(mc_nest_comp) + + # can we re-create it? + mc_nest2 = MyClass.from_json_dict(mc_nest_comp) + + print(mc_nest) + print(mc_nest2) + + assert mc_nest == mc_nest2 diff --git a/Student/Ruohan/lesson04/json/json_save/saveables.py b/Student/Ruohan/lesson04/json/json_save/saveables.py new file mode 100644 index 0000000..16ac75f --- /dev/null +++ b/Student/Ruohan/lesson04/json/json_save/saveables.py @@ -0,0 +1,218 @@ +#!/usr/bin/env python + +""" +The Saveable objects used by both the metaclass and decorator approach. +""" +import ast + +# import json + +__all__ = ['Bool', + 'Dict', + 'Float', + 'Int', + 'List', + 'Saveable', + 'String', + 'Tuple', + ] + + +class Saveable(): + """ + Base class for all saveable types + """ + default = None + ALL_SAVEABLES = {} + + @staticmethod + def to_json_compat(val): + """ + returns a json-compatible version of val + + should be overridden in saveable types that are not json compatible. + """ + return val + + @staticmethod + def to_python(val): + """ + convert from a json compatible version to the python version + + Must be overridden if not a one-to-one match + + This is where validation could be added as well. + """ + return val + + +class String(Saveable): + """ + A Saveable string + + Strings are the same in JSON as Python, so nothing to do here + """ + default = "" + + +class Bool(Saveable): + """ + A Saveable boolean + + Booleans are pretty much the same in JSON as Python, so nothing to do here + """ + default = False + + +class Int(Saveable): + + """ + A Saveable integer + + Integers are a little different in JSON than Python. Strictly speaking + JSON only has "numbers", which can be integer or float, so a little to + do here to make sure we get an int in Python. + """ + + default = 0 + + @staticmethod + def to_python(val): + """ + Convert a number to a python integer + """ + return int(val) + + +class Float(Saveable): + """ + A Saveable floating point number + + floats are a little different in JSON than Python. Strictly speaking + JSON only has "numbers", which can be integer or float, so a little to + do here to make sure we get a float in Python. + """ + + default = 0.0 + + @staticmethod + def to_python(val): + """ + Convert a number to a python float + """ + return float(val) + +# Container types: these need to hold Saveable objects. + + +class Tuple(Saveable): + """ + This assumes that whatever is in the tuple is Saveable or a "usual" + type: numbers, strings. + """ + default = () + + @staticmethod + def to_python(val): + """ + Convert a list to a tuple -- json only has one array type, + which matches to a list. + """ + # simply uses the List to_python method -- that part is the same. + return tuple(List.to_python(val)) + + +class List(Saveable): + """ + This assumes that whatever is in the list is Saveable or a "usual" + type: numbers, strings. + """ + default = [] + + @staticmethod + def to_json_compat(val): + lst = [] + for item in val: + try: + lst.append(item.to_json_compat()) + except AttributeError: + lst.append(item) + return lst + + @staticmethod + def to_python(val): + """ + Convert an array to a list. + + Complicated because list may contain non-json-compatible objects + """ + # try to reconstitute using the obj method + new_list = [] + for item in val: + try: + obj_type = item["__obj_type"] + obj = Saveable.ALL_SAVEABLES[obj_type].from_json_dict(item) + new_list.append(obj) + except (TypeError, KeyError): + new_list.append(item) + return new_list + + +class Dict(Saveable): + """ + This assumes that whatever in the dict is Saveable as well. + + This supports non-string keys, but all keys must be the same type. + """ + default = {} + + @staticmethod + def to_json_compat(val): + d = {} + # first key, arbitrarily + key_type = type(next(iter(val.keys()))) + if key_type is not str: + # need to add key_type to json + d['__key_not_string'] = True + key_not_string = True + else: + key_not_string = False + for key, item in val.items(): + kis = type(key) is str + if ((kis and key_not_string) or (not (kis or key_not_string))): + raise TypeError("dict keys must be all strings or no strings") + if key_type is not str: + # convert key to string + s_key = repr(key) + # make sure it can be reconstituted + if ast.literal_eval(s_key) != key: + raise ValueError(f"json save cannot save dicts with key:{key}") + else: + s_key = key + try: + d[s_key] = item.to_json_compat() + except AttributeError: + d[s_key] = item + return d + + @staticmethod + def to_python(val): + """ + Convert a json object to a dict + + Complicated because object may contain non-json-compatible objects + """ + + # try to reconstitute using the obj method + new_dict = {} + key_not_string = val.pop('__key_not_string', False) + for key, item in val.items(): + if key_not_string: + key = ast.literal_eval(key) + try: + obj_type = item["__obj_type"] + obj = Saveable.ALL_SAVEABLES[obj_type].from_json_dict(item) + new_dict[key] = obj + except (KeyError, TypeError): + new_dict[key] = item + return new_dict diff --git a/Student/Ruohan/lesson04/json/json_save/test/__init__.py b/Student/Ruohan/lesson04/json/json_save/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Student/Ruohan/lesson04/json/json_save/test/temp.json b/Student/Ruohan/lesson04/json/json_save/test/temp.json new file mode 100644 index 0000000..760c652 --- /dev/null +++ b/Student/Ruohan/lesson04/json/json_save/test/temp.json @@ -0,0 +1,21 @@ +{ + "__obj_type": "ClassWithList", + "x": 34, + "lst": [ + { + "__obj_type": "SimpleClass", + "a": 3, + "b": 4.5 + }, + { + "__obj_type": "SimpleClass", + "a": 100, + "b": 5.2 + }, + { + "__obj_type": "SimpleClass", + "a": 34, + "b": 89.1 + } + ] +} \ No newline at end of file diff --git a/Student/Ruohan/lesson04/json/json_save/test/test_json_save_dec.py b/Student/Ruohan/lesson04/json/json_save/test/test_json_save_dec.py new file mode 100644 index 0000000..f587517 --- /dev/null +++ b/Student/Ruohan/lesson04/json/json_save/test/test_json_save_dec.py @@ -0,0 +1,256 @@ +#!/usr/bin/env python + +""" +test code for the decorator version of json_save +""" + +import pytest + +import json_save.json_save_dec as js + + +# Some simple classes to test: + +@js.json_save +class NoInit: + """ + A class with saveable attribute, but no __init__ + """ + x = js.Int() + y = js.String() + + +@js.json_save +class SimpleClass: + + a = js.Int() + b = js.Float() + + def __init__(self, a=None, b=None): + if a is not None: + self.a = a + if b is not None: + self.b = b + + +@js.json_save +class ClassWithList: + + x = js.Int() + lst = js.List() + + def __init__(self, x, lst): + self.x = x + self.lst = lst + + +@js.json_save +class ClassWithDict: + x = js.Int() + d = js.Dict() + + def __init__(self, x, d): + self.x = x + self.d = d + + +@pytest.fixture +def nested_example(): + l = [SimpleClass(3, 4.5), + SimpleClass(100, 5.2), + SimpleClass(34, 89.1), + ] + + return ClassWithList(34, l) + +@pytest.fixture +def nested_dict(): + d = {'this': SimpleClass(3, 4.5), + 'that': SimpleClass(100, 5.2), + 'other': SimpleClass(34, 89.1), + } + + return ClassWithDict(34, d) + + +# now the actual test code + +def test_hasattr(): + """ + checks that the default attributes get set if they are not created by an __init__ + """ + ts = NoInit() + # has the instance attributes even though no __init__ exists + # they should be the default values + assert ts.x == 0 + assert ts.y == "" + + +def test_attrs(): + ts = SimpleClass() + + attrs = ts._attrs_to_save + assert list(attrs.keys()) == ['a', 'b'] + + +def test_simple_save(): + + ts = SimpleClass() + ts.a = 5 + ts.b = 3.14 + + saved = ts.to_json_compat() + assert saved['a'] == 5 + assert saved['b'] == 3.14 + assert saved['__obj_type'] == 'SimpleClass' + + +def test_list_attr(): + + cwl = ClassWithList(10, [1, 5, 2, 8]) + + saved = cwl.to_json_compat() + assert saved['x'] == 10 + assert saved['lst'] == [1, 5, 2, 8] + assert saved['__obj_type'] == 'ClassWithList' + + +def test_nested(nested_example): + + saved = nested_example.to_json_compat() + + assert saved['x'] == 34 + assert len(saved['lst']) == 3 + for obj in saved['lst']: + assert obj['__obj_type'] == 'SimpleClass' + + +def test_save_load_simple(): + sc = SimpleClass(5, 3.14) + + jc = sc.to_json_compat() + + # re-create it from the dict: + sc2 = SimpleClass.from_json_dict(jc) + + assert sc == sc2 + + +def test_save_load_nested(nested_example): + + jc = nested_example.to_json_compat() + + # re-create it from the dict: + nested_example2 = ClassWithList.from_json_dict(jc) + + assert nested_example == nested_example2 + + +def test_from_json_dict(nested_example): + + j_dict = nested_example.to_json_compat() + + reconstructed = js.from_json_dict(j_dict) + + assert reconstructed == nested_example + + +def test_from_json(nested_example): + """ + can it be re-created from an actual json string? + """ + + json_str = nested_example.to_json() + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_example + + +def test_from_json_file(nested_example): + """ + can it be re-created from an actual json file? + """ + + json_str = nested_example.to_json() + with open("temp.json", 'w') as tempfile: + tempfile.write(nested_example.to_json()) + + with open("temp.json") as tempfile: + reconstructed = js.from_json(tempfile) + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_example + + +def test_dict(): + """ + a simple class with a dict attribute + """ + cwd = ClassWithDict(45, {"this": 34, "that": 12}) + + # see if it can be reconstructed + + jc = cwd.to_json_compat() + + # re-create it from the dict: + cwd2 = ClassWithDict.from_json_dict(jc) + + assert cwd == cwd2 + + +def test_from_json_dict2(nested_dict): + """ + can it be re-created from an actual json string? + """ + + json_str = nested_dict.to_json() + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_dict + + +def test_eq(): + sc1 = SimpleClass(3, 4.5) + sc2 = SimpleClass(3, 4.5) + + assert sc1 == sc2 + + +def test_not_eq(): + sc1 = SimpleClass(3, 4.5) + sc2 = SimpleClass(3, 4.4) + + assert sc1 != sc2 + + +def test_not_eq_reconstruct(): + sc1 = SimpleClass.from_json_dict(SimpleClass(3, 4.5).to_json_compat()) + sc2 = SimpleClass.from_json_dict(SimpleClass(2, 4.5).to_json_compat()) + + assert sc1 != sc2 + assert sc2 != sc1 + + +def test_not_valid(): + """ + You should get an error trying to make a savable class with + no savable attributes. + """ + with pytest.raises(TypeError): + @js.json_save + class NotValid(): + pass + + +def test_not_valid_class_not_instance(): + """ + You should get an error trying to make a savable class with + no savable attributes. + """ + with pytest.raises(TypeError): + @js.json_save + class NotValid(): + a = js.Int + b = js.Float diff --git a/Student/Ruohan/lesson04/json/json_save/test/test_json_save_meta.py b/Student/Ruohan/lesson04/json/json_save/test/test_json_save_meta.py new file mode 100644 index 0000000..2f42c22 --- /dev/null +++ b/Student/Ruohan/lesson04/json/json_save/test/test_json_save_meta.py @@ -0,0 +1,235 @@ +#!/usr/bin/env python3 + +""" +tests for json_save +""" + +import json_save.json_save_meta as js + +import pytest + + +class NoInit(js.JsonSaveable): + x = js.Int() + y = js.String() + + +# A few simple examples to test +class SimpleClass(js.JsonSaveable): + + a = js.Int() + b = js.Float() + + def __init__(self, a=None, b=None): + if a is not None: + self.a = a + if b is not None: + self.b = b + + +class ClassWithList(js.JsonSaveable): + + x = js.Int() + lst = js.List() + + def __init__(self, x, lst): + self.x = x + self.lst = lst + + +class ClassWithDict(js.JsonSaveable): + x = js.Int() + d = js.Dict() + + def __init__(self, x, d): + self.x = x + self.d = d + + +@pytest.fixture +def nested_example(): + lst = [SimpleClass(3, 4.5), + SimpleClass(100, 5.2), + SimpleClass(34, 89.1), + ] + + return ClassWithList(34, lst) + + +@pytest.fixture +def nested_dict(): + d = {'this': SimpleClass(3, 4.5), + 'that': SimpleClass(100, 5.2), + 'other': SimpleClass(34, 89.1), + } + + return ClassWithDict(34, d) + + +def test_hasattr(): + ts = NoInit() + # has the attributes even though no __init__ exists + # they should be the default values + assert ts.x == 0 + assert ts.y == "" + + +def test_simple_save(): + + ts = SimpleClass() + ts.a = 5 + ts.b = 3.14 + + saved = ts.to_json_compat() + assert saved['a'] == 5 + assert saved['b'] == 3.14 + assert saved['__obj_type'] == 'SimpleClass' + + +def test_list_attr(): + + cwl = ClassWithList(10, [1, 5, 2, 8]) + + saved = cwl.to_json_compat() + assert saved['x'] == 10 + assert saved['lst'] == [1, 5, 2, 8] + assert saved['__obj_type'] == 'ClassWithList' + + +def test_nested(nested_example): + + saved = nested_example.to_json_compat() + + assert saved['x'] == 34 + assert len(saved['lst']) == 3 + for obj in saved['lst']: + assert obj['__obj_type'] == 'SimpleClass' + + +def test_save_load_simple(): + sc = SimpleClass(5, 3.14) + + jc = sc.to_json_compat() + + # re-create it from the dict: + sc2 = SimpleClass.from_json_dict(jc) + + assert sc == sc2 + + +def test_save_load_nested(nested_example): + + jc = nested_example.to_json_compat() + + # re-create it from the dict: + nested_example2 = ClassWithList.from_json_dict(jc) + + assert nested_example == nested_example2 + + +def test_from_json_dict(nested_example): + + j_dict = nested_example.to_json_compat() + + reconstructed = js.from_json_dict(j_dict) + + assert reconstructed == nested_example + + +def test_from_json(nested_example): + """ + can it be re-created from an actual json string? + """ + + json_str = nested_example.to_json() + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_example + + +def test_from_json_file(nested_example): + """ + can it be re-created from an actual json file? + """ + + json_str = nested_example.to_json() + with open("temp.json", 'w') as tempfile: + tempfile.write(nested_example.to_json()) + + with open("temp.json") as tempfile: + reconstructed = js.from_json(tempfile) + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_example + + +def test_dict(): + """ + a simple class with a dict attribute + """ + cwd = ClassWithDict(45, {"this": 34, "that": 12}) + + # see if it can be reconstructed + + jc = cwd.to_json_compat() + + # re-create it from the dict: + cwd2 = ClassWithDict.from_json_dict(jc) + + assert cwd == cwd2 + + +def test_from_json_dict2(nested_dict): + """ + can it be re-created from an actual json string? + """ + + json_str = nested_dict.to_json() + + reconstructed = js.from_json(json_str) + + assert reconstructed == nested_dict + +def test_eq(): + sc1 = SimpleClass(3, 4.5) + sc2 = SimpleClass(3, 4.5) + + assert sc1 == sc2 + + +def test_not_eq(): + sc1 = SimpleClass(3, 4.5) + sc2 = SimpleClass(3, 4.4) + + assert sc1 != sc2 + + +def test_not_eq_reconstruct(): + sc1 = SimpleClass.from_json_dict(SimpleClass(3, 4.5).to_json_compat()) + sc2 = SimpleClass.from_json_dict(SimpleClass(2, 4.5).to_json_compat()) + + assert sc1 != sc2 + assert sc2 != sc1 + + +def test_not_valid(): + """ + You should get an error trying to make a savable class with + no savable attributes. + """ + with pytest.raises(TypeError): + class NotValid(js.JsonSaveable): + pass + + +def test_not_valid_class_not_instance(): + """ + You should get an error trying to make a savable class with + no savable attributes. + """ + with pytest.raises(TypeError): + class NotValid(js.JsonSaveable): + a = js.Int + b = js.Float diff --git a/Student/Ruohan/lesson04/json/json_save/test/test_savables.py b/Student/Ruohan/lesson04/json/json_save/test/test_savables.py new file mode 100644 index 0000000..831374c --- /dev/null +++ b/Student/Ruohan/lesson04/json/json_save/test/test_savables.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +""" +tests for the savable objects +""" +import pytest + +import json + +from json_save.saveables import * + +# The simple, almost json <-> python ones: +# Type, default, example +basics = [(String, "This is a string"), + (Int, 23), + (Float, 3.1458), + (Bool, True), + (Bool, False), + (List, [2, 3, 4]), + (Tuple, (1, 2, 3.4, "this")), + (List, [[1, 2, 3], [4, 5, 6]]), + (List, [{"3": 34}, {"4": 5}]), # list with dicts in it. + (Dict, {"this": {"3": 34}, "that": {"4": 5}}) # dict with dicts + ] + + +@pytest.mark.parametrize(('Type', 'val'), basics) +def test_basics(Type, val): + js = json.dumps(Type.to_json_compat(val)) + val2 = Type.to_python(json.loads(js)) + assert val == val2 + assert type(val) == type(val2) + + +nested = [(List, [(1, 2), (3, 4), (5, 6)]), # tuple in list + (Tuple, ((1, 2), (3, 4), (5, 6))), # tuple in tuple + ] + + +# This maybe should be fixed in the future?? +@pytest.mark.xfail(reason="nested not-standard types not supported") +@pytest.mark.parametrize(('Type', 'val'), nested) +def test_nested(Type, val): + print("original value:", val) + js = json.dumps(Type.to_json_compat(val)) + print("js is:", js) + val2 = Type.to_python(json.loads(js)) + print("new value is:", val2) + assert val == val2 + assert type(val) == type(val2) + + + + +dicts = [{"this": 14, "that": 1.23}, + {34: 15, 23: 5}, + {3.4: "float_key", 1.2: "float_key"}, + {(1, 2, 3): "tuple_key"}, + {(3, 4, 5): "tuple_int", ("this", "that"): "tuple_str"}, + {4: "int_key", 1.23: "float_key", (1, 2, 3): "tuple_key"}, + ] + + +@pytest.mark.parametrize('val', dicts) +def test_dicts(val): + js = json.dumps(Dict.to_json_compat(val)) + val2 = Dict.to_python(json.loads(js)) + assert val == val2 + assert type(val) == type(val2) + # check that the types of the keys is the same + for k1, k2 in zip(val.keys(), val2.keys()): + assert type(k1) is type(k2) + + +# These are dicts that can't be saved +# -- mixing string and non-string keys +bad_dicts = [{"this": "string_key", 4: "int_key"}, + {3: "int_key", "this": "string_key"}, + {None: "none_key", "this": "string_key"}, + {"this": "string_key", None: "none_key"}, + ] + + +@pytest.mark.parametrize("val", bad_dicts) +def test_bad_dicts(val): + with pytest.raises(TypeError): + Dict.to_json_compat(val) diff --git a/Student/Ruohan/lesson04/json/setup.py b/Student/Ruohan/lesson04/json/setup.py new file mode 100755 index 0000000..d85dd95 --- /dev/null +++ b/Student/Ruohan/lesson04/json/setup.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +""" +This is about as simple a setup.py as you can have + +But its enough to support the json_save package + +""" + +import os + +from setuptools import setup, find_packages + + +def get_version(): + """ + Reads the version string from the package __init__ and returns it + """ + with open(os.path.join("json_save", "__init__.py")) as init_file: + for line in init_file: + parts = line.strip().partition("=") + if parts[0].strip() == "__version__": + return parts[2].strip().strip("'").strip('"') + return None + + +setup( + name='json_save', + version=get_version(), + author='Chris Barker', + author_email='PythonCHB@gmail.com', + packages=find_packages(), + # license='LICENSE.txt', + description='Metaclass based system for saving object to JSON', + long_description=open('README.txt').read(), +) diff --git a/Student/Ruohan/lesson04/mailroom_oo_decoration.py b/Student/Ruohan/lesson04/mailroom_oo_decoration.py new file mode 100644 index 0000000..e1e5385 --- /dev/null +++ b/Student/Ruohan/lesson04/mailroom_oo_decoration.py @@ -0,0 +1,148 @@ +#!/usr/bin/env python3 + +import json_save.json_save_dec as js +import sys +import string + +@js.jason_save +class Donor(): + name = js.String() + amount = js.Float() + lst = js.List() + + def __init__(self, name, lst = []): + self.name = name.capitalize() + self.donation = lst + + def add_donations(self, amount): + self.donation.append(amount) + + @property + def total(self): + return sum(self.donation) + + + @property + def times(self): + return len(self.donation) + + @property + def ave(self): + return self.total/self.times + + @property + def last(self): + if len(self.donation) > 0: + return self.donation[-1] + else: + return 0 + + def __str__(self): + return f'{self.name}:{self.donation}' + + +@js.jason_save +class donor_DB(): + dict = js.Dict() + + def __init__(self, dict= {}): + self._donors = dict + + def find_donor(self, name): + if name.lower() in self._donors: + return self._donors[name.lower()] + return None + + def add_donor(self, name): + if self.find_donor(name): + pass + else: + donor = Donor(name) + self._donors[donor.name.lower()] = donor + + def search_donor(self,name): + return self._donors.get(name.lower()) + + def donor_list(self): + return list(self._donors) + + def _letter(self, d): + letter = f'''Dear {d.name} + Thank you for your generous donation of ${d.last:.2f}. + We appreciate your contribution. + Team R ''' + return letter + + def report(self): + head = '{:20}|{:20}|{:10}|{:20}|'.format('Donor Name','Total Given', 'Num Gifts','Average Gifts') + result = [head] + result.append('-'*len(head)) + for donor in self._donors: + result.append(f"{donor.name:20}|${donor.total:19,.2f}|{donor.times:10}|${donor.ave:19,.2f}|") + return result + + def file_letters(self): + for donor in self._donors: + f_name = donor.name+'.txt' + with open(f_name, 'wt') as f: + print(f'''Dear {donor.name}, + Thank you for your generous donation ${donor.total:.2f}. + We appreciate your contribution. + Team R ''', file = f) + + def quit(self): + sys.exit() + + +def Welcome(): + print('Welcome to Mailroom') + db = donor_DB() + while True: + print('\nwhat do you want to do?') + print('1) Check the donor list\n' + '2) A new donation\n' + '3) Send thank you\n' + '4) Creat a report\n' + '5) Send letters to everyone\n' + '6) quit\n') + response = input('>> ') + if response == '1': + print(db.donor_list()) + print('Return to main menu\n') + elif response == '2': + name = input('Enter the donor name: ').lower() + db.add_donor(name) + amount = input('Enter the donation amount: ') + try: amount = float(amount) + except ValueError: + print('error: donation amount is invalid\n') + db.search_donor(name).add_donations(amount) + print('New donation has been recorded') + print('Return to main menu\n') + elif response == '3': + name = input('Who do you want to send thank you letter to: ').lower() + if db.find_donor(name): + print(db._letter(db.find_donor(name))) + else: + print('Donor is not recorded, please add donor to datasets first(chose 2 in main menu)\n') + print('Return to main menu\n') + continue + elif response == '4': + for line in db.report(): + print(line) + print('Return to main menu\n') + elif response == '5': + db.file_letters() + print('Letters have been saved') + print('Return to main menu\n') + elif response == '6': + print('Exit Now') + db.quit() + else: + print('Invalid input') + print('Return to main menu\n') + continue + + +if __name__ == "__main__": + Welcome() diff --git a/Student/Ruohan/lesson05/.ipynb_checkpoints/Activity_5-checkpoint.ipynb b/Student/Ruohan/lesson05/.ipynb_checkpoints/Activity_5-checkpoint.ipynb new file mode 100644 index 0000000..067f63c --- /dev/null +++ b/Student/Ruohan/lesson05/.ipynb_checkpoints/Activity_5-checkpoint.ipynb @@ -0,0 +1,39 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def my_fun(n):\n", + " for i in range(0, n):\n", + " i / (50 - i)\n", + "\n", + "if __name__ == \"__main__\":\n", + " my_fun(100)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson05/Activity_5.ipynb b/Student/Ruohan/lesson05/Activity_5.ipynb new file mode 100644 index 0000000..fe1fb86 --- /dev/null +++ b/Student/Ruohan/lesson05/Activity_5.ipynb @@ -0,0 +1,61 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "ename": "ZeroDivisionError", + "evalue": "division by zero", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m50\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mmy_fun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mmy_fun\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mlogging\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mi\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m50\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0mmy_fun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" + ] + } + ], + "source": [ + "import logging\n", + "\n", + "def my_fun(n):\n", + " for i in range(0, n):\n", + " logging.debug(i)\n", + " i / (50 - i)\n", + " \n", + "my_fun(100)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson05/Activity_5.py b/Student/Ruohan/lesson05/Activity_5.py new file mode 100644 index 0000000..b3c83cb --- /dev/null +++ b/Student/Ruohan/lesson05/Activity_5.py @@ -0,0 +1,31 @@ +import logging + +format = "%(asctime)s %(filename)s:%(lineno)-3d %(levelname)s %(message)s" + +formatter = logging.Formatter(format) + +file_handler = logging.FileHandler('mylog.log') +file_handler.setLevel(logging.WARNING) # Add this line +file_handler.setFormatter(formatter) + +console_handler = logging.StreamHandler() # Add this line +console_handler.setLevel(logging.DEBUG) # Add this line +console_handler.setFormatter(formatter) # Add this line + +logger = logging.getLogger() +logger.setLevel(logging.NOTSET) # Add this line +logger.addHandler(file_handler) +logger.addHandler(console_handler) # Add this line + +def my_fun(n): + for i in range(0, n): + logging.debug(i) + if i == 50: + logging.warning("The value of i is 50.") + try: + i / (50 - i) + except ZeroDivisionError: + logging.error("Tried to divide by zero. Var i was {}. Recovered gracefully.".format(i)) + +if __name__ == "__main__": + my_fun(100) diff --git a/Student/Ruohan/lesson05/Activity_debugging.txt b/Student/Ruohan/lesson05/Activity_debugging.txt new file mode 100644 index 0000000..69b4a24 --- /dev/null +++ b/Student/Ruohan/lesson05/Activity_debugging.txt @@ -0,0 +1,72 @@ +The function keeps calling itself and it can't stop because +there is no condition to return False + + + +Below is my debugging log: +RuohandeMacBook:lesson05 zhangruohan$ python3 -m pdb recursive.py 9 +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(1)() +-> import sys +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(3)() +-> def my_fun(n): +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(10)() +-> if __name__ == '__main__': +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(11)() +-> n = int(sys.argv[1]) +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(12)() +-> print(my_fun(n)) +(Pdb) pp n +9 +(Pdb) s +--Call-- +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(3)my_fun() +-> def my_fun(n): +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(4)my_fun() +-> if n == 2: +(Pdb) pp n +9 +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(7)my_fun() +-> return my_fun(n/2) +(Pdb) s +--Call-- +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(3)my_fun() +-> def my_fun(n): +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(4)my_fun() +-> if n == 2: +(Pdb) pp n +4.5 +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(7)my_fun() +-> return my_fun(n/2) +(Pdb) s +--Call-- +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(3)my_fun() +-> def my_fun(n): +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(4)my_fun() +-> if n == 2: +(Pdb) pp n +2.25 +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(7)my_fun() +-> return my_fun(n/2) +(Pdb) s +--Call-- +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(3)my_fun() +-> def my_fun(n): +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(4)my_fun() +-> if n == 2: +(Pdb) pp n +1.125 +(Pdb) n +> /Users/zhangruohan/Documents/Python_UW/SP2018-Python220-Accelerated/Student/Ruohan/lesson05/recursive.py(7)my_fun() +-> return my_fun(n/2) +(Pdb) diff --git a/Student/Ruohan/lesson05/assignment_5.py b/Student/Ruohan/lesson05/assignment_5.py new file mode 100644 index 0000000..5a6cb77 --- /dev/null +++ b/Student/Ruohan/lesson05/assignment_5.py @@ -0,0 +1,38 @@ +import logging +import logging.handlers + +format = "%(asctime)s %(filename)s:%(lineno)-3d %(levelname)s %(message)s" +format_without_time = "%(filename)s:%(lineno)-4d %(levelname)s %(message)s" + +formatter_withtime = logging.Formatter(format) +formatter_withouttime = logging.Formatter(format_without_time) + +file_handler = logging.FileHandler('mylog.log') +file_handler.setLevel(logging.WARNING) # Add this line +file_handler.setFormatter(formatter_withtime) + +console_handler = logging.StreamHandler() # Add this line +console_handler.setLevel(logging.DEBUG) # Add this line +console_handler.setFormatter(formatter_withtime) # Add this line + +server_handler = logging.handlers.SysLogHandler(address="/var/run/syslog") # Add this line +server_handler.setLevel(logging.ERROR) # Add this line +server_handler.setFormatter(formatter_withouttime) + +logger = logging.getLogger() +logger.setLevel(logging.NOTSET) # Add this line +logger.addHandler(file_handler) +logger.addHandler(console_handler) # Add this line + +def my_fun(n): + for i in range(0, n): + logging.debug(i) + if i == 50: + logging.warning("The value of i is 50.") + try: + i / (50 - i) + except ZeroDivisionError: + logging.error("Tried to divide by zero. Var i was {}. Recovered gracefully.".format(i)) + +if __name__ == "__main__": + my_fun(100) diff --git a/Student/Ruohan/lesson05/myPysyslog.py b/Student/Ruohan/lesson05/myPysyslog.py new file mode 100644 index 0000000..e69de29 diff --git a/Student/Ruohan/lesson05/mylog.log b/Student/Ruohan/lesson05/mylog.log new file mode 100644 index 0000000..a5a1a97 --- /dev/null +++ b/Student/Ruohan/lesson05/mylog.log @@ -0,0 +1,10 @@ +2018-06-07 20:16:35,500 Activity_5.py:24 WARNING The value of i is 50. +2018-06-07 20:16:35,500 Activity_5.py:28 ERROR Tried to divide by zero. Var i was 50. Recovered gracefully. +2018-06-07 20:17:24,372 Activity_5.py:24 WARNING The value of i is 50. +2018-06-07 20:17:24,372 Activity_5.py:28 ERROR Tried to divide by zero. Var i was 50. Recovered gracefully. +2018-06-10 16:33:00,022 Activity_5.py:24 WARNING The value of i is 50. +2018-06-10 16:33:00,023 Activity_5.py:28 ERROR Tried to divide by zero. Var i was 50. Recovered gracefully. +2018-06-10 16:40:32,152 Activity_5.py:24 WARNING The value of i is 50. +2018-06-10 16:40:32,152 Activity_5.py:28 ERROR Tried to divide by zero. Var i was 50. Recovered gracefully. +2018-06-22 17:39:58,599 assignment_5.py:31 WARNING The value of i is 50. +2018-06-22 17:39:58,599 assignment_5.py:35 ERROR Tried to divide by zero. Var i was 50. Recovered gracefully. diff --git a/Student/Ruohan/lesson05/pysyslog.py b/Student/Ruohan/lesson05/pysyslog.py new file mode 100644 index 0000000..49f6831 --- /dev/null +++ b/Student/Ruohan/lesson05/pysyslog.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +## Tiny Syslog Server in Python. +## +## This is a tiny syslog server that is able to receive UDP based syslog +## entries on a specified port and save them to a file. +## That's it... it does nothing else... +## There are a few configuration parameters. + +LOG_FILE = 'syslog.log' +HOST, PORT = "127.0.0.1", 514 + +# +# NO USER SERVICEABLE PARTS BELOW HERE... +# + +import logging +import socketserver + +logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=LOG_FILE, filemode='a') + +class SyslogUDPHandler(socketserver.BaseRequestHandler): + + def handle(self): + data = bytes.decode(self.request[0].strip()) + socket = self.request[1] + print( "%s : " % self.client_address[0], str(data)) + logging.info(str(data)) + +if __name__ == "__main__": + try: + server = socketserver.UDPServer((HOST,PORT), SyslogUDPHandler) + server.serve_forever(poll_interval=0.5) + except (IOError, SystemExit): + raise + except KeyboardInterrupt: + print ("Crtl+C Pressed. Shutting down.") diff --git a/Student/Ruohan/lesson05/recursive.py b/Student/Ruohan/lesson05/recursive.py new file mode 100644 index 0000000..c0a81a1 --- /dev/null +++ b/Student/Ruohan/lesson05/recursive.py @@ -0,0 +1,12 @@ +import sys + +def my_fun(n): + if n == 2: + return True + else: + return my_fun(n/2) + + +if __name__ == '__main__': + n = int(sys.argv[1]) + print(my_fun(n)) diff --git a/Student/Ruohan/lesson06/__pycache__/calculate.cpython-36.pyc b/Student/Ruohan/lesson06/__pycache__/calculate.cpython-36.pyc new file mode 100644 index 0000000..3b77f59 Binary files /dev/null and b/Student/Ruohan/lesson06/__pycache__/calculate.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/calculate.py b/Student/Ruohan/lesson06/calculate.py new file mode 100644 index 0000000..d68e7c7 --- /dev/null +++ b/Student/Ruohan/lesson06/calculate.py @@ -0,0 +1,14 @@ +import sys +sys.path.append("..") + +class Calculate(object): + def add(self, x, y): + if type(x) == int and type(y) == int: + return x + y + else: + raise TypeError("Invalid type: {} and {}".format(type(x), type(y))) + +if __name__ == '__main__': + calc = Calculate() + result = calc.add(2, 2) + print (result) diff --git a/Student/Ruohan/lesson06/calculator/__pycache__/adder.cpython-36.pyc b/Student/Ruohan/lesson06/calculator/__pycache__/adder.cpython-36.pyc new file mode 100644 index 0000000..038813d Binary files /dev/null and b/Student/Ruohan/lesson06/calculator/__pycache__/adder.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/calculator/__pycache__/calculator.cpython-36.pyc b/Student/Ruohan/lesson06/calculator/__pycache__/calculator.cpython-36.pyc new file mode 100644 index 0000000..891a8e6 Binary files /dev/null and b/Student/Ruohan/lesson06/calculator/__pycache__/calculator.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/calculator/__pycache__/divider.cpython-36.pyc b/Student/Ruohan/lesson06/calculator/__pycache__/divider.cpython-36.pyc new file mode 100644 index 0000000..05e21ae Binary files /dev/null and b/Student/Ruohan/lesson06/calculator/__pycache__/divider.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/calculator/__pycache__/exceptions.cpython-36.pyc b/Student/Ruohan/lesson06/calculator/__pycache__/exceptions.cpython-36.pyc new file mode 100644 index 0000000..a57b720 Binary files /dev/null and b/Student/Ruohan/lesson06/calculator/__pycache__/exceptions.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/calculator/__pycache__/multiplier.cpython-36.pyc b/Student/Ruohan/lesson06/calculator/__pycache__/multiplier.cpython-36.pyc new file mode 100644 index 0000000..eb92825 Binary files /dev/null and b/Student/Ruohan/lesson06/calculator/__pycache__/multiplier.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/calculator/__pycache__/subtracter.cpython-36.pyc b/Student/Ruohan/lesson06/calculator/__pycache__/subtracter.cpython-36.pyc new file mode 100644 index 0000000..7ad5b6b Binary files /dev/null and b/Student/Ruohan/lesson06/calculator/__pycache__/subtracter.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/calculator/__pycache__/test.cpython-36.pyc b/Student/Ruohan/lesson06/calculator/__pycache__/test.cpython-36.pyc new file mode 100644 index 0000000..937c2e8 Binary files /dev/null and b/Student/Ruohan/lesson06/calculator/__pycache__/test.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/calculator/adder.py b/Student/Ruohan/lesson06/calculator/adder.py new file mode 100644 index 0000000..df99630 --- /dev/null +++ b/Student/Ruohan/lesson06/calculator/adder.py @@ -0,0 +1,4 @@ +class Adder(object): + @staticmethod + def calc(operand_1, operand_2): + return operand_1 + operand_2 diff --git a/Student/Ruohan/lesson06/calculator/calculator.py b/Student/Ruohan/lesson06/calculator/calculator.py new file mode 100644 index 0000000..56b63c7 --- /dev/null +++ b/Student/Ruohan/lesson06/calculator/calculator.py @@ -0,0 +1,34 @@ +from exceptions import InsufficientOperands + +class Calculator(object): + def __init__(self, adder, subtracter, multiplier, divider): + self.adder = adder + self.subtracter = subtracter + self.multiplier = multiplier + self.divider = divider + + self.stack = [] + + def enter_number(self, number): + self.stack.insert(0, number) + + def _do_calc(self, operator): + try: + result = operator.calc(self.stack[1], self.stack[0]) + except IndexError: + raise InsufficientOperands + + self.stack = [result] + return result + + def add(self): + return self._do_calc(self.adder) + + def subtract(self): + return self._do_calc(self.subtracter) + + def multiply(self): + return self._do_calc(self.multiplier) + + def divid(self): + return self._do_calc(self.divider) diff --git a/Student/Ruohan/lesson06/calculator/divider.py b/Student/Ruohan/lesson06/calculator/divider.py new file mode 100644 index 0000000..4259530 --- /dev/null +++ b/Student/Ruohan/lesson06/calculator/divider.py @@ -0,0 +1,4 @@ +class Divider(object): + @staticmethod + def calc(operand_1, operand_2): + return operand_1 / operand_2 diff --git a/Student/Ruohan/lesson06/calculator/exceptions.py b/Student/Ruohan/lesson06/calculator/exceptions.py new file mode 100644 index 0000000..f1602cf --- /dev/null +++ b/Student/Ruohan/lesson06/calculator/exceptions.py @@ -0,0 +1,2 @@ +class InsufficientOperands(Exception): + pass diff --git a/Student/Ruohan/lesson06/calculator/multiplier.py b/Student/Ruohan/lesson06/calculator/multiplier.py new file mode 100644 index 0000000..c6aef6a --- /dev/null +++ b/Student/Ruohan/lesson06/calculator/multiplier.py @@ -0,0 +1,4 @@ +class Multiplier(object): + @staticmethod + def calc(operand_1, operand_2): + return operand_1 * operand_2 diff --git a/Student/Ruohan/lesson06/calculator/subtracter.py b/Student/Ruohan/lesson06/calculator/subtracter.py new file mode 100644 index 0000000..8990190 --- /dev/null +++ b/Student/Ruohan/lesson06/calculator/subtracter.py @@ -0,0 +1,4 @@ +class Subtracter(object): + @staticmethod + def calc(operand_1, operand_2): + return operand_1 - operand_2 diff --git a/Student/Ruohan/lesson06/calculator/test.py b/Student/Ruohan/lesson06/calculator/test.py new file mode 100644 index 0000000..fbbfded --- /dev/null +++ b/Student/Ruohan/lesson06/calculator/test.py @@ -0,0 +1,91 @@ +from unittest import TestCase +from unittest.mock import MagicMock +from adder import Adder +from subtracter import Subtracter +from multiplier import Multiplier +from divider import Divider +from calculator import Calculator +from exceptions import InsufficientOperands + + +class AdderTest(TestCase): + + def test_adding(self): + adder = Adder() + + for i in range(-10, 10): + for j in range(-10, 10): + self.assertEqual(i + j, adder.calc(i, j)) + +class SubtracterTest(TestCase): + + def test_subtracting(self): + subtracter = Subtracter() + + for i in range(-10, 10): + for j in range(-10, 10): + self.assertEqual(i - j, subtracter.calc(i, j)) + +class MultiplierTests(TestCase): + + def test_multiplying(self): + multiplier = Multiplier() + + for i in range(-10, 10): + for j in range(-10, 10): + self.assertEqual(i * j, multiplier.calc(i, j)) + +class DividerTests(TestCase): + + def test_dividing(self): + divider = Divider() + + for i in range(-10, 10): + for j in range(1, 10): + self.assertEqual(i / j, divider.calc(i, j)) + +class CalculatorTests(TestCase): + def setUp(self): + self.adder = Adder() + self.subtracter = Subtracter() + self.multiplier = Multiplier() + self.divider = Divider() + + self.calculator = Calculator(self.adder, self.subtracter, self.multiplier, self.divider) + + def test_insufficient_operands(self): + self.calculator.enter_number(0) + with self.assertRaises(InsufficientOperands): + self.calculator.add() + + def test_adder_call(self): + self.adder.calc = MagicMock(return_value = 0) + + self.calculator.enter_number(1) + self.calculator.enter_number(2) + self.calculator.add() + self.adder.calc.assert_called_with(1, 2) + + def test_subtracter_call(self): + self.subtracter.calc = MagicMock(return_value = 0) + + self.calculator.enter_number(1) + self.calculator.enter_number(2) + self.calculator.subtract() + self.subtracter.calc.assert_called_with(1, 2) + + def test_multiplier_call(self): + self.multiplier.calc = MagicMock(return_value = 0) + + self.calculator.enter_number(1) + self.calculator.enter_number(2) + self.calculator.multiply() + self.multiplier.calc.assert_called_with(1, 2) + + def test_divider_call(self): + self.divider.calc = MagicMock(return_value = 0) + + self.calculator.enter_number(1) + self.calculator.enter_number(2) + self.calculator.divid() + self.divider.calc.assert_called_with(1, 2) diff --git a/Student/Ruohan/lesson06/roman-numerals-exercise-master/.gitignore b/Student/Ruohan/lesson06/roman-numerals-exercise-master/.gitignore new file mode 100755 index 0000000..004a8d7 --- /dev/null +++ b/Student/Ruohan/lesson06/roman-numerals-exercise-master/.gitignore @@ -0,0 +1,2 @@ +*.pyc +.coverage diff --git a/Student/Ruohan/lesson06/roman-numerals-exercise-master/README.md b/Student/Ruohan/lesson06/roman-numerals-exercise-master/README.md new file mode 100755 index 0000000..a6863ff --- /dev/null +++ b/Student/Ruohan/lesson06/roman-numerals-exercise-master/README.md @@ -0,0 +1,42 @@ +# Roman Numerals, Advanced Testing Exercise + +Before the invention and wide-spread use of the number "zero", many cultures used different ways to represent large numbers. This exercise is based on the Roman method of representing large numbers, known as "Roman numerals." + +In the Roman numeral system, certain characters represent certain numbers. The following table gives the number value of the Roman numerals that we will use in this exercise: + +* I: 1 +* V: 5 +* X: 10 +* L: 50 +* C: 100 +* D: 500 +* M: 1000 + +A composite number can be produced by listing several characters, from largest-valued to smallest-valued and adding their values. For example: + +* LX: 60 +* LXV: 65 +* MMMD: 3500 + +Additionally, if a smaller-valued numeral _comes before_ a larger-valued numeral, then that value is subtracted from the total value, rather than added. For example: + +* IV: (5 - 1): 4 +* MMIV: 1000 + 1000 + (5 - 1): 2004 +* XC: (100 - 10): 90 + +There's a version of the Roman numeral system where _any_ smaller-valued numeral which comes before a larger-valued numeral is subtracted from the total, but we won't use that version of numerals. + +This repository includes a `RomanToInt` class which can convert a Roman numeral string into an integer. The class works on Roman numeral strings with a value up to 3999. You can use this class at the command line, using the included `main.py`. For example: `python main.py IMMMM`. + +## Your Goals + +1. Write a comprehensive set of tests into `test.py`. +2. All of your tests should pass, using: `python -m unittest test.py`. +3. Running `coverage run --source=roman_numerals -m unittest test.py; coverage report` should give a coverage of 100%. +4. Satisfy the linter such that `pylint roman_numerals` and `flake8 roman_numerals` show no errors. + +## Additional Comments + +Feel free to satisfy the linter through any combination of making improvements to the files and/or creating a pylint configuration file which ignores particular errors. If you create a custom configuration file, then be sure to `git include` it in your repository. + +For the purposes of this exercise, the code may include some bad style. Feel free to refactor the code if you see opportunities for improvement. diff --git a/Student/Ruohan/lesson06/roman-numerals-exercise-master/main.py b/Student/Ruohan/lesson06/roman-numerals-exercise-master/main.py new file mode 100755 index 0000000..2245d78 --- /dev/null +++ b/Student/Ruohan/lesson06/roman-numerals-exercise-master/main.py @@ -0,0 +1,10 @@ +import sys + +from roman_numerals.roman_to_int import RomanToInt + + +if __name__ == "__main__": + s = sys.argv[1] + + print("The Roman numerals {} are {} in decimal.".format(s, RomanToInt.convert(s))) + diff --git a/Student/Ruohan/lesson06/roman-numerals-exercise-master/roman_numerals/__init__.py b/Student/Ruohan/lesson06/roman-numerals-exercise-master/roman_numerals/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/Student/Ruohan/lesson06/roman-numerals-exercise-master/roman_numerals/roman_to_int.py b/Student/Ruohan/lesson06/roman-numerals-exercise-master/roman_numerals/roman_to_int.py new file mode 100755 index 0000000..0b616f5 --- /dev/null +++ b/Student/Ruohan/lesson06/roman-numerals-exercise-master/roman_numerals/roman_to_int.py @@ -0,0 +1,33 @@ +class RomanToInt(object): + + @staticmethod + def value_of(c): + if c == 'I': + return 1 + elif c == 'V': + return 5 + elif c == 'X': + return 10 + elif c == 'L': + return 50 + elif c == 'C': + return 100 + elif c == 'D': + return 500 + elif c == 'M': + return 1000 + else: + raise ValueError("Provided character must be one of: I V X L C D M.") + + @classmethod + def convert(cls, s): + + result = 0 + for i, c in enumerate(s): + if (i + 1) < len(s) and cls.value_of(c) < cls.value_of(s[i + 1]): + result -= cls.value_of(c) + else: + result += cls.value_of(c) + + return result + diff --git a/Student/Ruohan/lesson06/roman-numerals-exercise-master/test.py b/Student/Ruohan/lesson06/roman-numerals-exercise-master/test.py new file mode 100755 index 0000000..2e6675b --- /dev/null +++ b/Student/Ruohan/lesson06/roman-numerals-exercise-master/test.py @@ -0,0 +1,28 @@ +import unittest + +from roman_numerals.roman_to_int import RomanToInt + + +class TestRomanToInteger(unittest.TestCase): + + def test_single_x(self): + self.assertEqual(RomanToInt.convert('I'), 1) + self.assertEqual(RomanToInt.convert('V'), 5) + self.assertEqual(RomanToInt.convert('X'), 10) + self.assertEqual(RomanToInt.convert('L'), 50) + self.assertEqual(RomanToInt.convert('C'), 100) + self.assertEqual(RomanToInt.convert('D'), 500) + self.assertEqual(RomanToInt.convert('M'), 1000) + + + def test_composite_x(self): + self.assertEqual(RomanToInt.convert('LX'), 60) + self.assertEqual(RomanToInt.convert('LXV'), 65) + self.assertEqual(RomanToInt.convert('MMMD'), 3500) + self.assertEqual(RomanToInt.convert('IV'), 4) + self.assertEqual(RomanToInt.convert('MMIV'), 2004) + self.assertEqual(RomanToInt.convert('XC'), 90) + self.assertEqual(RomanToInt.convert('CLX'), 160) + + def test_valid_x(self): + self.assertRaises(ValueError, RomanToInt.value_of('w')) diff --git a/Student/Ruohan/lesson06/test/__pycache__/calculate.cpython-36.pyc b/Student/Ruohan/lesson06/test/__pycache__/calculate.cpython-36.pyc new file mode 100644 index 0000000..314b0af Binary files /dev/null and b/Student/Ruohan/lesson06/test/__pycache__/calculate.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/test/__pycache__/test.cpython-36.pyc b/Student/Ruohan/lesson06/test/__pycache__/test.cpython-36.pyc new file mode 100644 index 0000000..7e73047 Binary files /dev/null and b/Student/Ruohan/lesson06/test/__pycache__/test.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson06/test/test.py b/Student/Ruohan/lesson06/test/test.py new file mode 100644 index 0000000..2efd83c --- /dev/null +++ b/Student/Ruohan/lesson06/test/test.py @@ -0,0 +1,17 @@ +import unittest +import sys +sys.path.append("..") +from calculate import Calculate + + + +class TestCalculate(unittest.TestCase): + def setUp(self): + self.calc = Calculate() + + def test_add_method_returns_correct_result(self): + self.assertEqual(3, self.calc.add('2', 2)) + + +if __name__ == '__main__': + unittest.main() diff --git a/Student/Ruohan/lesson07/.ipynb_checkpoints/lesson_seven_relational_databases (1)-checkpoint.ipynb b/Student/Ruohan/lesson07/.ipynb_checkpoints/lesson_seven_relational_databases (1)-checkpoint.ipynb new file mode 100644 index 0000000..bd67fa0 --- /dev/null +++ b/Student/Ruohan/lesson07/.ipynb_checkpoints/lesson_seven_relational_databases (1)-checkpoint.ipynb @@ -0,0 +1,1523 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Preface: ORM or not?\n", + "\n", + "https://www.quora.com/What-are-the-pros-and-cons-of-using-raw-SQL-versus-ORM-for-database-development\n", + "\n", + "https://www.quora.com/Which-is-better-ORM-or-Plain-SQL\n", + "\n", + "http://mikehadlow.blogspot.com/2012/06/when-should-i-use-orm.html\n", + "\n", + "https://enterprisecraftsmanship.com/2015/11/30/do-you-need-an-orm/\n", + "\n", + "https://medium.com/@mithunsasidharan/should-i-or-should-i-not-use-orm-4c3742a639ce\n", + "\n", + "https://xkcd.com/1409/\n", + "\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
🐍
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "

Relational Databases

\n", + "
\n", + "

Introduction

\n", + "

In this lesson we are going to look at how to use a relational database with Python. Using relational databases is a massive subject in its own right, but we are going to concentrate on how to use these technologies simply and effectively.

\n", + "

What we learn here will be a foundation on which you can build as your database needs increase in volume and complexity.

\n", + "

Learning Objectives

\n", + "

Upon successful completion of this lesson, you will be able to:

\n", + "
    \n", + "
  • Apply data definition techniques to help assure the quality of the data your Python programs use.
  • \n", + "
  • Store and retrieve single and multiple sets of records in a database from your Python programs so that you can leverage data management services from a database.
  • \n", + "
  • Use simple techniques to help model data correctly in a relational database, and recognize the tradeoffs between different options for this. 
  • \n", + "
\n", + "

New Words, Concepts, and Tools

\n", + "
    \n", + "
  • We are going to learn about relational databases, data definition and management, and SQL. We will cover object relational mapping and relational database design, but all aligned to simplicity of use with Python. 
  • \n", + "
\n", + "

Required Reading

\n", + "\n", + "

Optional Reading

\n", + "
    \n", + "
  • How to interact with sqlite from Python (does not use Peewee)
  • \n", + "
  • How to interact with PostgreSQL from Python.
  • \n", + "
  • A great reference book for SQL that shows details of SQL for several databases is \"SQL in a Nutshell: A Desktop Quick Reference Guide\"
  • \n", + "
  • \n", + "

    If you really want to understand the details of SQL, then this is an excellent book: \"Joe Celko's SQL Programming Style (The Morgan Kaufmann Series in Data Management Systems)\"

    \n", + "
  • \n", + "
  • Data model design is a complex topic that requires lots of knowledge. If you do a lot of database work then the three books in the series \"The Data Model Resource Book\" (Vol. 1, 2 and 3) are invaluable (the links are the numbers).
  • \n", + "
\n", + "

\n", + "\n", + "\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "

Relational Databases

\n", + "
\n", + "

Here we are going to cover the background to why we use databases, and set the stage for later, when we will start to\n", + " develop a Python database application.

\n", + "

You will probably wish to clone the repository you can find on\n", + " GitHub. For this first video, the files are in the \"stuff\" directory. These are the files we refer to, and they\n", + " are included here, below the video:

\n", + "\n", + "

 

\n", + "

Video 1: Relational Databases with Python

\n", + "\n", + "\n", + "

simple_data_write.py

" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\"\"\"\n", + "write a csv file\n", + "data is \"anonymous\" (no schema)\n", + "\n", + "Creates a file that looks like this:\n", + "\n", + "John,502-77-3056,2/27/1985,117.45\n", + "\n", + "\"\"\"\n", + "\n", + "import csv\n", + "\n", + "peopledata = ['John', '502-77-3056', '2/27/1985', 117.45]\n", + "\n", + "with open('simple_data_write.csv', 'w') as people:\n", + " peoplewriter = csv.writer(people, quotechar=\"'\", quoting=csv.QUOTE_ALL)\n", + " peoplewriter.writerow(peopledata)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```\n", + "'John','502-77-3056','2/27/1985','117.45'\n", + "```\n", + "

\n", + "

simple_data_write_headers.py

" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\"\"\"\n", + "write a csv file\n", + "data is \"anonymous\" (no schema)\n", + "\n", + "Creates a file that looks like this:\n", + "\n", + "John,502-77-3056,2/27/1985,117.45\n", + "\n", + "\"\"\"\n", + "\n", + "import csv\n", + "\n", + "people_headers = ['Person Name','SSN','BirthDate','Account Balance']\n", + "people_data = [ \n", + " ['John', '502-77-3056', '2/27/1985', 117.45],\n", + " ['Jane', '756-01-5323', '12/01/1983', 120.9], \n", + "]\n", + "\n", + "with open('simple_data_write_headers.tsv', 'w') as people:\n", + " peoplewriter = csv.writer(people, delimiter='|', quoting=csv.QUOTE_NONNUMERIC)\n", + " peoplewriter.writerow(people_headers)\n", + " for person in people_data:\n", + " peoplewriter.writerow(person)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```\n", + "\"Person Name\"|\"SSN\"|\"BirthDate\"|\"Account Balance\"\n", + "\"John\"|\"502-77-3056\"|\"2/27/1985\"|\"117.45\"\n", + "\"Jane\"|\"756-01-5323\"|\"12/01/1983\"|\"120.9\"\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

 

\n", + "

We have covered the basis of data definition, and why it is important. We now know what a schema is and why it is important.\n", + " Now we can start to write a Python program that uses a database.

\n", + "

 

\n", + "

\n", + " Be sure you cloned the repository we mentioned prior to video 1 from \n", + " GitHub\n", + " . In this video we will be using the modules in the \"src\" directory We start with \n", + " v00_personjob_model.py\n", + "

\n", + "

Key fragments are included here too, below the video.

\n", + "\n", + "\n", + "

Video 2: Using the Model, Using the Person Class, Using the Job Class

\n", + "\n", + "

 Here is the model code:

\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:__main__:One off program to build the classes from the model in the database\n", + "INFO:__main__:Here we define our data (the schema)\n", + "INFO:__main__:First name and connect to a database (sqlite here)\n", + "INFO:__main__:The next 3 lines of code are the only database specific code\n", + "INFO:__main__:This means we can easily switch to a different database\n", + "INFO:__main__:Enable the Peewee magic! This base class does it all\n", + "INFO:__main__:By inheritance only we keep our model (almost) technology neutral\n", + "INFO:__main__:Note how we defined the class\n", + "INFO:__main__:Specify the fields in our model, their lengths and if mandatory\n", + "INFO:__main__:Must be a unique identifier for each person\n", + "INFO:__main__:Now the Job class with a simlar approach\n", + "INFO:__main__:Dates\n", + "INFO:__main__:Number\n", + "INFO:__main__:Which person had the Job\n", + "INFO:__main__:An alternate Person class\n", + "INFO:__main__:Note: no primary key so we're give one 'for free'\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\"\"\n", + " Simple database example with Peewee ORM, sqlite and Python\n", + " Here we define the schema\n", + " Use logging for messages so they can be turned off\n", + "\"\"\"\n", + "\n", + "import logging\n", + "from peewee import *\n", + "\n", + "logging.basicConfig(level=logging.INFO)\n", + "logger = logging.getLogger(__name__)\n", + "\n", + "logger.info('One off program to build the classes from the model in the database')\n", + "\n", + "logger.info('Here we define our data (the schema)')\n", + "logger.info('First name and connect to a database (sqlite here)')\n", + "\n", + "logger.info('The next 3 lines of code are the only database specific code')\n", + "\n", + "database = SqliteDatabase('personjob.db')\n", + "database.connect()\n", + "database.execute_sql('PRAGMA foreign_keys = ON;') # needed for sqlite only\n", + "\n", + "logger.info('This means we can easily switch to a different database')\n", + "logger.info('Enable the Peewee magic! This base class does it all')\n", + "logger.info('By inheritance only we keep our model (almost) technology neutral')\n", + "\n", + "class BaseModel(Model):\n", + " class Meta:\n", + " database = database\n", + "\n", + "class Person(BaseModel):\n", + " \"\"\"\n", + " This class defines Person, which maintains details of someone\n", + " for whom we want to research career to date.\n", + " \"\"\"\n", + "\n", + " logger.info('Note how we defined the class')\n", + "\n", + " logger.info('Specify the fields in our model, their lengths and if mandatory')\n", + " logger.info('Must be a unique identifier for each person')\n", + "\n", + " person_name = CharField(primary_key = True, max_length = 30)\n", + " lives_in_town = CharField(max_length = 40)\n", + " nickname = CharField(max_length = 20, null = True)\n", + "\n", + "class Job(BaseModel):\n", + " \"\"\"\n", + " This class defines Job, which maintains details of past Jobs\n", + " held by a Person.\n", + " \"\"\"\n", + "\n", + " logger.info('Now the Job class with a simlar approach')\n", + " job_name = CharField(primary_key = True, max_length = 30)\n", + " logger.info('Dates')\n", + " start_date = DateField(formats = 'YYYY-MM-DD')\n", + " end_date = DateField(formats = 'YYYY-MM-DD')\n", + " logger.info('Number')\n", + "\n", + " salary = DecimalField(max_digits = 7, decimal_places = 2)\n", + " logger.info('Which person had the Job')\n", + " person_employed = ForeignKeyField(Person, related_name='was_filled_by', null = False)\n", + "\n", + "class PersonNumKey(BaseModel):\n", + " \"\"\"\n", + " This class defines Person, which maintains details of someone\n", + " for whom we want to research career to date.\n", + " \"\"\"\n", + "\n", + " logger.info('An alternate Person class')\n", + " logger.info(\"Note: no primary key so we're give one 'for free'\")\n", + "\n", + " person_name = CharField(max_length = 30)\n", + " lives_in_town = CharField(max_length = 40)\n", + " nickname = CharField(max_length = 20, null = True)\n", + "\n", + "database.create_tables([\n", + " Job,\n", + " Person,\n", + " PersonNumKey\n", + " ])\n", + "\n", + "database.close()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "

 

\n", + "

Now we have looked at the model, lets look at how we create, read, and delete data from the database, using the Person class. Here\n", + " we use the following code:\n", + "  \n", + " v3_p1_populate_db.py, then \n", + " v3_p1_populate_db.py and finally \n", + " v3_p3_add_and_delete.py\n", + " .

\n", + "

Video 3: Using the Person class

\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p1.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "import logging\n", + "# from personjob_model import *\n", + "\n", + "def populate_db():\n", + " \"\"\"\n", + " add person data to database\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Working with Person class')\n", + " logger.info('Note how I use constants and a list of tuples as a simple schema')\n", + " logger.info('Normally you probably will have prompted for this from a user')\n", + "\n", + " PERSON_NAME = 0\n", + " LIVES_IN_TOWN = 1\n", + " NICKNAME = 2\n", + "\n", + " people = [\n", + " ('Andrew', 'Sumner', 'Andy'),\n", + " ('Peter', 'Seattle', None),\n", + " ('Susan', 'Boston', 'Beannie'),\n", + " ('Pam', 'Coventry', 'PJ'),\n", + " ('Steven', 'Colchester', None),\n", + " ]\n", + "\n", + " logger.info('Creating Person records: iterate through the list of tuples')\n", + " logger.info('Prepare to explain any errors with exceptions')\n", + " logger.info('and the transaction tells the database to fail on error')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " for person in people:\n", + " with database.transaction():\n", + " new_person = Person.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + " logger.info('Database add successful')\n", + "\n", + " logger.info('Print the Person records we saved...')\n", + " for saved_person in Person:\n", + " logger.info(f'{saved_person.person_name} lives in {saved_person.lives_in_town} ' +\\\n", + " f'and likes to be known as {saved_person.nickname}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[PERSON_NAME]}')\n", + " logger.info(e)\n", + " logger.info('See how the database protects our data')\n", + "\n", + " finally:\n", + " logger.info('database closes')\n", + " database.close()\n", + "\n", + "populate_db()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p2.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "# from personjob_model import *\n", + "\n", + "import logging\n", + "\n", + "def select_and_update():\n", + " \"\"\"\"\n", + " show how we can select a specific record, and then search and read through several records\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + "\n", + " logger.info('Find and display by selecting a spcific Person name...')\n", + " aperson = Person.get(Person.person_name == 'Susan')\n", + "\n", + " logger.info(f'{aperson.person_name} lives in {aperson.lives_in_town} ' + \\\n", + " f' and likes to be known as {aperson.nickname}')\n", + "\n", + " logger.info('Search and display all Person with missing nicknames')\n", + " logger.info('Our person class inherits select(). Specify search with .where()')\n", + " logger.info('Peter gets a nickname but noone else')\n", + "\n", + " for person in Person.select().where(Person.nickname == None):\n", + " logger.info(f'{person.person_name} does not have a nickname; see: {person.nickname}')\n", + " if person.person_name == 'Peter':\n", + " logger.info('Changing nickname for Peter')\n", + " logger.info('Update the database')\n", + " person.nickname = 'Painter'\n", + " person.save()\n", + " else:\n", + " logger.info(f'Not giving a nickname to {person.person_name}')\n", + "\n", + " logger.info('And here is where we prove it by finding Peter and displaying')\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " logger.info(f'{aperson.person_name} now has a nickname of {aperson.nickname}')\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "select_and_update()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p3.py\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "import logging\n", + "\n", + "# from personjob_model import *\n", + "\n", + "database = SqliteDatabase('personjob.db')\n", + "\n", + "def add_and_delete():\n", + " \"\"\"\"\n", + " show how we can add a record, and delete a record\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + "\n", + " logger.info('Add and display a Person called Fred; then delete him...')\n", + " logger.info('Add Fred in one step')\n", + "\n", + " new_person = Person.create(\n", + " person_name = 'Fred',\n", + " lives_in_town = 'Seattle',\n", + " nickname = 'Fearless')\n", + " new_person.save()\n", + "\n", + " logger.info('Show Fred')\n", + " aperson = Person.get(Person.person_name == 'Fred')\n", + "\n", + " logger.info(f'We just created {aperson.person_name}, who lives in {aperson.lives_in_town}')\n", + " logger.info('but now we will delete him...')\n", + "\n", + " aperson.delete_instance()\n", + "\n", + " logger.info('Reading and print all Person records (but not Fred; he has been deleted)...')\n", + "\n", + " for person in Person:\n", + " logger.info(f'{person.person_name} lives in {person.lives_in_town} and likes to be known as {person.nickname}')\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "add_and_delete()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "

 

\n", + "

Working with one class is not typical. Usually we will have several. We'll illustrate this by working with the Job class.\n", + " He we will use all the Python modules for the repository that start with v4:

\n", + "

Video 4: Using the Job class

\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://github.com/milesak60/RDBMS-example/blob/master/personjob_learning_v5_p1.py\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "# from personjob_model import *\n", + "\n", + "import logging\n", + "\n", + "def populate_db():\n", + " \"\"\"\n", + " add job data to database\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Working with Job class')\n", + " logger.info('Creating Job records: just like Person. We use the foreign key')\n", + "\n", + " JOB_NAME = 0\n", + " START_DATE = 1\n", + " END_DATE = 2\n", + " SALARY = 3\n", + " PERSON_EMPLOYED = 4\n", + "\n", + " jobs = [\n", + " ('Analyst', '2001-09-22', '2003-01-30',65500, 'Andrew'),\n", + " ('Senior analyst', '2003-02-01', '2006-10-22', 70000, 'Andrew'),\n", + " ('Senior business analyst', '2006-10-23', '2016-12-24', 80000, 'Andrew'),\n", + " ('Admin supervisor', '2012-10-01', '2014-11,10', 45900, 'Peter'),\n", + " ('Admin manager', '2014-11-14', '2018-01,05', 45900, 'Peter')\n", + " ]\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " for job in jobs:\n", + " with database.transaction():\n", + " new_job = Job.create(\n", + " job_name = job[JOB_NAME],\n", + " start_date = job[START_DATE],\n", + " end_date = job[END_DATE],\n", + " salary = job[SALARY],\n", + " person_employed = job[PERSON_EMPLOYED])\n", + " new_job.save()\n", + "\n", + " logger.info('Reading and print all Job rows (note the value of person)...')\n", + " for job in Job:\n", + " logger.info(f'{job.job_name} : {job.start_date} to {job.end_date} for {job.person_employed}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {job[JOB_NAME]}')\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " logger.info('database closes')\n", + " database.close()\n", + "\n", + "populate_db()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p2.py\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def join_classes():\n", + " \"\"\"\n", + " demonstrate how to join classes together : matches\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Working with Job class')\n", + "\n", + " logger.info('Now resolve the join and print (INNER shows only jobs that match person)...')\n", + " logger.info('Notice how we use a query variable in this case')\n", + " logger.info('We select the classes we need, and we join Person to Job')\n", + " logger.info('Inner join (which is the default) shows only records that match')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " query = (Person\n", + " .select(Person, Job)\n", + " .join(Job, JOIN.INNER)\n", + " )\n", + " \n", + " \"\"\"\n", + " select distinct p.*, j.* \n", + " from\n", + " person as p \n", + " inner join job as j\n", + " p.person_first_name = j.first_name and\n", + " p.person_last_name = j.last_name\n", + " limit 100 \n", + " \n", + " \"\"\"\n", + "\n", + " logger.info('View matching records from both tables')\n", + " for person in query:\n", + " logger.info(f'Person {person.person_name} had job {person.job.job_name}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {job[JOB_NAME]}')\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " logger.info('database closes')\n", + " database.close()\n", + "\n", + "join_classes()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p3.py\n", + "\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def join_classes():\n", + " \"\"\"\n", + " demonstrate how to join classes together : no matches too\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('View matching records and Persons without Jobs (note LEFT_OUTER)')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " query = (Person\n", + " .select(Person, Job)\n", + " .join(Job, JOIN.LEFT_OUTER)\n", + " )\n", + "\n", + " \n", + " \n", + " \n", + " for person in query:\n", + " try:\n", + " logger.info(f'Person {person.person_name} had job {person.job.job_name}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Person {person.person_name} had no job')\n", + "\n", + "\n", + " logger.info('Example of how to summarize data')\n", + " logger.info('Note select() creates a count and names it job_count')\n", + " logger.info('group_by and order_by control level and sorting')\n", + "\n", + " query = (Person\n", + " .select(Person, fn.COUNT(Job.job_name).alias('job_count'))\n", + " .join(Job, JOIN.LEFT_OUTER)\n", + " .group_by(Person)\n", + " .order_by(Person.person_name))\n", + "\n", + " \"\"\"\n", + " select p.person_first_name, p.person_last_name, count(j.job_name) as job_count \n", + " from\n", + " person as p \n", + " left outer join job as j\n", + " p.person_first_name = j.first_name and\n", + " p.person_last_name = j.last_name\n", + " group by p.person_first_name, p.person_last_name\n", + " \n", + " \n", + " \"\"\" \n", + " \n", + " \n", + " for person in query:\n", + " logger.info(f'{person.person_name} had {person.job_count} jobs')\n", + "\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "join_classes()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p4.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def show_integrity_add():\n", + " \"\"\"\n", + " demonstrate how database protects data inegrity : add\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " with database.transaction():\n", + " logger.info('Try to add a new job where a person doesnt exist...')\n", + "\n", + " addjob = ('Sales', '2010-04-01', '2018-02-08', 80400, 'Harry')\n", + "\n", + " logger.info('Adding a sales job for Harry')\n", + " logger.info(addjob)\n", + " new_job = Job.create(\n", + " job_name = addjob[JOB_NAME],\n", + " start_date = addjob[START_DATE],\n", + " end_date = addjob[END_DATE],\n", + " salary = addjob[SALARY],\n", + " person_employed = addjob[PERSON_EMPLOYED])\n", + " new_job.save()\n", + "\n", + " except Exception as e:\n", + " logger.info('Add failed because Harry is not in Person')\n", + " logger.info(f'For Job create: {addjob[0]}')\n", + " logger.info(e)\n", + "\n", + "show_integrity_add()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p5.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def show_integrity_del():\n", + " \"\"\"\n", + " demonstrate how database protects data inegrity : delete\n", + " \"\"\"\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " logger.info('Try to Delete a person who has jobs...')\n", + " with database.transaction():\n", + " aperson = Person.get(Person.person_name == 'Andrew')\n", + " logger.info(f'Trying to delete {aperson.person_name} who lives in {aperson.lives_in_town}')\n", + " aperson.delete_instance()\n", + " \n", + " \"\"\"delete from person where person_name = 'Andrew'\"\"\"\n", + "\n", + " except Exception as e:\n", + " logger.info('Delete failed because Andrew has Jobs')\n", + " logger.info(f'Delete failed: {aperson.person_name}')\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + " \n", + "show_integrity_del()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p6.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def add_with_without_BK():\n", + " \"\"\"\n", + " demonstrate impact of business keys\n", + " \"\"\"\n", + "\n", + " PERSON_NAME = 0\n", + " LIVES_IN_TOWN = 1\n", + " NICKNAME = 2\n", + " people = [\n", + " ('Andrew', 'Sumner', 'Andy'),\n", + " ('Peter', 'Seattle', None),\n", + " ('Susan', 'Boston', 'Beannie'),\n", + " ('Pam', 'Coventry', 'PJ'),\n", + " ('Steven', 'Colchester', None),\n", + " ]\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Try creating Person records again: it will fail')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " try:\n", + " with database.transaction():\n", + " for person in people:\n", + " new_person = Person.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + " logger.info('Database add successful')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[PERSON_NAME]}')\n", + " logger.info(e)\n", + "\n", + " logger.info('We make sure duplicates are not unintentionally created this way')\n", + " logger.info('BUT: how do we really identify a Person (uniquely)?')\n", + "\n", + " logger.info('Creating Person records, but in a new table with generated PK...')\n", + " try:\n", + " with database.transaction():\n", + " for person in people:\n", + " new_person = PersonNumKey.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[0]}')\n", + " logger.info(e)\n", + "\n", + " logger.info('Watch what happens when we do it again')\n", + "\n", + " try:\n", + " with database.transaction():\n", + " for person in people:\n", + " new_person = PersonNumKey.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[0]}')\n", + " logger.info(e)\n", + "\n", + " logger.info('Note no PK specified, no PK violation; \"duplicates\" created!')\n", + " for person in PersonNumKey.select():\n", + " logger.info(f'Name : {person.person_name} with id: {person.id}')\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "add_with_without_BK()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p7.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def cant_change_PK():\n", + " \"\"\"\n", + " show that PKs cant be changed (and that there is no error!)\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " logger.info(\"Back to Person class: try to change Peter's name\")\n", + "\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " logger.info(f'Current value is {aperson.person_name}')\n", + "\n", + " logger.info('Update Peter to Peta, thereby trying to change the PK...')\n", + "\n", + " try:\n", + " try:\n", + " with database.transaction():\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " aperson.person_name = 'Peta'\n", + " aperson.save()\n", + " logger.info(f'Tried to change Peter to {aperson.person_name}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Cant change a PK and caught you trying') # not caught; no error thrown by Peewee\n", + " logger.info(e)\n", + "\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " logger.info(f'Looked for Peter: found! -> {aperson.person_name}')\n", + "\n", + " try:\n", + " aperson = Person.get(Person.person_name == 'Peta')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Looking for Peta results in zero records. PK changes are ignored and do not throw an error!!!!')\n", + " logger.info(f'Cant change a PK')\n", + " logger.info('PK \"change\" can only be achieved with a delete and new create')\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "cant_change_PK()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "

 

\n", + "

Now we are going to learn about the best way to design the data in our database.  We will use the digram in the \"stuff\"\n", + " directory, which is also included below, along with the SQL code:

\n", + "

 

\n", + "

Video: Behind the scenes / Database Technologies

\n", + "\n", + "

 

\n", + "

Database diagram:

\n", + "

 \n", + "  

\n", + "

Code samples from the video:

\n", + "

SQL statement

\n", + "
\n", + "
select* from person;\n",
+    "
\n", + "
\n", + "

Start sqlite3 database (from the command line):

\n", + "
\n", + "
sqlite3 personjob.db\n",
+    "
\n", + "
\n", + "

The sqlite> prompt indicates we are ready to enter sqlite commands.

\n", + "
\n", + "
sqlite> .tables\n",
+    "job person personnumkey\n",
+    "
\n", + "
\n", + "

  Here is how sqlite sees the schema:

\n", + "
\n", + "
sqlite> .schema\n",
+    "\n",
+    "CREATE TABLE IF NOT EXISTS \"person\" (\"person_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));
\n", + "CREATE TABLE IF NOT EXISTS \"job\" (\"job_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"start_date\" DATE NOT NULL, \"end_date\" DATE NOT NULL, \"salary\" DECIMAL(7, 2) NOT NULL, \"person_employed_id\" VARCHAR(30) NOT NULL, FOREIGN KEY (\"person_employed_id\") REFERENCES \"person\" (\"person_name\"));
\n", + "CREATE INDEX \"job_person_employed_id\" ON \"job\" (\"person_employed_id\");
\n", + "CREATE TABLE IF NOT EXISTS \"personnumkey\" (\"id\" INTEGER NOT NULL PRIMARY KEY, \"person_name\" VARCHAR(30) NOT NULL, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));
\n", + "
\n", + "
\n", + "

 

\n", + "

 

\n", + "
\n", + "
sqlite> .mode column\n",
+    "sqlite> .width 15 15 15 15 15\n",
+    "sqlite> .headers on\n",
+    "
\n", + "
\n", + "

 

\n", + "
\n", + "
sqlite> select * from person;\n",
+    "sqlite> select * from job;\n",
+    "
\n", + "
\n", + "

Enter .quit to leave sqlite.

\n", + "

Lesson Summary

\n", + "

In this lesson we have learned about how we define, store and retrieve data in a relational database using Python, Peewee\n", + " and sqlite.

\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

 

\n", + "

Video: Conclusion

\n", + "

 \n", + " PeeWee documentation\n", + "

\n", + "

 

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "## Non-ORM Example" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CREATE TABLE IF NOT EXISTS \"person\" (\"person_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\n", + "CREATE TABLE IF NOT EXISTS \"job\" (\"job_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"start_date\" DATE NOT NULL, \"end_date\" DATE NOT NULL, \"salary\" DECIMAL(7, 2) NOT NULL, \"person_employed_id\" VARCHAR(30) NOT NULL, FOREIGN KEY (\"person_employed_id\") REFERENCES \"person\" (\"person_name\"));\n", + "CREATE INDEX \"job_person_employed_id\" ON \"job\" (\"person_employed_id\");\n", + "CREATE TABLE IF NOT EXISTS \"personnumkey\" (\"id\" INTEGER NOT NULL PRIMARY KEY, \"person_name\" VARCHAR(30) NOT NULL, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\n" + ] + } + ], + "source": [ + "import sqlite3\n", + "import os\n", + "\n", + "database_filename = \"personjob.db\"\n", + "\n", + "try:\n", + " # remove if file exists\n", + " os.remove(database_filename)\n", + "except:\n", + " # ignore if file doesn't exist\n", + " pass \n", + "\n", + "statements = [\n", + " \"\"\"CREATE TABLE IF NOT EXISTS \"person\" (\"person_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\"\"\",\n", + " \"\"\"CREATE TABLE IF NOT EXISTS \"job\" (\"job_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"start_date\" DATE NOT NULL, \"end_date\" DATE NOT NULL, \"salary\" DECIMAL(7, 2) NOT NULL, \"person_employed_id\" VARCHAR(30) NOT NULL, FOREIGN KEY (\"person_employed_id\") REFERENCES \"person\" (\"person_name\"));\"\"\",\n", + " \"\"\"CREATE INDEX \"job_person_employed_id\" ON \"job\" (\"person_employed_id\");\"\"\",\n", + " \"\"\"CREATE TABLE IF NOT EXISTS \"personnumkey\" (\"id\" INTEGER NOT NULL PRIMARY KEY, \"person_name\" VARCHAR(30) NOT NULL, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\"\"\",\n", + "]\n", + "\n", + "with sqlite3.connect(database_filename) as conn:\n", + " try: \n", + " for stmt in statements:\n", + " print(stmt)\n", + " conn.execute(stmt)\n", + " except Exception as e:\n", + " print(e)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "\n", + "
\n", + "

Activity

\n", + "
\n", + "

Warm up for the assignment

\n", + "

Before we launch into the assignment, let's be sure that you have everything you need to get started. We'll enhance the modules from the video along the way.

\n", + "

Preparing

\n", + "

Be sure to

\n", + "
\n", + "
pip install peewee\n",
+    "
\n", + "
\n", + "

 first.

\n", + "

Also, be sure that  

\n", + "
\n", + "
sqlite3 -version\n",
+    "
\n", + "
\n", + "

returns the sqlite3 version number, indicating it is installed. It should be, as it's bundled with Python 3.

\n", + "

Clone the repo at 

\n", + "
git@github.com:milesak60/RDBMS.git
\n", + "

although you should already have that from earlier in this lesson.

\n", + "

Make sure everything runs before proceeding to the next step.

\n", + "

Let's add a department

\n", + "

We have details of Persons. We have details of Jobs. Now we need to track in which Department a Person held a Job. For a Department, we need to know it's department number, which is 4 characters long and start with a letter. We need to know the department name (30 characters), and the name of the department manager (30 characters). We also need to know the duration in days that the job was held. Think about this last one carefully.

\n", + "

Make the necessary changes, annotating the code with log statements to explain what's going on. Also, draw a digram to help think through how you will incorporate Department into the programs.

\n", + "

Finally, produce a list using pretty print that shows all of the departments a person worked in for every job they ever had. 

\n", + "

Instructions

\n", + "

Once you've completed the activity from the lesson content, commit your changes and submit:

\n", + "
    \n", + "
  • a link to your repository on GitHub
  • \n", + "
  • the relevant .py file(s)
  • \n", + "
\n", + "

We'll be grading this activity purely on the percentage of included tests that pass.

\n", + "

Submitting Your Work 

\n", + "

Put the file(s) (ex: a_new_file.py) in your student directory in a new subdirectory named for this lesson, and add it to your clone early (git add a_new_file.py). Make frequent commits with good, clear messages about what you're doing and why.

\n", + "

When you're done and ready for the instructors to review your work, push your changes to your GitHub fork (git push) and then go to the GitHub website and make a pull request. Copy the link to the pull request.

\n", + "

Click the Submit Assignment button in the upper right.

\n", + "

Part 1: File(s)

\n", + "

Use the Choose File button to find and select the saved .py file or, if there are multiple files for the assignment, the .zip file.

\n", + "

Part 2: GitHub Link

\n", + "

Paste the GitHub link to the pull request in the comments area.

\n", + "

Click the Submit Assignment button.

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "\n", + "
\n", + "

Assignment

\n", + "
\n", + "

Instructions

\n", + "

Goal:

\n", + "

Redesign the object oriented mailroom program from class one using Peewee classes so that the data is persisted in sqlite. The approach you used in the OO exercise will naturally lend itself to this. (See Lesson 4, video 2)

\n", + "

Suggestions

\n", + "

You will need ways to add, update and delete data. Update and delete will mean that you need to find the data to update / delete. Perhaps you can do this by allowing a search first, and then selecting the particular instance to delete.

\n", + "

Remember that you need to read from the database, rather than relying on data held in variables when your program is running. To show you understand how this works, run the donor report from a separate program that read the database.

\n", + "

Generally, be sure to adapt the exception handling so that it helps identify any database errors, and consider how you may need to adapt your tests.

\n", + "

Be sure to give a lot of thought to what you should use as the primary key for your Peewee classes. In doing this, just consider data items that are unique in the context of the mailroom application. If you have to resort to generated keys, be sure to note why in the applicable docstring. And talking of which, be sure to define all your classes, as you learned in the videos.

\n", + "

The example programs for the videos will be a good starting point for reminders of syntax, but remember that the primary determinate of the structure of your solution will be a good object oriented Python application. The fact that it will now be persistent should not make too ,much difference. 

\n", + "

\n", + "

Submitting Your Work 

\n", + "

Put the file(s) (ex: a_new_file.py) in your student directory in a new subdirectory named for this lesson, and add it to your clone early (git add a_new_file.py). Make frequent commits with good, clear messages about what you're doing and why.

\n", + "

When you're done and ready for the instructors to review your work, push your changes to your GitHub fork (git push) and then go to the GitHub website and make a pull request. Copy the link to the pull request.

\n", + "

Click the Submit Assignment button in the upper right.

\n", + "

Part 1: File(s)

\n", + "

Use the Choose File button to find and select the saved .py file or, if there are multiple files for the assignment, the .zip file.

\n", + "

Part 2: GitHub Link

\n", + "

Paste the GitHub link to the pull request in the comments area.

\n", + "

Click the Submit Assignment button.

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
👹
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "\n", + "
\n", + "
\n", + "\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "\n", + "### Mailroom\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\"\"\"\n", + " Simple database example with Peewee ORM, sqlite and Python\n", + " Here we define the schema\n", + " Use logging for messages so they can be turned off\n", + "\"\"\"\n", + "\n", + "import logging\n", + "from peewee import *\n", + "\n", + "logging.basicConfig(level=logging.INFO)\n", + "logger = logging.getLogger(__name__)\n", + "\n", + "logger.info('One off program to build the classes from the model in the database')\n", + "\n", + "logger.info('Here we define our data (the schema)')\n", + "logger.info('First name and connect to a database (sqlite here)')\n", + "\n", + "logger.info('The next 3 lines of code are the only database specific code')\n", + "\n", + "database = SqliteDatabase('personjob.db')\n", + "database.connect()\n", + "database.execute_sql('PRAGMA foreign_keys = ON;') # needed for sqlite only\n", + "\n", + "logger.info('This means we can easily switch to a different database')\n", + "logger.info('Enable the Peewee magic! This base class does it all')\n", + "logger.info('By inheritance only we keep our model (almost) technology neutral')\n", + "\n", + "class BaseModel(Model):\n", + " class Meta:\n", + " database = database\n", + "\n", + "class Person(BaseModel):\n", + " \"\"\"\n", + " This class defines Person, which maintains details of someone\n", + " for whom we want to research career to date.\n", + " \"\"\"\n", + "\n", + " logger.info('Note how we defined the class')\n", + "\n", + " logger.info('Specify the fields in our model, their lengths and if mandatory')\n", + " logger.info('Must be a unique identifier for each person')\n", + "\n", + " first_name = CharField(max_length = 30)\n", + " last_name = CharField(max_length = 30)\n", + " nickname = CharField(max_length = 20, null = True)\n", + " \n", + " city = CharField(max_length = 40, null = True)\n", + " state = CharField(max_length = 40, null = True)\n", + " email = CharField(max_length = 40)\n", + " phone = CharField(max_length = 40)\n", + " \n", + " employer_id = IntegerField()\n", + " \n", + "\n", + "class Employer(BaseModel):\n", + " employer_name = CharField(max_length = 30)\n", + " city = CharField(max_length = 40)\n", + " state = CharField(max_length = 40) \n", + " website = CharField(max_length = 40)\n", + " contact_person = CharField(max_length = 150)\n", + " contact_phone = CharField(max_length = 20)\n", + " contact_email = CharField(max_length = 20)\n", + " employee_match = IntegerField(1)\n", + " employee_size = IntegerField()\n", + " \n", + "class EmployerInterests(BaseModel):\n", + " employer_id = IntegerField()\n", + " employer_interest_category = IntegerField()\n", + " employer_interest_details = CharField(max_length = 250)\n", + "\n", + " \n", + "\n", + "class Donor(BaseModel):\n", + " person_id = IntegerField()\n", + "\n", + "\n", + "class Donations(BaseModel):\n", + " person_id = IntegerField()\n", + " amount = DecimalField()\n", + " donation_date = DateTimeField()\n", + " \n", + "\"\"\"\n", + " select person_id, count(donations_id) as donation_frequency\n", + " from donations\n", + " group by person_id\n", + " \n", + "\"\"\"\n", + " \n", + " \n", + "class EmployerInterests(BaseModel):\n", + " employer_id = IntegerField()\n", + " employer_interest_category = IntegerField()\n", + " employer_interest_details = CharField(max_length = 250) \n", + "\n", + " \n", + " \n", + "database.create_tables([\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " ])\n", + "\n", + "database.close()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/.gitignore b/Student/Ruohan/lesson07/RDBMS-example-master/.gitignore new file mode 100755 index 0000000..2eeb9b2 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/.gitignore @@ -0,0 +1,102 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +__pycache__/ + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/DatabaseDiagram.jpeg b/Student/Ruohan/lesson07/RDBMS-example-master/DatabaseDiagram.jpeg new file mode 100755 index 0000000..c05d271 Binary files /dev/null and b/Student/Ruohan/lesson07/RDBMS-example-master/DatabaseDiagram.jpeg differ diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/LICENSE b/Student/Ruohan/lesson07/RDBMS-example-master/LICENSE new file mode 100755 index 0000000..94a9ed0 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/README.md b/Student/Ruohan/lesson07/RDBMS-example-master/README.md new file mode 100755 index 0000000..eb5b9eb --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/README.md @@ -0,0 +1,7 @@ + +[On GitLab now (no more updates here)](https://gitlab.com/uw-pyclass/RDBMS-example) + +# RDBMS-example +Peewee and sqlite for UW Python class + +Example Python code for RDBMS class diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class.zip b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class.zip new file mode 100644 index 0000000..98ecaf8 Binary files /dev/null and b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class.zip differ diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/create_personjob.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/create_personjob.py new file mode 100755 index 0000000..62c3a7d --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/create_personjob.py @@ -0,0 +1,24 @@ +""" + Create database examle with Peewee ORM, sqlite and Python + +""" + +from personjob_model import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +from peewee import * + +logger.info('One off program to build the classes from the model in the database') + +database.create_tables([ + Department, + Job, + Person, + PersonNumKey + ]) + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob.db b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob.db new file mode 100644 index 0000000..9f4087f Binary files /dev/null and b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob.db differ diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_activity.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_activity.py new file mode 100644 index 0000000..562446f --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_activity.py @@ -0,0 +1,149 @@ + +from personjob_modeli import * +import logging +from datetime import datetime + + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('Working with People class') +PERSON_NAME = 0 +LIVES_IN_TOWN = 1 +NICKNAME = 2 + +people = [ + ('Andrew', 'Sumner', 'Andy'), + ('Peter', 'Seattle', None), + ('Susan', 'Boston', 'Beannie'), + ('Pam', 'Coventry', 'PJ'), + ('Steven', 'Colchester', None), + ] + +logger.info('Creating Person records: iterate through the list of tuples') +logger.info('Prepare to explain any errors with exceptions') +logger.info('and the transaction tells the database to rollback on error') + +for person in people: + try: + with database.transaction(): + new_person = Person.create( + person_name = person[PERSON_NAME], + lives_in_town = person[LIVES_IN_TOWN], + nickname = person[NICKNAME]) + new_person.save() + logger.info('Database add successful') + + except Exception as e: + logger.info(f'Error creating = {person[PERSON_NAME]}') + logger.info(e) + + +logger.info('Read and print all Person records we created...') + +for person in Person: + logger.info(f'{person.person_name} lives in {person.lives_in_town} ' +\ + f'and likes to be known as {person.nickname}') + + +logger.info('Working with Job class') + +logger.info('Creating Job records: just like Person. We use the foreign key') + +JOB_NAME = 0 +START_DATE = 1 +END_DATE = 2 +SALARY = 3 +PERSON_EMPLOYED = 4 + +jobs = [ + ('Analyst', '2001-09-22', '2003-01-30',65500, 'Andrew'), + ('Senior analyst', '2003-02-01', '2006-10-22', 70000, 'Andrew'), + ('Senior business analyst', '2006-10-23', '2016-12-24', 80000, 'Andrew'), + ('Admin supervisor', '2012-10-01', '2014-11-10', 45900, 'Peter'), + ('Admin manager', '2014-11-14', '2018-01-05', 45900, 'Peter') + ] + + +for job in jobs: + try: + # compute the job duration in number of days + date_format = '%Y%m%d' + a = job[START_DATE].replace('-', '') + b = job[END_DATE].replace('-', '') + d0 = datetime.strptime(a, date_format) + d1 = datetime.strptime(b, date_format) + JD = (d1 - d0).days + logger.info(JD) + #save data to Job Table + with database.transaction(): + new_job = Job.create( + job_name = job[JOB_NAME], + start_date = job[START_DATE], + end_date = job[END_DATE], + salary = job[SALARY], + job_duration = JD, + person_employed = job[PERSON_EMPLOYED]) + new_job.save() + + except Exception as e: + logger.info(f'Error creating = {job[JOB_NAME]}') + logger.info(e) + +logger.info('Reading and print all Job rows (note the value of person)...') + +for job in Job: + logger.info(f'{job.job_name} : {job.start_date} to {job.end_date} for {job.job_duration} days for {job.person_employed}') + +logger.info('Working with Department class') + +logger.info('Creating Department records: just like Job. We use the foreign key') + +DEPARTMENT_NUMBER = 0 +DEPARTMENT_NAME = 1 +MANAGER = 2 +JOB_INCLUDED = 3 + +departments = [ + ('RD01', 'Research', 'Peter', 'Analyst'), + ('RD02', 'Research', 'Bill', 'Senior analyst'), + ('RD03', 'Research', 'Anna', 'Senior business analyst'), + ('HR01', 'Human Resource Management', 'Sam', 'Admin supervisor'), + ('HR02', 'Human Resource Management', 'Jessica', 'Admin manager') + ] + +for department in departments: + try: + with database.transaction(): + new_department = Department.create( + department_number = department[DEPARTMENT_NUMBER], + department_name = department[DEPARTMENT_NAME], + manager = department[MANAGER], + job_included = department[JOB_INCLUDED]) + new_department.save() + + except Exception as e: + logger.info(f'Error creating = {department[DEPARTMENT_NAME]}') + logger.info(e) + +logger.info('Reading and print all Department rows') + +for department in Department: + logger.info(f'{department.department_number} : ' + f'{department.department_name} manager is {department.manager} has ' + f'{department.job_included}') + +query = (Person + .select(Person, Job) + .join(Job) + .order_by(Person) + .prefetch(Job, Department) + ) + + +for person in query: + logger.info(f'Person {person.person_name} had job {person.job.job_name} for {person.job.job_duration} days') + for department in person.job.was_in: + logger.info(f" in {department.department_number}") + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v1_p1.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v1_p1.py new file mode 100755 index 0000000..1a118b2 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v1_p1.py @@ -0,0 +1,14 @@ +""" + Creates a file that looks like this: + + John,502-77-3056,2/27/1985,117.45 + +""" + +import csv + +peopledata = ['John', '502-77-3056', '2/27/1985', 117.45] + +with open('people.csv', 'w') as people: + peoplewriter = csv.writer(people) + peoplewriter.writerow(peopledata) diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v1_p2.csv b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v1_p2.csv new file mode 100755 index 0000000..cb857f3 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v1_p2.csv @@ -0,0 +1,2 @@ +'Name','SSN','BirthDate','Account Balance' +John,502-77-3056,2/27/1985,117.45 diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v1_p3.txt b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v1_p3.txt new file mode 100755 index 0000000..1059954 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v1_p3.txt @@ -0,0 +1,3 @@ +Field Name: 'Person Name', Type: String, Maximum Length: 30, Mandatory +Field Name: 'SSN', Type: String, Maximum Length: 11, Mandatory +etc \ No newline at end of file diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v3_p1.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v3_p1.py new file mode 100755 index 0000000..65213aa --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v3_p1.py @@ -0,0 +1,63 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + Person: + 1. insert records + 2. display all records + 3. show transactions + 4. show error checking + 5. show logging (to explain what's going on) + +""" + +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('Working with Person class') +logger.info('Note how I use constants and a list of tuples as a simple schema') +logger.info('Normally you probably will have prompted for this from a user') + +PERSON_NAME = 0 +LIVES_IN_TOWN = 1 +NICKNAME = 2 + +people = [ + ('Andrew', 'Sumner', 'Andy'), + ('Peter', 'Seattle', None), + ('Susan', 'Boston', 'Beannie'), + ('Pam', 'Coventry', 'PJ'), + ('Steven', 'Colchester', None), + ] + +logger.info('Creating Person records: iterate through the list of tuples') +logger.info('Prepare to explain any errors with exceptions') +logger.info('and the transaction tells the database to rollback on error') + +for person in people: + try: + with database.transaction(): + new_person = Person.create( + person_name = person[PERSON_NAME], + lives_in_town = person[LIVES_IN_TOWN], + nickname = person[NICKNAME]) + new_person.save() + logger.info('Database add successful') + + except Exception as e: + logger.info(f'Error creating = {person[PERSON_NAME]}') + logger.info(e) + logger.info('See how the database protects our data') + +logger.info('Read and print all Person records we created...') + +for person in Person: + logger.info(f'{person.person_name} lives in {person.lives_in_town} ' +\ + f'and likes to be known as {person.nickname}') + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v3_p2.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v3_p2.py new file mode 100755 index 0000000..15a6f24 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v3_p2.py @@ -0,0 +1,49 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + Person: + + 1. filter records and display + +""" +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('Working with Person class to search, find') + +PERSON_NAME = 0 +LIVES_IN_TOWN = 1 +NICKNAME = 2 + +logger.info('Find and display by selecting with one Person name...') + +aperson = Person.get(Person.person_name == 'Susan') +logger.info(f'{aperson.person_name} lives in {aperson.lives_in_town} ' + \ + f' and likes to be known as {aperson.nickname}') + +logger.info('Search and display Person with missing nicknames') +logger.info('Our person class inherits select(). Specify search with .where()') +logger.info('Peter gets a nickname but noone else') + +for person in Person.select().where(Person.nickname == None): + logger.info(f'{person.person_name} does not have a nickname; see: {person.nickname}') + if person.person_name == 'Peter': + logger.info('Changing nickname for Peter') + logger.info('Update the database') + person.nickname = 'Painter' + person.save() + else: + logger.info(f'Not giving a nickname to {person.person_name}') + +logger.info('And here is where we prove it by finding Peter and displaying') +aperson = Person.get(Person.person_name == 'Peter') +logger.info(f'{aperson.person_name} now has a nickname of {aperson.nickname}') + + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v3_p3.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v3_p3.py new file mode 100755 index 0000000..5ff874d --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v3_p3.py @@ -0,0 +1,45 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + Person: + + 1. add a new record and delete it + +""" +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +PERSON_NAME = 0 +LIVES_IN_TOWN = 1 +NICKNAME = 2 + +logger.info('Add and display a Person called Fred; then delete him...') + +logger.info('Add Fred in one step') + +new_person = Person.create( + person_name = 'Fred', + lives_in_town = 'Seattle', + nickname = 'Fearless') +new_person.save() + +logger.info('Show Fred') +aperson = Person.get(Person.person_name == 'Fred') + +logger.info(f'We just created {aperson.person_name}, who lives in {aperson.lives_in_town}') +logger.info('but now we will delete him...') + +aperson.delete_instance() + +logger.info('Reading and print all Person records (but not Fred; he has been deleted)...') + +for person in Person: + logger.info(f'{person.person_name} lives in {person.lives_in_town} and likes to be known as {person.nickname}') + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p1.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p1.py new file mode 100755 index 0000000..b9e6eaa --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p1.py @@ -0,0 +1,54 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + +""" + +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('Working with Job class') + +logger.info('Creating Job records: just like Person. We use the foreign key') + +JOB_NAME = 0 +START_DATE = 1 +END_DATE = 2 +SALARY = 3 +PERSON_EMPLOYED = 4 + +jobs = [ + ('Analyst', '2001-09-22', '2003-01-30',65500, 'Andrew'), + ('Senior analyst', '2003-02-01', '2006-10-22', 70000, 'Andrew'), + ('Senior business analyst', '2006-10-23', '2016-12-24', 80000, 'Andrew'), + ('Admin supervisor', '2012-10-01', '2014-11,10', 45900, 'Peter'), + ('Admin manager', '2014-11-14', '2018-01,05', 45900, 'Peter') + ] + +for job in jobs: + try: + with database.transaction(): + new_job = Job.create( + job_name = job[JOB_NAME], + start_date = job[START_DATE], + end_date = job[END_DATE], + salary = job[SALARY], + person_employed = job[PERSON_EMPLOYED]) + new_job.save() + + except Exception as e: + logger.info(f'Error creating = {job[JOB_NAME]}') + logger.info(e) + +logger.info('Reading and print all Job rows (note the value of person)...') + +for job in Job: + logger.info(f'{job.job_name} : {job.start_date} to {job.end_date} for {job.person_employed}') + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p2.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p2.py new file mode 100755 index 0000000..04d973c --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p2.py @@ -0,0 +1,40 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + +""" + +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('Working with Job class') + +JOB_NAME = 0 +START_DATE = 1 +END_DATE = 2 +SALARY = 3 +PERSON_EMPLOYED = 4 + +logger.info('Now resolve the join and print (INNER shows only jobs that match person)...') +logger.info('Notice how we use a query variable in this case') +logger.info('We select the classes we need, and we join Person to Job') +logger.info('Inner join (which is the default) shows only records that match') + +query = (Person + .select(Person, Job) + .join(Job, JOIN.INNER) + ) + +logger.info('View matching records from both tables') + +for person in query: + logger.info(f'Person {person.person_name} had job {person.job.job_name}') + + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p3.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p3.py new file mode 100755 index 0000000..4311bff --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p3.py @@ -0,0 +1,45 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + +""" + +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('View matching records and Persons without Jobs (note LEFT_OUTER)') + + +query = (Person + .select(Person, Job) + .join(Job, JOIN.LEFT_OUTER) + ) + +for person in query: + try: + logger.info(f'Person {person.person_name} had job {person.job.job_name}') + + except Exception as e: + logger.info(f'Person {person.person_name} had no job') + +logger.info('Example of how to summarize data') +logger.info('Note select() creates a count and names it job_count') +logger.info('group_by and order_by control level and sorting') + +query = (Person + .select(Person, fn.COUNT(Job.job_name).alias('job_count')) + .join(Job, JOIN.LEFT_OUTER) + .group_by(Person) + .order_by(Person.person_name)) + +for person in query: + logger.info(f'{person.person_name} had {person.job_count} jobs') + + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p4.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p4.py new file mode 100755 index 0000000..e8206ba --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p4.py @@ -0,0 +1,51 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + +""" + +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('Try to add a new job where a person doesnt exist...') + +addjob = ('Sales', '2010-04-01', '2018-02-08', 80400, 'Harry') + +logger.info('Adding a sales job for Harry') +logger.info(addjob) + +try: + with database.transaction(): + new_job = Job.create( + job_name = addjob[JOB_NAME], + start_date = addjob[START_DATE], + end_date = addjob[END_DATE], + salary = addjob[SALARY], + person_employed = addjob[PERSON_EMPLOYED]) + new_job.save() + +except Exception as e: + logger.info('Add failed because Harry is not in Person') + logger.info(f'For Job create: {addjob[0]}') + logger.info(e) + +logger.info('Try to Delete a person who has jobs...') + +try: + with database.transaction(): + aperson = Person.get(Person.person_name == 'Andrew') + logger.info(f'Trying to delete {aperson.person_name} who lives in {aperson.lives_in_town}') + aperson.delete_instance() + +except Exception as e: + logger.info('Delete failed because Andrew has Jobs') + logger.info(f'Delete failed: {aperson.person_name}') + logger.info(e) + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p5.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p5.py new file mode 100755 index 0000000..d1a2820 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p5.py @@ -0,0 +1,64 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + +""" + +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('Working with Job class') + +logger.info('Example of how to summarize data. Also shows join)') +logger.info('Note how we generate the count in the select()') +logger.info('group_by and order_by control the level of totals and sorting') + + +logger.info('Try to add a new job where a person doesnt exist...') + +JOB_NAME = 0 +START_DATE = 1 +END_DATE = 2 +SALARY = 3 +PERSON_EMPLOYED = 4 + +addjob = ('Sales', '2010-04-01', '2018-02-08', 80400, 'Harry') + +logger.info('We are trying to add:') +logger.info(addjob) + +try: + with database.transaction(): + new_job = Job.create( + job_name = addjob[JOB_NAME], + start_date = addjob[START_DATE], + end_date = addjob[END_DATE], + salary = addjob[SALARY], + person_employed = addjob[PERSON_EMPLOYED]) + new_job.save() + +except Exception as e: + logger.info('But we get an exception') + logger.info(f'For Job create: {addjob[0]}') + logger.info(e) + +logger.info('Try to Delete a person who has jobs...') + +try: + with database.transaction(): + aperson = Person.get(Person.person_name == 'Andrew') + logger.info(f'Trying to delete {aperson.person_name} who lives in {aperson.lives_in_town}') + aperson.delete_instance() + +except Exception as e: + logger.info(f'Delete failed: {aperson.person_name}') + logger.info(e) + + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p6.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p6.py new file mode 100755 index 0000000..e1f7bd5 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p6.py @@ -0,0 +1,86 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + +""" +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('Working with Person class') +logger.info('Note how I use constants and a list of tuples as a simple schema') +logger.info('Normally you probably will have prompted for this from a user') + +PERSON_NAME = 0 +LIVES_IN_TOWN = 1 +NICKNAME = 2 + +people = [ + ('Andrew', 'Sumner', 'Andy'), + ('Peter', 'Seattle', None), + ('Susan', 'Boston', 'Beannie'), + ('Pam', 'Coventry', 'PJ'), + ('Steven', 'Colchester', None), + ] + +logger.info('Try creating Person records again: it will fail') + +for person in people: + try: + with database.transaction(): + new_person = Person.create( + person_name = person[PERSON_NAME], + lives_in_town = person[LIVES_IN_TOWN], + nickname = person[NICKNAME]) + new_person.save() + logger.info('Database add successful') + + except Exception as e: + logger.info(f'Error creating = {person[PERSON_NAME]}') + logger.info(e) + +logger.info('We make sure duplicates are not unintentionally created this way') +logger.info('BUT: how do we really identify a Person (uniquely)?') + + +logger.info('Creating Person records, but in a new table with generated PK...') + +for person in people: + try: + with database.transaction(): + new_person = PersonNumKey.create( + person_name = person[PERSON_NAME], + lives_in_town = person[LIVES_IN_TOWN], + nickname = person[NICKNAME]) + new_person.save() + + except Exception as e: + logger.info(f'Error creating = {person[0]}') + logger.info(e) + +logger.info('Watch what happens when we do it again') + +for person in people: + try: + with database.transaction(): + new_person = PersonNumKey.create( + person_name = person[PERSON_NAME], + lives_in_town = person[LIVES_IN_TOWN], + nickname = person[NICKNAME]) + new_person.save() + + except Exception as e: + logger.info(f'Error creating = {person[0]}') + logger.info(e) + +logger.info('Note no PK specified, no PK violation; "duplicates" created!') + +for person in PersonNumKey.select(): + logger.info(f'Name : {person.person_name} with id: {person.id}') + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p7.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p7.py new file mode 100755 index 0000000..7007a58 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_learning_v5_p7.py @@ -0,0 +1,44 @@ +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) + + +""" +from personjob_modeli import * + +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info("Back to Person class: try to change Peter's name") + +aperson = Person.get(Person.person_name == 'Peter') +logger.info(f'Current value is {aperson.person_name}') + +logger.info('Update Peter to Peta, thereby trying to change the PK...') + +try: + with database.transaction(): + aperson = Person.get(Person.person_name == 'Peter') + aperson.person_name = 'Peta' + aperson.save() + logger.info(f'Tried to change Peter to {aperson.person_name}') + +except Exception as e: + logger.info(f'Cant change a PK and caught you trying') # not caught; no error thrown by Peewee + logger.info(e) + +aperson = Person.get(Person.person_name == 'Peter') +logger.info(f'Looked for Peter: found! -> {aperson.person_name}') + +try: + aperson = Person.get(Person.person_name == 'Peta') + +except Exception as e: + logger.info(f'Looking for Peta results in zero records. PK changes are ignored and do not throw an error!!!!') + logger.info(f'Cant change a PK') + logger.info('PK "change" can only be achieved with a delete and new create') + +database.close() diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_model.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_model.py new file mode 100755 index 0000000..76bf7f6 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_model.py @@ -0,0 +1,103 @@ +""" + Simple database example with Peewee ORM, sqlite and Python + Here we define the schema + Use logging for messages so they can be turned off + +""" +import logging +from peewee import * + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('Here we define our data (the schema)') +logger.info('First name and connect to a database (sqlite here)') + +logger.info('The next 3 lines of code are the only database specific code') + +database = SqliteDatabase('personjob.db') +database.connect() +database.execute_sql('PRAGMA foreign_keys = ON;') # needed for sqlite only + +# if you wanted to use heroku postgres: +# +# psycopg2 +# +# parse.uses_netloc.append("postgres") +# url = parse.urlparse(os.environ["DATABASE_URL"]) +# +# conn = psycopg2.connect( +# database=url.path[1:], +# user=url.username, +# password=url.password, +# host=url.hostname, +# port=url.port +# ) +# database = conn.cursor() +# +# Also consider elephantsql.com (be sure to use configparser for PWß) + +logger.info('This means we can easily switch to a different database') + +logger.info('Enable the Peewee magic! This base class does it all') + +class BaseModel(Model): + class Meta: + database = database + +logger.info('By inheritance only we keep our model (almost) technology neutral') + +class Person(BaseModel): + """ + This class defines Person, which maintains details of someone + for whom we want to research career to date. + """ + logger.info('Note how we defined the class') + + logger.info('Specify the fields in our model, their lengths and if mandatory') + logger.info('Must be a unique identifier for each person') + person_name = CharField(primary_key = True, max_length = 30) + lives_in_town = CharField(max_length = 40) + nickname = CharField(max_length = 20, null = True) + +class Job(BaseModel): + """ + This class defines Job, which maintains details of past Jobs + held by a Person. + """ + logger.info('Now the Job class with a simlar approach') + job_name = CharField(primary_key = True, max_length = 30) + logger.info('Dates') + start_date = DateField(formats = 'YYYY-MM-DD') + end_date = DateField(formats = 'YYYY-MM-DD') + logger.info('Number') + salary = DecimalField(max_digits = 7, decimal_places = 2) + logger.info('Which person had the Job') + person_employed = ForeignKeyField(Person, related_name='was_filled_by', null = False) + logger.info('Duration in days of job held in this department ') + job_duration = IntegerField() + +# acitivity for lesson07 +class Department(BaseModel): + """ + This class defines Department, which track in which Department a Person held a Job + """ + logger.info('Now create Department Class with similar approach') + department_number = CharField(primary_key = True, max_length = 4) + logger.info('Department Name') + department_name = CharField(max_length = 30) + logger.info('Department Manager Name') + manager = CharField(max_length = 30) + logger.info('What job included in this department') + job_included = ForeignKeyField(Job, related_name='was_in', null = False) + +class PersonNumKey(BaseModel): + """ + This class defines Person, which maintains details of someone + for whom we want to research career to date. + """ + logger.info('An alternate Person class') + logger.info("Note: no primary key so we're give one 'for free'") + person_name = CharField(max_length = 30) + lives_in_town = CharField(max_length = 40) + nickname = CharField(max_length = 20, null = True) diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_modeli.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_modeli.py new file mode 100755 index 0000000..767cf63 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/personjob_modeli.py @@ -0,0 +1,57 @@ +""" + Simple database examle with Peewee ORM, sqlite and Python + Here we define the schema + Use logging for messages so they can be turned off + +""" + +from peewee import * + +database = SqliteDatabase('personjob.db') +database.connect() +database.execute_sql('PRAGMA foreign_keys = ON;') + +class BaseModel(Model): + class Meta: + database = database + + +class Person(BaseModel): + """ + This class defines Person, which maintains details of someone + for whom we want to research career to date. + """ + person_name = CharField(primary_key = True, max_length = 30) + lives_in_town = CharField(max_length = 40) + nickname = CharField(max_length = 20, null = True) + +class Job(BaseModel): + """ + This class defines Job, which maintains details of past Jobs + held by a Person. + """ + job_name = CharField(primary_key = True, max_length = 30) + start_date = DateField(formats = 'YYYY-MM-DD') + end_date = DateField(formats = 'YYYY-MM-DD') + job_duration = IntegerField() + salary = DecimalField(max_digits = 7, decimal_places = 2) + person_employed = ForeignKeyField(Person, related_name='was_filled_by', null = False) + +# acitivity for lesson07 +class Department(BaseModel): + """ + This class defines Department, which track in which Department a Person held a Job + """ + department_number = CharField(primary_key = True, max_length = 4) + department_name = CharField(max_length = 30) + manager = CharField(max_length = 30) + job_included = ForeignKeyField(Job, related_name='was_in', null = False) + +class PersonNumKey(BaseModel): + """ + This class defines Person, which maintains details of someone + for whom we want to research career to date. + """ + person_name = CharField(max_length = 30) + lives_in_town = CharField(max_length = 40) + nickname = CharField(max_length = 20, null = True) diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/utilities.py b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/utilities.py new file mode 100755 index 0000000..d07765e --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/rdms-class/utilities.py @@ -0,0 +1,3 @@ +def printu(message): + print(f'---\n\n{message}') + print('=' * len(message)) diff --git a/Student/Ruohan/lesson07/RDBMS-example-master/su.sql b/Student/Ruohan/lesson07/RDBMS-example-master/su.sql new file mode 100755 index 0000000..b41a918 --- /dev/null +++ b/Student/Ruohan/lesson07/RDBMS-example-master/su.sql @@ -0,0 +1,5 @@ +PRAGMA foreign_keys = ON; +.mode column +.width 20 20 20 20 20 20 +.mode column +.headers on \ No newline at end of file diff --git a/Student/Ruohan/lesson07/lesson_seven_relational_databases (1).ipynb b/Student/Ruohan/lesson07/lesson_seven_relational_databases (1).ipynb new file mode 100644 index 0000000..bd67fa0 --- /dev/null +++ b/Student/Ruohan/lesson07/lesson_seven_relational_databases (1).ipynb @@ -0,0 +1,1523 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Preface: ORM or not?\n", + "\n", + "https://www.quora.com/What-are-the-pros-and-cons-of-using-raw-SQL-versus-ORM-for-database-development\n", + "\n", + "https://www.quora.com/Which-is-better-ORM-or-Plain-SQL\n", + "\n", + "http://mikehadlow.blogspot.com/2012/06/when-should-i-use-orm.html\n", + "\n", + "https://enterprisecraftsmanship.com/2015/11/30/do-you-need-an-orm/\n", + "\n", + "https://medium.com/@mithunsasidharan/should-i-or-should-i-not-use-orm-4c3742a639ce\n", + "\n", + "https://xkcd.com/1409/\n", + "\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
🐍
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "

Relational Databases

\n", + "
\n", + "

Introduction

\n", + "

In this lesson we are going to look at how to use a relational database with Python. Using relational databases is a massive subject in its own right, but we are going to concentrate on how to use these technologies simply and effectively.

\n", + "

What we learn here will be a foundation on which you can build as your database needs increase in volume and complexity.

\n", + "

Learning Objectives

\n", + "

Upon successful completion of this lesson, you will be able to:

\n", + "
    \n", + "
  • Apply data definition techniques to help assure the quality of the data your Python programs use.
  • \n", + "
  • Store and retrieve single and multiple sets of records in a database from your Python programs so that you can leverage data management services from a database.
  • \n", + "
  • Use simple techniques to help model data correctly in a relational database, and recognize the tradeoffs between different options for this. 
  • \n", + "
\n", + "

New Words, Concepts, and Tools

\n", + "
    \n", + "
  • We are going to learn about relational databases, data definition and management, and SQL. We will cover object relational mapping and relational database design, but all aligned to simplicity of use with Python. 
  • \n", + "
\n", + "

Required Reading

\n", + "\n", + "

Optional Reading

\n", + "
    \n", + "
  • How to interact with sqlite from Python (does not use Peewee)
  • \n", + "
  • How to interact with PostgreSQL from Python.
  • \n", + "
  • A great reference book for SQL that shows details of SQL for several databases is \"SQL in a Nutshell: A Desktop Quick Reference Guide\"
  • \n", + "
  • \n", + "

    If you really want to understand the details of SQL, then this is an excellent book: \"Joe Celko's SQL Programming Style (The Morgan Kaufmann Series in Data Management Systems)\"

    \n", + "
  • \n", + "
  • Data model design is a complex topic that requires lots of knowledge. If you do a lot of database work then the three books in the series \"The Data Model Resource Book\" (Vol. 1, 2 and 3) are invaluable (the links are the numbers).
  • \n", + "
\n", + "

\n", + "\n", + "\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "

Relational Databases

\n", + "
\n", + "

Here we are going to cover the background to why we use databases, and set the stage for later, when we will start to\n", + " develop a Python database application.

\n", + "

You will probably wish to clone the repository you can find on\n", + " GitHub. For this first video, the files are in the \"stuff\" directory. These are the files we refer to, and they\n", + " are included here, below the video:

\n", + "\n", + "

 

\n", + "

Video 1: Relational Databases with Python

\n", + "\n", + "\n", + "

simple_data_write.py

" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\"\"\"\n", + "write a csv file\n", + "data is \"anonymous\" (no schema)\n", + "\n", + "Creates a file that looks like this:\n", + "\n", + "John,502-77-3056,2/27/1985,117.45\n", + "\n", + "\"\"\"\n", + "\n", + "import csv\n", + "\n", + "peopledata = ['John', '502-77-3056', '2/27/1985', 117.45]\n", + "\n", + "with open('simple_data_write.csv', 'w') as people:\n", + " peoplewriter = csv.writer(people, quotechar=\"'\", quoting=csv.QUOTE_ALL)\n", + " peoplewriter.writerow(peopledata)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```\n", + "'John','502-77-3056','2/27/1985','117.45'\n", + "```\n", + "

\n", + "

simple_data_write_headers.py

" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\"\"\"\n", + "write a csv file\n", + "data is \"anonymous\" (no schema)\n", + "\n", + "Creates a file that looks like this:\n", + "\n", + "John,502-77-3056,2/27/1985,117.45\n", + "\n", + "\"\"\"\n", + "\n", + "import csv\n", + "\n", + "people_headers = ['Person Name','SSN','BirthDate','Account Balance']\n", + "people_data = [ \n", + " ['John', '502-77-3056', '2/27/1985', 117.45],\n", + " ['Jane', '756-01-5323', '12/01/1983', 120.9], \n", + "]\n", + "\n", + "with open('simple_data_write_headers.tsv', 'w') as people:\n", + " peoplewriter = csv.writer(people, delimiter='|', quoting=csv.QUOTE_NONNUMERIC)\n", + " peoplewriter.writerow(people_headers)\n", + " for person in people_data:\n", + " peoplewriter.writerow(person)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```\n", + "\"Person Name\"|\"SSN\"|\"BirthDate\"|\"Account Balance\"\n", + "\"John\"|\"502-77-3056\"|\"2/27/1985\"|\"117.45\"\n", + "\"Jane\"|\"756-01-5323\"|\"12/01/1983\"|\"120.9\"\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

 

\n", + "

We have covered the basis of data definition, and why it is important. We now know what a schema is and why it is important.\n", + " Now we can start to write a Python program that uses a database.

\n", + "

 

\n", + "

\n", + " Be sure you cloned the repository we mentioned prior to video 1 from \n", + " GitHub\n", + " . In this video we will be using the modules in the \"src\" directory We start with \n", + " v00_personjob_model.py\n", + "

\n", + "

Key fragments are included here too, below the video.

\n", + "\n", + "\n", + "

Video 2: Using the Model, Using the Person Class, Using the Job Class

\n", + "\n", + "

 Here is the model code:

\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:__main__:One off program to build the classes from the model in the database\n", + "INFO:__main__:Here we define our data (the schema)\n", + "INFO:__main__:First name and connect to a database (sqlite here)\n", + "INFO:__main__:The next 3 lines of code are the only database specific code\n", + "INFO:__main__:This means we can easily switch to a different database\n", + "INFO:__main__:Enable the Peewee magic! This base class does it all\n", + "INFO:__main__:By inheritance only we keep our model (almost) technology neutral\n", + "INFO:__main__:Note how we defined the class\n", + "INFO:__main__:Specify the fields in our model, their lengths and if mandatory\n", + "INFO:__main__:Must be a unique identifier for each person\n", + "INFO:__main__:Now the Job class with a simlar approach\n", + "INFO:__main__:Dates\n", + "INFO:__main__:Number\n", + "INFO:__main__:Which person had the Job\n", + "INFO:__main__:An alternate Person class\n", + "INFO:__main__:Note: no primary key so we're give one 'for free'\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\"\"\n", + " Simple database example with Peewee ORM, sqlite and Python\n", + " Here we define the schema\n", + " Use logging for messages so they can be turned off\n", + "\"\"\"\n", + "\n", + "import logging\n", + "from peewee import *\n", + "\n", + "logging.basicConfig(level=logging.INFO)\n", + "logger = logging.getLogger(__name__)\n", + "\n", + "logger.info('One off program to build the classes from the model in the database')\n", + "\n", + "logger.info('Here we define our data (the schema)')\n", + "logger.info('First name and connect to a database (sqlite here)')\n", + "\n", + "logger.info('The next 3 lines of code are the only database specific code')\n", + "\n", + "database = SqliteDatabase('personjob.db')\n", + "database.connect()\n", + "database.execute_sql('PRAGMA foreign_keys = ON;') # needed for sqlite only\n", + "\n", + "logger.info('This means we can easily switch to a different database')\n", + "logger.info('Enable the Peewee magic! This base class does it all')\n", + "logger.info('By inheritance only we keep our model (almost) technology neutral')\n", + "\n", + "class BaseModel(Model):\n", + " class Meta:\n", + " database = database\n", + "\n", + "class Person(BaseModel):\n", + " \"\"\"\n", + " This class defines Person, which maintains details of someone\n", + " for whom we want to research career to date.\n", + " \"\"\"\n", + "\n", + " logger.info('Note how we defined the class')\n", + "\n", + " logger.info('Specify the fields in our model, their lengths and if mandatory')\n", + " logger.info('Must be a unique identifier for each person')\n", + "\n", + " person_name = CharField(primary_key = True, max_length = 30)\n", + " lives_in_town = CharField(max_length = 40)\n", + " nickname = CharField(max_length = 20, null = True)\n", + "\n", + "class Job(BaseModel):\n", + " \"\"\"\n", + " This class defines Job, which maintains details of past Jobs\n", + " held by a Person.\n", + " \"\"\"\n", + "\n", + " logger.info('Now the Job class with a simlar approach')\n", + " job_name = CharField(primary_key = True, max_length = 30)\n", + " logger.info('Dates')\n", + " start_date = DateField(formats = 'YYYY-MM-DD')\n", + " end_date = DateField(formats = 'YYYY-MM-DD')\n", + " logger.info('Number')\n", + "\n", + " salary = DecimalField(max_digits = 7, decimal_places = 2)\n", + " logger.info('Which person had the Job')\n", + " person_employed = ForeignKeyField(Person, related_name='was_filled_by', null = False)\n", + "\n", + "class PersonNumKey(BaseModel):\n", + " \"\"\"\n", + " This class defines Person, which maintains details of someone\n", + " for whom we want to research career to date.\n", + " \"\"\"\n", + "\n", + " logger.info('An alternate Person class')\n", + " logger.info(\"Note: no primary key so we're give one 'for free'\")\n", + "\n", + " person_name = CharField(max_length = 30)\n", + " lives_in_town = CharField(max_length = 40)\n", + " nickname = CharField(max_length = 20, null = True)\n", + "\n", + "database.create_tables([\n", + " Job,\n", + " Person,\n", + " PersonNumKey\n", + " ])\n", + "\n", + "database.close()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "

 

\n", + "

Now we have looked at the model, lets look at how we create, read, and delete data from the database, using the Person class. Here\n", + " we use the following code:\n", + "  \n", + " v3_p1_populate_db.py, then \n", + " v3_p1_populate_db.py and finally \n", + " v3_p3_add_and_delete.py\n", + " .

\n", + "

Video 3: Using the Person class

\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p1.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "import logging\n", + "# from personjob_model import *\n", + "\n", + "def populate_db():\n", + " \"\"\"\n", + " add person data to database\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Working with Person class')\n", + " logger.info('Note how I use constants and a list of tuples as a simple schema')\n", + " logger.info('Normally you probably will have prompted for this from a user')\n", + "\n", + " PERSON_NAME = 0\n", + " LIVES_IN_TOWN = 1\n", + " NICKNAME = 2\n", + "\n", + " people = [\n", + " ('Andrew', 'Sumner', 'Andy'),\n", + " ('Peter', 'Seattle', None),\n", + " ('Susan', 'Boston', 'Beannie'),\n", + " ('Pam', 'Coventry', 'PJ'),\n", + " ('Steven', 'Colchester', None),\n", + " ]\n", + "\n", + " logger.info('Creating Person records: iterate through the list of tuples')\n", + " logger.info('Prepare to explain any errors with exceptions')\n", + " logger.info('and the transaction tells the database to fail on error')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " for person in people:\n", + " with database.transaction():\n", + " new_person = Person.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + " logger.info('Database add successful')\n", + "\n", + " logger.info('Print the Person records we saved...')\n", + " for saved_person in Person:\n", + " logger.info(f'{saved_person.person_name} lives in {saved_person.lives_in_town} ' +\\\n", + " f'and likes to be known as {saved_person.nickname}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[PERSON_NAME]}')\n", + " logger.info(e)\n", + " logger.info('See how the database protects our data')\n", + "\n", + " finally:\n", + " logger.info('database closes')\n", + " database.close()\n", + "\n", + "populate_db()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p2.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "# from personjob_model import *\n", + "\n", + "import logging\n", + "\n", + "def select_and_update():\n", + " \"\"\"\"\n", + " show how we can select a specific record, and then search and read through several records\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + "\n", + " logger.info('Find and display by selecting a spcific Person name...')\n", + " aperson = Person.get(Person.person_name == 'Susan')\n", + "\n", + " logger.info(f'{aperson.person_name} lives in {aperson.lives_in_town} ' + \\\n", + " f' and likes to be known as {aperson.nickname}')\n", + "\n", + " logger.info('Search and display all Person with missing nicknames')\n", + " logger.info('Our person class inherits select(). Specify search with .where()')\n", + " logger.info('Peter gets a nickname but noone else')\n", + "\n", + " for person in Person.select().where(Person.nickname == None):\n", + " logger.info(f'{person.person_name} does not have a nickname; see: {person.nickname}')\n", + " if person.person_name == 'Peter':\n", + " logger.info('Changing nickname for Peter')\n", + " logger.info('Update the database')\n", + " person.nickname = 'Painter'\n", + " person.save()\n", + " else:\n", + " logger.info(f'Not giving a nickname to {person.person_name}')\n", + "\n", + " logger.info('And here is where we prove it by finding Peter and displaying')\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " logger.info(f'{aperson.person_name} now has a nickname of {aperson.nickname}')\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "select_and_update()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p3.py\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "import logging\n", + "\n", + "# from personjob_model import *\n", + "\n", + "database = SqliteDatabase('personjob.db')\n", + "\n", + "def add_and_delete():\n", + " \"\"\"\"\n", + " show how we can add a record, and delete a record\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + "\n", + " logger.info('Add and display a Person called Fred; then delete him...')\n", + " logger.info('Add Fred in one step')\n", + "\n", + " new_person = Person.create(\n", + " person_name = 'Fred',\n", + " lives_in_town = 'Seattle',\n", + " nickname = 'Fearless')\n", + " new_person.save()\n", + "\n", + " logger.info('Show Fred')\n", + " aperson = Person.get(Person.person_name == 'Fred')\n", + "\n", + " logger.info(f'We just created {aperson.person_name}, who lives in {aperson.lives_in_town}')\n", + " logger.info('but now we will delete him...')\n", + "\n", + " aperson.delete_instance()\n", + "\n", + " logger.info('Reading and print all Person records (but not Fred; he has been deleted)...')\n", + "\n", + " for person in Person:\n", + " logger.info(f'{person.person_name} lives in {person.lives_in_town} and likes to be known as {person.nickname}')\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "add_and_delete()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "

 

\n", + "

Working with one class is not typical. Usually we will have several. We'll illustrate this by working with the Job class.\n", + " He we will use all the Python modules for the repository that start with v4:

\n", + "

Video 4: Using the Job class

\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://github.com/milesak60/RDBMS-example/blob/master/personjob_learning_v5_p1.py\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "# from personjob_model import *\n", + "\n", + "import logging\n", + "\n", + "def populate_db():\n", + " \"\"\"\n", + " add job data to database\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Working with Job class')\n", + " logger.info('Creating Job records: just like Person. We use the foreign key')\n", + "\n", + " JOB_NAME = 0\n", + " START_DATE = 1\n", + " END_DATE = 2\n", + " SALARY = 3\n", + " PERSON_EMPLOYED = 4\n", + "\n", + " jobs = [\n", + " ('Analyst', '2001-09-22', '2003-01-30',65500, 'Andrew'),\n", + " ('Senior analyst', '2003-02-01', '2006-10-22', 70000, 'Andrew'),\n", + " ('Senior business analyst', '2006-10-23', '2016-12-24', 80000, 'Andrew'),\n", + " ('Admin supervisor', '2012-10-01', '2014-11,10', 45900, 'Peter'),\n", + " ('Admin manager', '2014-11-14', '2018-01,05', 45900, 'Peter')\n", + " ]\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " for job in jobs:\n", + " with database.transaction():\n", + " new_job = Job.create(\n", + " job_name = job[JOB_NAME],\n", + " start_date = job[START_DATE],\n", + " end_date = job[END_DATE],\n", + " salary = job[SALARY],\n", + " person_employed = job[PERSON_EMPLOYED])\n", + " new_job.save()\n", + "\n", + " logger.info('Reading and print all Job rows (note the value of person)...')\n", + " for job in Job:\n", + " logger.info(f'{job.job_name} : {job.start_date} to {job.end_date} for {job.person_employed}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {job[JOB_NAME]}')\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " logger.info('database closes')\n", + " database.close()\n", + "\n", + "populate_db()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p2.py\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def join_classes():\n", + " \"\"\"\n", + " demonstrate how to join classes together : matches\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Working with Job class')\n", + "\n", + " logger.info('Now resolve the join and print (INNER shows only jobs that match person)...')\n", + " logger.info('Notice how we use a query variable in this case')\n", + " logger.info('We select the classes we need, and we join Person to Job')\n", + " logger.info('Inner join (which is the default) shows only records that match')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " query = (Person\n", + " .select(Person, Job)\n", + " .join(Job, JOIN.INNER)\n", + " )\n", + " \n", + " \"\"\"\n", + " select distinct p.*, j.* \n", + " from\n", + " person as p \n", + " inner join job as j\n", + " p.person_first_name = j.first_name and\n", + " p.person_last_name = j.last_name\n", + " limit 100 \n", + " \n", + " \"\"\"\n", + "\n", + " logger.info('View matching records from both tables')\n", + " for person in query:\n", + " logger.info(f'Person {person.person_name} had job {person.job.job_name}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {job[JOB_NAME]}')\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " logger.info('database closes')\n", + " database.close()\n", + "\n", + "join_classes()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p3.py\n", + "\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def join_classes():\n", + " \"\"\"\n", + " demonstrate how to join classes together : no matches too\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('View matching records and Persons without Jobs (note LEFT_OUTER)')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " query = (Person\n", + " .select(Person, Job)\n", + " .join(Job, JOIN.LEFT_OUTER)\n", + " )\n", + "\n", + " \n", + " \n", + " \n", + " for person in query:\n", + " try:\n", + " logger.info(f'Person {person.person_name} had job {person.job.job_name}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Person {person.person_name} had no job')\n", + "\n", + "\n", + " logger.info('Example of how to summarize data')\n", + " logger.info('Note select() creates a count and names it job_count')\n", + " logger.info('group_by and order_by control level and sorting')\n", + "\n", + " query = (Person\n", + " .select(Person, fn.COUNT(Job.job_name).alias('job_count'))\n", + " .join(Job, JOIN.LEFT_OUTER)\n", + " .group_by(Person)\n", + " .order_by(Person.person_name))\n", + "\n", + " \"\"\"\n", + " select p.person_first_name, p.person_last_name, count(j.job_name) as job_count \n", + " from\n", + " person as p \n", + " left outer join job as j\n", + " p.person_first_name = j.first_name and\n", + " p.person_last_name = j.last_name\n", + " group by p.person_first_name, p.person_last_name\n", + " \n", + " \n", + " \"\"\" \n", + " \n", + " \n", + " for person in query:\n", + " logger.info(f'{person.person_name} had {person.job_count} jobs')\n", + "\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "join_classes()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p4.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def show_integrity_add():\n", + " \"\"\"\n", + " demonstrate how database protects data inegrity : add\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " with database.transaction():\n", + " logger.info('Try to add a new job where a person doesnt exist...')\n", + "\n", + " addjob = ('Sales', '2010-04-01', '2018-02-08', 80400, 'Harry')\n", + "\n", + " logger.info('Adding a sales job for Harry')\n", + " logger.info(addjob)\n", + " new_job = Job.create(\n", + " job_name = addjob[JOB_NAME],\n", + " start_date = addjob[START_DATE],\n", + " end_date = addjob[END_DATE],\n", + " salary = addjob[SALARY],\n", + " person_employed = addjob[PERSON_EMPLOYED])\n", + " new_job.save()\n", + "\n", + " except Exception as e:\n", + " logger.info('Add failed because Harry is not in Person')\n", + " logger.info(f'For Job create: {addjob[0]}')\n", + " logger.info(e)\n", + "\n", + "show_integrity_add()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p5.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def show_integrity_del():\n", + " \"\"\"\n", + " demonstrate how database protects data inegrity : delete\n", + " \"\"\"\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " logger.info('Try to Delete a person who has jobs...')\n", + " with database.transaction():\n", + " aperson = Person.get(Person.person_name == 'Andrew')\n", + " logger.info(f'Trying to delete {aperson.person_name} who lives in {aperson.lives_in_town}')\n", + " aperson.delete_instance()\n", + " \n", + " \"\"\"delete from person where person_name = 'Andrew'\"\"\"\n", + "\n", + " except Exception as e:\n", + " logger.info('Delete failed because Andrew has Jobs')\n", + " logger.info(f'Delete failed: {aperson.person_name}')\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + " \n", + "show_integrity_del()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p6.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def add_with_without_BK():\n", + " \"\"\"\n", + " demonstrate impact of business keys\n", + " \"\"\"\n", + "\n", + " PERSON_NAME = 0\n", + " LIVES_IN_TOWN = 1\n", + " NICKNAME = 2\n", + " people = [\n", + " ('Andrew', 'Sumner', 'Andy'),\n", + " ('Peter', 'Seattle', None),\n", + " ('Susan', 'Boston', 'Beannie'),\n", + " ('Pam', 'Coventry', 'PJ'),\n", + " ('Steven', 'Colchester', None),\n", + " ]\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Try creating Person records again: it will fail')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " try:\n", + " with database.transaction():\n", + " for person in people:\n", + " new_person = Person.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + " logger.info('Database add successful')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[PERSON_NAME]}')\n", + " logger.info(e)\n", + "\n", + " logger.info('We make sure duplicates are not unintentionally created this way')\n", + " logger.info('BUT: how do we really identify a Person (uniquely)?')\n", + "\n", + " logger.info('Creating Person records, but in a new table with generated PK...')\n", + " try:\n", + " with database.transaction():\n", + " for person in people:\n", + " new_person = PersonNumKey.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[0]}')\n", + " logger.info(e)\n", + "\n", + " logger.info('Watch what happens when we do it again')\n", + "\n", + " try:\n", + " with database.transaction():\n", + " for person in people:\n", + " new_person = PersonNumKey.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[0]}')\n", + " logger.info(e)\n", + "\n", + " logger.info('Note no PK specified, no PK violation; \"duplicates\" created!')\n", + " for person in PersonNumKey.select():\n", + " logger.info(f'Name : {person.person_name} with id: {person.id}')\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "add_with_without_BK()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p7.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def cant_change_PK():\n", + " \"\"\"\n", + " show that PKs cant be changed (and that there is no error!)\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " logger.info(\"Back to Person class: try to change Peter's name\")\n", + "\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " logger.info(f'Current value is {aperson.person_name}')\n", + "\n", + " logger.info('Update Peter to Peta, thereby trying to change the PK...')\n", + "\n", + " try:\n", + " try:\n", + " with database.transaction():\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " aperson.person_name = 'Peta'\n", + " aperson.save()\n", + " logger.info(f'Tried to change Peter to {aperson.person_name}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Cant change a PK and caught you trying') # not caught; no error thrown by Peewee\n", + " logger.info(e)\n", + "\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " logger.info(f'Looked for Peter: found! -> {aperson.person_name}')\n", + "\n", + " try:\n", + " aperson = Person.get(Person.person_name == 'Peta')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Looking for Peta results in zero records. PK changes are ignored and do not throw an error!!!!')\n", + " logger.info(f'Cant change a PK')\n", + " logger.info('PK \"change\" can only be achieved with a delete and new create')\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "cant_change_PK()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "

 

\n", + "

Now we are going to learn about the best way to design the data in our database.  We will use the digram in the \"stuff\"\n", + " directory, which is also included below, along with the SQL code:

\n", + "

 

\n", + "

Video: Behind the scenes / Database Technologies

\n", + "\n", + "

 

\n", + "

Database diagram:

\n", + "

 \n", + "  

\n", + "

Code samples from the video:

\n", + "

SQL statement

\n", + "
\n", + "
select* from person;\n",
+    "
\n", + "
\n", + "

Start sqlite3 database (from the command line):

\n", + "
\n", + "
sqlite3 personjob.db\n",
+    "
\n", + "
\n", + "

The sqlite> prompt indicates we are ready to enter sqlite commands.

\n", + "
\n", + "
sqlite> .tables\n",
+    "job person personnumkey\n",
+    "
\n", + "
\n", + "

  Here is how sqlite sees the schema:

\n", + "
\n", + "
sqlite> .schema\n",
+    "\n",
+    "CREATE TABLE IF NOT EXISTS \"person\" (\"person_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));
\n", + "CREATE TABLE IF NOT EXISTS \"job\" (\"job_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"start_date\" DATE NOT NULL, \"end_date\" DATE NOT NULL, \"salary\" DECIMAL(7, 2) NOT NULL, \"person_employed_id\" VARCHAR(30) NOT NULL, FOREIGN KEY (\"person_employed_id\") REFERENCES \"person\" (\"person_name\"));
\n", + "CREATE INDEX \"job_person_employed_id\" ON \"job\" (\"person_employed_id\");
\n", + "CREATE TABLE IF NOT EXISTS \"personnumkey\" (\"id\" INTEGER NOT NULL PRIMARY KEY, \"person_name\" VARCHAR(30) NOT NULL, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));
\n", + "
\n", + "
\n", + "

 

\n", + "

 

\n", + "
\n", + "
sqlite> .mode column\n",
+    "sqlite> .width 15 15 15 15 15\n",
+    "sqlite> .headers on\n",
+    "
\n", + "
\n", + "

 

\n", + "
\n", + "
sqlite> select * from person;\n",
+    "sqlite> select * from job;\n",
+    "
\n", + "
\n", + "

Enter .quit to leave sqlite.

\n", + "

Lesson Summary

\n", + "

In this lesson we have learned about how we define, store and retrieve data in a relational database using Python, Peewee\n", + " and sqlite.

\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

 

\n", + "

Video: Conclusion

\n", + "

 \n", + " PeeWee documentation\n", + "

\n", + "

 

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "## Non-ORM Example" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CREATE TABLE IF NOT EXISTS \"person\" (\"person_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\n", + "CREATE TABLE IF NOT EXISTS \"job\" (\"job_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"start_date\" DATE NOT NULL, \"end_date\" DATE NOT NULL, \"salary\" DECIMAL(7, 2) NOT NULL, \"person_employed_id\" VARCHAR(30) NOT NULL, FOREIGN KEY (\"person_employed_id\") REFERENCES \"person\" (\"person_name\"));\n", + "CREATE INDEX \"job_person_employed_id\" ON \"job\" (\"person_employed_id\");\n", + "CREATE TABLE IF NOT EXISTS \"personnumkey\" (\"id\" INTEGER NOT NULL PRIMARY KEY, \"person_name\" VARCHAR(30) NOT NULL, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\n" + ] + } + ], + "source": [ + "import sqlite3\n", + "import os\n", + "\n", + "database_filename = \"personjob.db\"\n", + "\n", + "try:\n", + " # remove if file exists\n", + " os.remove(database_filename)\n", + "except:\n", + " # ignore if file doesn't exist\n", + " pass \n", + "\n", + "statements = [\n", + " \"\"\"CREATE TABLE IF NOT EXISTS \"person\" (\"person_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\"\"\",\n", + " \"\"\"CREATE TABLE IF NOT EXISTS \"job\" (\"job_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"start_date\" DATE NOT NULL, \"end_date\" DATE NOT NULL, \"salary\" DECIMAL(7, 2) NOT NULL, \"person_employed_id\" VARCHAR(30) NOT NULL, FOREIGN KEY (\"person_employed_id\") REFERENCES \"person\" (\"person_name\"));\"\"\",\n", + " \"\"\"CREATE INDEX \"job_person_employed_id\" ON \"job\" (\"person_employed_id\");\"\"\",\n", + " \"\"\"CREATE TABLE IF NOT EXISTS \"personnumkey\" (\"id\" INTEGER NOT NULL PRIMARY KEY, \"person_name\" VARCHAR(30) NOT NULL, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\"\"\",\n", + "]\n", + "\n", + "with sqlite3.connect(database_filename) as conn:\n", + " try: \n", + " for stmt in statements:\n", + " print(stmt)\n", + " conn.execute(stmt)\n", + " except Exception as e:\n", + " print(e)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "\n", + "
\n", + "

Activity

\n", + "
\n", + "

Warm up for the assignment

\n", + "

Before we launch into the assignment, let's be sure that you have everything you need to get started. We'll enhance the modules from the video along the way.

\n", + "

Preparing

\n", + "

Be sure to

\n", + "
\n", + "
pip install peewee\n",
+    "
\n", + "
\n", + "

 first.

\n", + "

Also, be sure that  

\n", + "
\n", + "
sqlite3 -version\n",
+    "
\n", + "
\n", + "

returns the sqlite3 version number, indicating it is installed. It should be, as it's bundled with Python 3.

\n", + "

Clone the repo at 

\n", + "
git@github.com:milesak60/RDBMS.git
\n", + "

although you should already have that from earlier in this lesson.

\n", + "

Make sure everything runs before proceeding to the next step.

\n", + "

Let's add a department

\n", + "

We have details of Persons. We have details of Jobs. Now we need to track in which Department a Person held a Job. For a Department, we need to know it's department number, which is 4 characters long and start with a letter. We need to know the department name (30 characters), and the name of the department manager (30 characters). We also need to know the duration in days that the job was held. Think about this last one carefully.

\n", + "

Make the necessary changes, annotating the code with log statements to explain what's going on. Also, draw a digram to help think through how you will incorporate Department into the programs.

\n", + "

Finally, produce a list using pretty print that shows all of the departments a person worked in for every job they ever had. 

\n", + "

Instructions

\n", + "

Once you've completed the activity from the lesson content, commit your changes and submit:

\n", + "
    \n", + "
  • a link to your repository on GitHub
  • \n", + "
  • the relevant .py file(s)
  • \n", + "
\n", + "

We'll be grading this activity purely on the percentage of included tests that pass.

\n", + "

Submitting Your Work 

\n", + "

Put the file(s) (ex: a_new_file.py) in your student directory in a new subdirectory named for this lesson, and add it to your clone early (git add a_new_file.py). Make frequent commits with good, clear messages about what you're doing and why.

\n", + "

When you're done and ready for the instructors to review your work, push your changes to your GitHub fork (git push) and then go to the GitHub website and make a pull request. Copy the link to the pull request.

\n", + "

Click the Submit Assignment button in the upper right.

\n", + "

Part 1: File(s)

\n", + "

Use the Choose File button to find and select the saved .py file or, if there are multiple files for the assignment, the .zip file.

\n", + "

Part 2: GitHub Link

\n", + "

Paste the GitHub link to the pull request in the comments area.

\n", + "

Click the Submit Assignment button.

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "\n", + "
\n", + "

Assignment

\n", + "
\n", + "

Instructions

\n", + "

Goal:

\n", + "

Redesign the object oriented mailroom program from class one using Peewee classes so that the data is persisted in sqlite. The approach you used in the OO exercise will naturally lend itself to this. (See Lesson 4, video 2)

\n", + "

Suggestions

\n", + "

You will need ways to add, update and delete data. Update and delete will mean that you need to find the data to update / delete. Perhaps you can do this by allowing a search first, and then selecting the particular instance to delete.

\n", + "

Remember that you need to read from the database, rather than relying on data held in variables when your program is running. To show you understand how this works, run the donor report from a separate program that read the database.

\n", + "

Generally, be sure to adapt the exception handling so that it helps identify any database errors, and consider how you may need to adapt your tests.

\n", + "

Be sure to give a lot of thought to what you should use as the primary key for your Peewee classes. In doing this, just consider data items that are unique in the context of the mailroom application. If you have to resort to generated keys, be sure to note why in the applicable docstring. And talking of which, be sure to define all your classes, as you learned in the videos.

\n", + "

The example programs for the videos will be a good starting point for reminders of syntax, but remember that the primary determinate of the structure of your solution will be a good object oriented Python application. The fact that it will now be persistent should not make too ,much difference. 

\n", + "

\n", + "

Submitting Your Work 

\n", + "

Put the file(s) (ex: a_new_file.py) in your student directory in a new subdirectory named for this lesson, and add it to your clone early (git add a_new_file.py). Make frequent commits with good, clear messages about what you're doing and why.

\n", + "

When you're done and ready for the instructors to review your work, push your changes to your GitHub fork (git push) and then go to the GitHub website and make a pull request. Copy the link to the pull request.

\n", + "

Click the Submit Assignment button in the upper right.

\n", + "

Part 1: File(s)

\n", + "

Use the Choose File button to find and select the saved .py file or, if there are multiple files for the assignment, the .zip file.

\n", + "

Part 2: GitHub Link

\n", + "

Paste the GitHub link to the pull request in the comments area.

\n", + "

Click the Submit Assignment button.

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
👹
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "\n", + "
\n", + "
\n", + "\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "\n", + "### Mailroom\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\"\"\"\n", + " Simple database example with Peewee ORM, sqlite and Python\n", + " Here we define the schema\n", + " Use logging for messages so they can be turned off\n", + "\"\"\"\n", + "\n", + "import logging\n", + "from peewee import *\n", + "\n", + "logging.basicConfig(level=logging.INFO)\n", + "logger = logging.getLogger(__name__)\n", + "\n", + "logger.info('One off program to build the classes from the model in the database')\n", + "\n", + "logger.info('Here we define our data (the schema)')\n", + "logger.info('First name and connect to a database (sqlite here)')\n", + "\n", + "logger.info('The next 3 lines of code are the only database specific code')\n", + "\n", + "database = SqliteDatabase('personjob.db')\n", + "database.connect()\n", + "database.execute_sql('PRAGMA foreign_keys = ON;') # needed for sqlite only\n", + "\n", + "logger.info('This means we can easily switch to a different database')\n", + "logger.info('Enable the Peewee magic! This base class does it all')\n", + "logger.info('By inheritance only we keep our model (almost) technology neutral')\n", + "\n", + "class BaseModel(Model):\n", + " class Meta:\n", + " database = database\n", + "\n", + "class Person(BaseModel):\n", + " \"\"\"\n", + " This class defines Person, which maintains details of someone\n", + " for whom we want to research career to date.\n", + " \"\"\"\n", + "\n", + " logger.info('Note how we defined the class')\n", + "\n", + " logger.info('Specify the fields in our model, their lengths and if mandatory')\n", + " logger.info('Must be a unique identifier for each person')\n", + "\n", + " first_name = CharField(max_length = 30)\n", + " last_name = CharField(max_length = 30)\n", + " nickname = CharField(max_length = 20, null = True)\n", + " \n", + " city = CharField(max_length = 40, null = True)\n", + " state = CharField(max_length = 40, null = True)\n", + " email = CharField(max_length = 40)\n", + " phone = CharField(max_length = 40)\n", + " \n", + " employer_id = IntegerField()\n", + " \n", + "\n", + "class Employer(BaseModel):\n", + " employer_name = CharField(max_length = 30)\n", + " city = CharField(max_length = 40)\n", + " state = CharField(max_length = 40) \n", + " website = CharField(max_length = 40)\n", + " contact_person = CharField(max_length = 150)\n", + " contact_phone = CharField(max_length = 20)\n", + " contact_email = CharField(max_length = 20)\n", + " employee_match = IntegerField(1)\n", + " employee_size = IntegerField()\n", + " \n", + "class EmployerInterests(BaseModel):\n", + " employer_id = IntegerField()\n", + " employer_interest_category = IntegerField()\n", + " employer_interest_details = CharField(max_length = 250)\n", + "\n", + " \n", + "\n", + "class Donor(BaseModel):\n", + " person_id = IntegerField()\n", + "\n", + "\n", + "class Donations(BaseModel):\n", + " person_id = IntegerField()\n", + " amount = DecimalField()\n", + " donation_date = DateTimeField()\n", + " \n", + "\"\"\"\n", + " select person_id, count(donations_id) as donation_frequency\n", + " from donations\n", + " group by person_id\n", + " \n", + "\"\"\"\n", + " \n", + " \n", + "class EmployerInterests(BaseModel):\n", + " employer_id = IntegerField()\n", + " employer_interest_category = IntegerField()\n", + " employer_interest_details = CharField(max_length = 250) \n", + "\n", + " \n", + " \n", + "database.create_tables([\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " ])\n", + "\n", + "database.close()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson07/lesson_seven_relational_databases.ipynb b/Student/Ruohan/lesson07/lesson_seven_relational_databases.ipynb new file mode 100644 index 0000000..d262c23 --- /dev/null +++ b/Student/Ruohan/lesson07/lesson_seven_relational_databases.ipynb @@ -0,0 +1,1293 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Preface: ORM or not?\n", + "\n", + "https://www.quora.com/What-are-the-pros-and-cons-of-using-raw-SQL-versus-ORM-for-database-development\n", + "\n", + "https://www.quora.com/Which-is-better-ORM-or-Plain-SQL\n", + "\n", + "http://mikehadlow.blogspot.com/2012/06/when-should-i-use-orm.html\n", + "\n", + "https://enterprisecraftsmanship.com/2015/11/30/do-you-need-an-orm/\n", + "\n", + "https://medium.com/@mithunsasidharan/should-i-or-should-i-not-use-orm-4c3742a639ce\n", + "\n", + "https://xkcd.com/1409/\n", + "\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
🐍
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "

Relational Databases

\n", + "
\n", + "

Introduction

\n", + "

In this lesson we are going to look at how to use a relational database with Python. Using relational databases is a massive subject in its own right, but we are going to concentrate on how to use these technologies simply and effectively.

\n", + "

What we learn here will be a foundation on which you can build as your database needs increase in volume and complexity.

\n", + "

Learning Objectives

\n", + "

Upon successful completion of this lesson, you will be able to:

\n", + "
    \n", + "
  • Apply data definition techniques to help assure the quality of the data your Python programs use.
  • \n", + "
  • Store and retrieve single and multiple sets of records in a database from your Python programs so that you can leverage data management services from a database.
  • \n", + "
  • Use simple techniques to help model data correctly in a relational database, and recognize the tradeoffs between different options for this. 
  • \n", + "
\n", + "

New Words, Concepts, and Tools

\n", + "
    \n", + "
  • We are going to learn about relational databases, data definition and management, and SQL. We will cover object relational mapping and relational database design, but all aligned to simplicity of use with Python. 
  • \n", + "
\n", + "

Required Reading

\n", + "\n", + "

Optional Reading

\n", + "
    \n", + "
  • How to interact with sqlite from Python (does not use Peewee)
  • \n", + "
  • How to interact with PostgreSQL from Python.
  • \n", + "
  • A great reference book for SQL that shows details of SQL for several databases is \"SQL in a Nutshell: A Desktop Quick Reference Guide\"
  • \n", + "
  • \n", + "

    If you really want to understand the details of SQL, then this is an excellent book: \"Joe Celko's SQL Programming Style (The Morgan Kaufmann Series in Data Management Systems)\"

    \n", + "
  • \n", + "
  • Data model design is a complex topic that requires lots of knowledge. If you do a lot of database work then the three books in the series \"The Data Model Resource Book\" (Vol. 1, 2 and 3) are invaluable (the links are the numbers).
  • \n", + "
\n", + "

\n", + "\n", + "\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "

Relational Databases

\n", + "
\n", + "

Here we are going to cover the background to why we use databases, and set the stage for later, when we will start to\n", + " develop a Python database application.

\n", + "

You will probably wish to clone the repository you can find on\n", + " GitHub. For this first video, the files are in the \"stuff\" directory. These are the files we refer to, and they\n", + " are included here, below the video:

\n", + "\n", + "

 

\n", + "

Video 1: Relational Databases with Python

\n", + "\n", + "\n", + "

simple_data_write.py

" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\"\"\"\n", + "write a csv file\n", + "data is \"anonymous\" (no schema)\n", + "\n", + "Creates a file that looks like this:\n", + "\n", + "John,502-77-3056,2/27/1985,117.45\n", + "\n", + "\"\"\"\n", + "\n", + "import csv\n", + "\n", + "peopledata = ['John', '502-77-3056', '2/27/1985', 117.45]\n", + "\n", + "with open('simple_data_write.csv', 'w') as people:\n", + " peoplewriter = csv.writer(people, quotechar=\"'\", quoting=csv.QUOTE_ALL)\n", + " peoplewriter.writerow(peopledata)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```\n", + "'John','502-77-3056','2/27/1985','117.45'\n", + "```\n", + "

\n", + "

simple_data_write_headers.py

" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\"\"\"\n", + "write a csv file\n", + "data is \"anonymous\" (no schema)\n", + "\n", + "Creates a file that looks like this:\n", + "\n", + "John,502-77-3056,2/27/1985,117.45\n", + "\n", + "\"\"\"\n", + "\n", + "import csv\n", + "\n", + "people_headers = ['Person Name','SSN','BirthDate','Account Balance']\n", + "people_data = [ \n", + " ['John', '502-77-3056', '2/27/1985', 117.45],\n", + " ['Jane', '756-01-5323', '12/01/1983', 120.9], \n", + "]\n", + "\n", + "with open('simple_data_write_headers.tsv', 'w') as people:\n", + " peoplewriter = csv.writer(people, delimiter='|', quoting=csv.QUOTE_NONNUMERIC)\n", + " peoplewriter.writerow(people_headers)\n", + " for person in people_data:\n", + " peoplewriter.writerow(person)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```\n", + "\"Person Name\"|\"SSN\"|\"BirthDate\"|\"Account Balance\"\n", + "\"John\"|\"502-77-3056\"|\"2/27/1985\"|\"117.45\"\n", + "\"Jane\"|\"756-01-5323\"|\"12/01/1983\"|\"120.9\"\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

 

\n", + "

We have covered the basis of data definition, and why it is important. We now know what a schema is and why it is important.\n", + " Now we can start to write a Python program that uses a database.

\n", + "

 

\n", + "

\n", + " Be sure you cloned the repository we mentioned prior to video 1 from \n", + " GitHub\n", + " . In this video we will be using the modules in the \"src\" directory We start with \n", + " v00_personjob_model.py\n", + "

\n", + "

Key fragments are included here too, below the video.

\n", + "\n", + "\n", + "

Video 2: Using the Model, Using the Person Class, Using the Job Class

\n", + "\n", + "

 Here is the model code:

\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + " Simple database example with Peewee ORM, sqlite and Python\n", + " Here we define the schema\n", + " Use logging for messages so they can be turned off\n", + "\"\"\"\n", + "\n", + "import logging\n", + "from peewee import *\n", + "\n", + "logging.basicConfig(level=logging.INFO)\n", + "logger = logging.getLogger(__name__)\n", + "\n", + "logger.info('One off program to build the classes from the model in the database')\n", + "\n", + "logger.info('Here we define our data (the schema)')\n", + "logger.info('First name and connect to a database (sqlite here)')\n", + "\n", + "logger.info('The next 3 lines of code are the only database specific code')\n", + "\n", + "database = SqliteDatabase('personjob.db')\n", + "database.connect()\n", + "database.execute_sql('PRAGMA foreign_keys = ON;') # needed for sqlite only\n", + "\n", + "logger.info('This means we can easily switch to a different database')\n", + "logger.info('Enable the Peewee magic! This base class does it all')\n", + "logger.info('By inheritance only we keep our model (almost) technology neutral')\n", + "\n", + "class BaseModel(Model):\n", + " class Meta:\n", + " database = database\n", + "\n", + "class Person(BaseModel):\n", + " \"\"\"\n", + " This class defines Person, which maintains details of someone\n", + " for whom we want to research career to date.\n", + " \"\"\"\n", + "\n", + " logger.info('Note how we defined the class')\n", + "\n", + " logger.info('Specify the fields in our model, their lengths and if mandatory')\n", + " logger.info('Must be a unique identifier for each person')\n", + "\n", + " person_name = CharField(primary_key = True, max_length = 30)\n", + " lives_in_town = CharField(max_length = 40)\n", + " nickname = CharField(max_length = 20, null = True)\n", + "\n", + "class Job(BaseModel):\n", + " \"\"\"\n", + " This class defines Job, which maintains details of past Jobs\n", + " held by a Person.\n", + " \"\"\"\n", + "\n", + " logger.info('Now the Job class with a simlar approach')\n", + " job_name = CharField(primary_key = True, max_length = 30)\n", + " logger.info('Dates')\n", + " start_date = DateField(formats = 'YYYY-MM-DD')\n", + " end_date = DateField(formats = 'YYYY-MM-DD')\n", + " logger.info('Number')\n", + "\n", + " salary = DecimalField(max_digits = 7, decimal_places = 2)\n", + " logger.info('Which person had the Job')\n", + " person_employed = ForeignKeyField(Person, related_name='was_filled_by', null = False)\n", + "\n", + "class PersonNumKey(BaseModel):\n", + " \"\"\"\n", + " This class defines Person, which maintains details of someone\n", + " for whom we want to research career to date.\n", + " \"\"\"\n", + "\n", + " logger.info('An alternate Person class')\n", + " logger.info(\"Note: no primary key so we're give one 'for free'\")\n", + "\n", + " person_name = CharField(max_length = 30)\n", + " lives_in_town = CharField(max_length = 40)\n", + " nickname = CharField(max_length = 20, null = True)\n", + "\n", + "database.create_tables([\n", + " Job,\n", + " Person,\n", + " PersonNumKey\n", + " ])\n", + "\n", + "database.close()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "

 

\n", + "

Now we have looked at the model, lets look at how we create, read, and delete data from the database, using the Person class. Here\n", + " we use the following code:\n", + "  \n", + " v3_p1_populate_db.py, then \n", + " v3_p1_populate_db.py and finally \n", + " v3_p3_add_and_delete.py\n", + " .

\n", + "

Video 3: Using the Person class

\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p1.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "import logging\n", + "# from personjob_model import *\n", + "\n", + "def populate_db():\n", + " \"\"\"\n", + " add person data to database\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Working with Person class')\n", + " logger.info('Note how I use constants and a list of tuples as a simple schema')\n", + " logger.info('Normally you probably will have prompted for this from a user')\n", + "\n", + " PERSON_NAME = 0\n", + " LIVES_IN_TOWN = 1\n", + " NICKNAME = 2\n", + "\n", + " people = [\n", + " ('Andrew', 'Sumner', 'Andy'),\n", + " ('Peter', 'Seattle', None),\n", + " ('Susan', 'Boston', 'Beannie'),\n", + " ('Pam', 'Coventry', 'PJ'),\n", + " ('Steven', 'Colchester', None),\n", + " ]\n", + "\n", + " logger.info('Creating Person records: iterate through the list of tuples')\n", + " logger.info('Prepare to explain any errors with exceptions')\n", + " logger.info('and the transaction tells the database to fail on error')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " for person in people:\n", + " with database.transaction():\n", + " new_person = Person.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + " logger.info('Database add successful')\n", + "\n", + " logger.info('Print the Person records we saved...')\n", + " for saved_person in Person:\n", + " logger.info(f'{saved_person.person_name} lives in {saved_person.lives_in_town} ' +\\\n", + " f'and likes to be known as {saved_person.nickname}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[PERSON_NAME]}')\n", + " logger.info(e)\n", + " logger.info('See how the database protects our data')\n", + "\n", + " finally:\n", + " logger.info('database closes')\n", + " database.close()\n", + "\n", + "populate_db()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p2.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "# from personjob_model import *\n", + "\n", + "import logging\n", + "\n", + "def select_and_update():\n", + " \"\"\"\"\n", + " show how we can select a specific record, and then search and read through several records\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + "\n", + " logger.info('Find and display by selecting a spcific Person name...')\n", + " aperson = Person.get(Person.person_name == 'Susan')\n", + "\n", + " logger.info(f'{aperson.person_name} lives in {aperson.lives_in_town} ' + \\\n", + " f' and likes to be known as {aperson.nickname}')\n", + "\n", + " logger.info('Search and display all Person with missing nicknames')\n", + " logger.info('Our person class inherits select(). Specify search with .where()')\n", + " logger.info('Peter gets a nickname but noone else')\n", + "\n", + " for person in Person.select().where(Person.nickname == None):\n", + " logger.info(f'{person.person_name} does not have a nickname; see: {person.nickname}')\n", + " if person.person_name == 'Peter':\n", + " logger.info('Changing nickname for Peter')\n", + " logger.info('Update the database')\n", + " person.nickname = 'Painter'\n", + " person.save()\n", + " else:\n", + " logger.info(f'Not giving a nickname to {person.person_name}')\n", + "\n", + " logger.info('And here is where we prove it by finding Peter and displaying')\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " logger.info(f'{aperson.person_name} now has a nickname of {aperson.nickname}')\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "select_and_update()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p3.py\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "import logging\n", + "\n", + "# from personjob_model import *\n", + "\n", + "database = SqliteDatabase('personjob.db')\n", + "\n", + "def add_and_delete():\n", + " \"\"\"\"\n", + " show how we can add a record, and delete a record\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + "\n", + " logger.info('Add and display a Person called Fred; then delete him...')\n", + " logger.info('Add Fred in one step')\n", + "\n", + " new_person = Person.create(\n", + " person_name = 'Fred',\n", + " lives_in_town = 'Seattle',\n", + " nickname = 'Fearless')\n", + " new_person.save()\n", + "\n", + " logger.info('Show Fred')\n", + " aperson = Person.get(Person.person_name == 'Fred')\n", + "\n", + " logger.info(f'We just created {aperson.person_name}, who lives in {aperson.lives_in_town}')\n", + " logger.info('but now we will delete him...')\n", + "\n", + " aperson.delete_instance()\n", + "\n", + " logger.info('Reading and print all Person records (but not Fred; he has been deleted)...')\n", + "\n", + " for person in Person:\n", + " logger.info(f'{person.person_name} lives in {person.lives_in_town} and likes to be known as {person.nickname}')\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "add_and_delete()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "

 

\n", + "

Working with one class is not typical. Usually we will have several. We'll illustrate this by working with the Job class.\n", + " He we will use all the Python modules for the repository that start with v4:

\n", + "

Video 4: Using the Job class

\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://github.com/milesak60/RDBMS-example/blob/master/personjob_learning_v5_p1.py\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "# from personjob_model import *\n", + "\n", + "import logging\n", + "\n", + "def populate_db():\n", + " \"\"\"\n", + " add job data to database\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Working with Job class')\n", + " logger.info('Creating Job records: just like Person. We use the foreign key')\n", + "\n", + " JOB_NAME = 0\n", + " START_DATE = 1\n", + " END_DATE = 2\n", + " SALARY = 3\n", + " PERSON_EMPLOYED = 4\n", + "\n", + " jobs = [\n", + " ('Analyst', '2001-09-22', '2003-01-30',65500, 'Andrew'),\n", + " ('Senior analyst', '2003-02-01', '2006-10-22', 70000, 'Andrew'),\n", + " ('Senior business analyst', '2006-10-23', '2016-12-24', 80000, 'Andrew'),\n", + " ('Admin supervisor', '2012-10-01', '2014-11,10', 45900, 'Peter'),\n", + " ('Admin manager', '2014-11-14', '2018-01,05', 45900, 'Peter')\n", + " ]\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " for job in jobs:\n", + " with database.transaction():\n", + " new_job = Job.create(\n", + " job_name = job[JOB_NAME],\n", + " start_date = job[START_DATE],\n", + " end_date = job[END_DATE],\n", + " salary = job[SALARY],\n", + " person_employed = job[PERSON_EMPLOYED])\n", + " new_job.save()\n", + "\n", + " logger.info('Reading and print all Job rows (note the value of person)...')\n", + " for job in Job:\n", + " logger.info(f'{job.job_name} : {job.start_date} to {job.end_date} for {job.person_employed}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {job[JOB_NAME]}')\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " logger.info('database closes')\n", + " database.close()\n", + "\n", + "populate_db()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p2.py\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def join_classes():\n", + " \"\"\"\n", + " demonstrate how to join classes together : matches\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Working with Job class')\n", + "\n", + " logger.info('Now resolve the join and print (INNER shows only jobs that match person)...')\n", + " logger.info('Notice how we use a query variable in this case')\n", + " logger.info('We select the classes we need, and we join Person to Job')\n", + " logger.info('Inner join (which is the default) shows only records that match')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " query = (Person\n", + " .select(Person, Job)\n", + " .join(Job, JOIN.INNER)\n", + " )\n", + "\n", + " logger.info('View matching records from both tables')\n", + " for person in query:\n", + " logger.info(f'Person {person.person_name} had job {person.job.job_name}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {job[JOB_NAME]}')\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " logger.info('database closes')\n", + " database.close()\n", + "\n", + "join_classes()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p3.py\n", + "\n", + "\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def join_classes():\n", + " \"\"\"\n", + " demonstrate how to join classes together : no matches too\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('View matching records and Persons without Jobs (note LEFT_OUTER)')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " query = (Person\n", + " .select(Person, Job)\n", + " .join(Job, JOIN.LEFT_OUTER)\n", + " )\n", + "\n", + " for person in query:\n", + " try:\n", + " logger.info(f'Person {person.person_name} had job {person.job.job_name}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Person {person.person_name} had no job')\n", + "\n", + "\n", + " logger.info('Example of how to summarize data')\n", + " logger.info('Note select() creates a count and names it job_count')\n", + " logger.info('group_by and order_by control level and sorting')\n", + "\n", + " query = (Person\n", + " .select(Person, fn.COUNT(Job.job_name).alias('job_count'))\n", + " .join(Job, JOIN.LEFT_OUTER)\n", + " .group_by(Person)\n", + " .order_by(Person.person_name))\n", + "\n", + " for person in query:\n", + " logger.info(f'{person.person_name} had {person.job_count} jobs')\n", + "\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "join_classes()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p4.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def show_integrity_add():\n", + " \"\"\"\n", + " demonstrate how database protects data inegrity : add\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " with database.transaction():\n", + " logger.info('Try to add a new job where a person doesnt exist...')\n", + "\n", + " addjob = ('Sales', '2010-04-01', '2018-02-08', 80400, 'Harry')\n", + "\n", + " logger.info('Adding a sales job for Harry')\n", + " logger.info(addjob)\n", + " new_job = Job.create(\n", + " job_name = addjob[JOB_NAME],\n", + " start_date = addjob[START_DATE],\n", + " end_date = addjob[END_DATE],\n", + " salary = addjob[SALARY],\n", + " person_employed = addjob[PERSON_EMPLOYED])\n", + " new_job.save()\n", + "\n", + " except Exception as e:\n", + " logger.info('Add failed because Harry is not in Person')\n", + " logger.info(f'For Job create: {addjob[0]}')\n", + " logger.info(e)\n", + "\n", + "show_integrity_add()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p5.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def show_integrity_del():\n", + " \"\"\"\n", + " demonstrate how database protects data inegrity : delete\n", + " \"\"\"\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " logger.info('Try to Delete a person who has jobs...')\n", + " with database.transaction():\n", + " aperson = Person.get(Person.person_name == 'Andrew')\n", + " logger.info(f'Trying to delete {aperson.person_name} who lives in {aperson.lives_in_town}')\n", + " aperson.delete_instance()\n", + "\n", + " except Exception as e:\n", + " logger.info('Delete failed because Andrew has Jobs')\n", + " logger.info(f'Delete failed: {aperson.person_name}')\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + " \n", + "show_integrity_del()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p6.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def add_with_without_BK():\n", + " \"\"\"\n", + " demonstrate impact of business keys\n", + " \"\"\"\n", + "\n", + " PERSON_NAME = 0\n", + " LIVES_IN_TOWN = 1\n", + " NICKNAME = 2\n", + " people = [\n", + " ('Andrew', 'Sumner', 'Andy'),\n", + " ('Peter', 'Seattle', None),\n", + " ('Susan', 'Boston', 'Beannie'),\n", + " ('Pam', 'Coventry', 'PJ'),\n", + " ('Steven', 'Colchester', None),\n", + " ]\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " database = SqliteDatabase('personjob.db')\n", + "\n", + " logger.info('Try creating Person records again: it will fail')\n", + "\n", + " try:\n", + " database.connect()\n", + " database.execute_sql('PRAGMA foreign_keys = ON;')\n", + " try:\n", + " with database.transaction():\n", + " for person in people:\n", + " new_person = Person.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + " logger.info('Database add successful')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[PERSON_NAME]}')\n", + " logger.info(e)\n", + "\n", + " logger.info('We make sure duplicates are not unintentionally created this way')\n", + " logger.info('BUT: how do we really identify a Person (uniquely)?')\n", + "\n", + " logger.info('Creating Person records, but in a new table with generated PK...')\n", + " try:\n", + " with database.transaction():\n", + " for person in people:\n", + " new_person = PersonNumKey.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[0]}')\n", + " logger.info(e)\n", + "\n", + " logger.info('Watch what happens when we do it again')\n", + "\n", + " try:\n", + " with database.transaction():\n", + " for person in people:\n", + " new_person = PersonNumKey.create(\n", + " person_name = person[PERSON_NAME],\n", + " lives_in_town = person[LIVES_IN_TOWN],\n", + " nickname = person[NICKNAME])\n", + " new_person.save()\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Error creating = {person[0]}')\n", + " logger.info(e)\n", + "\n", + " logger.info('Note no PK specified, no PK violation; \"duplicates\" created!')\n", + " for person in PersonNumKey.select():\n", + " logger.info(f'Name : {person.person_name} with id: {person.id}')\n", + "\n", + " except Exception as e:\n", + " logger.info(e)\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "add_with_without_BK()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p7.py\n", + "\"\"\"\n", + " Learning persistence with Peewee and sqlite\n", + " delete the database to start over\n", + " (but running this program does not require it)\n", + "\"\"\"\n", + "\n", + "def cant_change_PK():\n", + " \"\"\"\n", + " show that PKs cant be changed (and that there is no error!)\n", + " \"\"\"\n", + "\n", + " logging.basicConfig(level=logging.INFO)\n", + " logger = logging.getLogger(__name__)\n", + "\n", + " logger.info(\"Back to Person class: try to change Peter's name\")\n", + "\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " logger.info(f'Current value is {aperson.person_name}')\n", + "\n", + " logger.info('Update Peter to Peta, thereby trying to change the PK...')\n", + "\n", + " try:\n", + " try:\n", + " with database.transaction():\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " aperson.person_name = 'Peta'\n", + " aperson.save()\n", + " logger.info(f'Tried to change Peter to {aperson.person_name}')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Cant change a PK and caught you trying') # not caught; no error thrown by Peewee\n", + " logger.info(e)\n", + "\n", + " aperson = Person.get(Person.person_name == 'Peter')\n", + " logger.info(f'Looked for Peter: found! -> {aperson.person_name}')\n", + "\n", + " try:\n", + " aperson = Person.get(Person.person_name == 'Peta')\n", + "\n", + " except Exception as e:\n", + " logger.info(f'Looking for Peta results in zero records. PK changes are ignored and do not throw an error!!!!')\n", + " logger.info(f'Cant change a PK')\n", + " logger.info('PK \"change\" can only be achieved with a delete and new create')\n", + "\n", + " finally:\n", + " database.close()\n", + "\n", + "cant_change_PK()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "

 

\n", + "

Now we are going to learn about the best way to design the data in our database.  We will use the digram in the \"stuff\"\n", + " directory, which is also included below, along with the SQL code:

\n", + "

 

\n", + "

Video: Behind the scenes / Database Technologies

\n", + "\n", + "

 

\n", + "

Database diagram:

\n", + "

 \n", + "  

\n", + "

Code samples from the video:

\n", + "

SQL statement

\n", + "
\n", + "
select* from person;\n",
+    "
\n", + "
\n", + "

Start sqlite3 database (from the command line):

\n", + "
\n", + "
sqlite3 personjob.db\n",
+    "
\n", + "
\n", + "

The sqlite> prompt indicates we are ready to enter sqlite commands.

\n", + "
\n", + "
sqlite> .tables\n",
+    "job person personnumkey\n",
+    "
\n", + "
\n", + "

  Here is how sqlite sees the schema:

\n", + "
\n", + "
sqlite> .schema\n",
+    "\n",
+    "CREATE TABLE IF NOT EXISTS \"person\" (\"person_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));
\n", + "CREATE TABLE IF NOT EXISTS \"job\" (\"job_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"start_date\" DATE NOT NULL, \"end_date\" DATE NOT NULL, \"salary\" DECIMAL(7, 2) NOT NULL, \"person_employed_id\" VARCHAR(30) NOT NULL, FOREIGN KEY (\"person_employed_id\") REFERENCES \"person\" (\"person_name\"));
\n", + "CREATE INDEX \"job_person_employed_id\" ON \"job\" (\"person_employed_id\");
\n", + "CREATE TABLE IF NOT EXISTS \"personnumkey\" (\"id\" INTEGER NOT NULL PRIMARY KEY, \"person_name\" VARCHAR(30) NOT NULL, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));
\n", + "
\n", + "
\n", + "

 

\n", + "

 

\n", + "
\n", + "
sqlite> .mode column\n",
+    "sqlite> .width 15 15 15 15 15\n",
+    "sqlite> .headers on\n",
+    "
\n", + "
\n", + "

 

\n", + "
\n", + "
sqlite> select * from person;\n",
+    "sqlite> select * from job;\n",
+    "
\n", + "
\n", + "

Enter .quit to leave sqlite.

\n", + "

Lesson Summary

\n", + "

In this lesson we have learned about how we define, store and retrieve data in a relational database using Python, Peewee\n", + " and sqlite.

\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

 

\n", + "

Video: Conclusion

\n", + "

 \n", + " PeeWee documentation\n", + "

\n", + "

 

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "## Non-ORM Example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import sqlite3\n", + "import os\n", + "\n", + "database_filename = \"personjob.db\"\n", + "\n", + "try:\n", + " # remove if file exists\n", + " os.remove(database_filename)\n", + "except:\n", + " # ignore if file doesn't exist\n", + " pass \n", + "\n", + "statements = [\n", + " \"\"\"CREATE TABLE IF NOT EXISTS \"person\" (\"person_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\"\"\",\n", + " \"\"\"CREATE TABLE IF NOT EXISTS \"job\" (\"job_name\" VARCHAR(30) NOT NULL PRIMARY KEY, \"start_date\" DATE NOT NULL, \"end_date\" DATE NOT NULL, \"salary\" DECIMAL(7, 2) NOT NULL, \"person_employed_id\" VARCHAR(30) NOT NULL, FOREIGN KEY (\"person_employed_id\") REFERENCES \"person\" (\"person_name\"));\"\"\",\n", + " \"\"\"CREATE INDEX \"job_person_employed_id\" ON \"job\" (\"person_employed_id\");\"\"\",\n", + " \"\"\"CREATE TABLE IF NOT EXISTS \"personnumkey\" (\"id\" INTEGER NOT NULL PRIMARY KEY, \"person_name\" VARCHAR(30) NOT NULL, \"lives_in_town\" VARCHAR(40) NOT NULL, \"nickname\" VARCHAR(20));\"\"\",\n", + "]\n", + "\n", + "with sqlite3.connect(database_filename) as conn:\n", + " try: \n", + " for stmt in statements:\n", + " print(stmt)\n", + " conn.execute(stmt)\n", + " except Exception as e:\n", + " print(e)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "\n", + "
\n", + "

Activity

\n", + "
\n", + "

Warm up for the assignment

\n", + "

Before we launch into the assignment, let's be sure that you have everything you need to get started. We'll enhance the modules from the video along the way.

\n", + "

Preparing

\n", + "

Be sure to

\n", + "
\n", + "
pip install peewee\n",
+    "
\n", + "
\n", + "

 first.

\n", + "

Also, be sure that  

\n", + "
\n", + "
sqlite3 -version\n",
+    "
\n", + "
\n", + "

returns the sqlite3 version number, indicating it is installed. It should be, as it's bundled with Python 3.

\n", + "

Clone the repo at 

\n", + "
git@github.com:milesak60/RDBMS.git
\n", + "

although you should already have that from earlier in this lesson.

\n", + "

Make sure everything runs before proceeding to the next step.

\n", + "

Let's add a department

\n", + "

We have details of Persons. We have details of Jobs. Now we need to track in which Department a Person held a Job. For a Department, we need to know it's department number, which is 4 characters long and start with a letter. We need to know the department name (30 characters), and the name of the department manager (30 characters). We also need to know the duration in days that the job was held. Think about this last one carefully.

\n", + "

Make the necessary changes, annotating the code with log statements to explain what's going on. Also, draw a digram to help think through how you will incorporate Department into the programs.

\n", + "

Finally, produce a list using pretty print that shows all of the departments a person worked in for every job they ever had. 

\n", + "

Instructions

\n", + "

Once you've completed the activity from the lesson content, commit your changes and submit:

\n", + "
    \n", + "
  • a link to your repository on GitHub
  • \n", + "
  • the relevant .py file(s)
  • \n", + "
\n", + "

We'll be grading this activity purely on the percentage of included tests that pass.

\n", + "

Submitting Your Work 

\n", + "

Put the file(s) (ex: a_new_file.py) in your student directory in a new subdirectory named for this lesson, and add it to your clone early (git add a_new_file.py). Make frequent commits with good, clear messages about what you're doing and why.

\n", + "

When you're done and ready for the instructors to review your work, push your changes to your GitHub fork (git push) and then go to the GitHub website and make a pull request. Copy the link to the pull request.

\n", + "

Click the Submit Assignment button in the upper right.

\n", + "

Part 1: File(s)

\n", + "

Use the Choose File button to find and select the saved .py file or, if there are multiple files for the assignment, the .zip file.

\n", + "

Part 2: GitHub Link

\n", + "

Paste the GitHub link to the pull request in the comments area.

\n", + "

Click the Submit Assignment button.

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "
\n", + "\n", + "\n", + "
\n", + "

Assignment

\n", + "
\n", + "

Instructions

\n", + "

Goal:

\n", + "

Redesign the object oriented mailroom program from class one using Peewee classes so that the data is persisted in sqlite. The approach you used in the OO exercise will naturally lend itself to this. (See Lesson 4, video 2)

\n", + "

Suggestions

\n", + "

You will need ways to add, update and delete data. Update and delete will mean that you need to find the data to update / delete. Perhaps you can do this by allowing a search first, and then selecting the particular instance to delete.

\n", + "

Remember that you need to read from the database, rather than relying on data held in variables when your program is running. To show you understand how this works, run the donor report from a separate program that read the database.

\n", + "

Generally, be sure to adapt the exception handling so that it helps identify any database errors, and consider how you may need to adapt your tests.

\n", + "

Be sure to give a lot of thought to what you should use as the primary key for your Peewee classes. In doing this, just consider data items that are unique in the context of the mailroom application. If you have to resort to generated keys, be sure to note why in the applicable docstring. And talking of which, be sure to define all your classes, as you learned in the videos.

\n", + "

The example programs for the videos will be a good starting point for reminders of syntax, but remember that the primary determinate of the structure of your solution will be a good object oriented Python application. The fact that it will now be persistent should not make too ,much difference. 

\n", + "

\n", + "

Submitting Your Work 

\n", + "

Put the file(s) (ex: a_new_file.py) in your student directory in a new subdirectory named for this lesson, and add it to your clone early (git add a_new_file.py). Make frequent commits with good, clear messages about what you're doing and why.

\n", + "

When you're done and ready for the instructors to review your work, push your changes to your GitHub fork (git push) and then go to the GitHub website and make a pull request. Copy the link to the pull request.

\n", + "

Click the Submit Assignment button in the upper right.

\n", + "

Part 1: File(s)

\n", + "

Use the Choose File button to find and select the saved .py file or, if there are multiple files for the assignment, the .zip file.

\n", + "

Part 2: GitHub Link

\n", + "

Paste the GitHub link to the pull request in the comments area.

\n", + "

Click the Submit Assignment button.

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
👹
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.6.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Student/Ruohan/lesson07/lesson_seven_relational_databases.py b/Student/Ruohan/lesson07/lesson_seven_relational_databases.py new file mode 100644 index 0000000..52548cc --- /dev/null +++ b/Student/Ruohan/lesson07/lesson_seven_relational_databases.py @@ -0,0 +1,1148 @@ + +# coding: utf-8 + +# ### Preface: ORM or not? +# +# https://www.quora.com/What-are-the-pros-and-cons-of-using-raw-SQL-versus-ORM-for-database-development +# +# https://www.quora.com/Which-is-better-ORM-or-Plain-SQL +# +# http://mikehadlow.blogspot.com/2012/06/when-should-i-use-orm.html +# +# https://enterprisecraftsmanship.com/2015/11/30/do-you-need-an-orm/ +# +# https://medium.com/@mithunsasidharan/should-i-or-should-i-not-use-orm-4c3742a639ce +# +# https://xkcd.com/1409/ +# +#
+#
+#
+#
+#
+#
+#
+#
+#
🐍
+ +# +#

Introduction

+#

In this lesson we are going to look at how to use a relational database with Python. Using relational databases is a massive subject in its own right, but we are going to concentrate on how to use these technologies simply and effectively.

+#

What we learn here will be a foundation on which you can build as your database needs increase in volume and complexity.

+#

Learning Objectives

+#

Upon successful completion of this lesson, you will be able to:

+#
    +#
  • Apply data definition techniques to help assure the quality of the data your Python programs use.
  • +#
  • Store and retrieve single and multiple sets of records in a database from your Python programs so that you can leverage data management services from a database.
  • +#
  • Use simple techniques to help model data correctly in a relational database, and recognize the tradeoffs between different options for this. 
  • +#
+#

New Words, Concepts, and Tools

+#
    +#
  • We are going to learn about relational databases, data definition and management, and SQL. We will cover object relational mapping and relational database design, but all aligned to simplicity of use with Python. 
  • +#
+#

Required Reading

+# +#

Optional Reading

+#
    +#
  • How to interact with sqlite from Python (does not use Peewee)
  • +#
  • How to interact with PostgreSQL from Python.
  • +#
  • A great reference book for SQL that shows details of SQL for several databases is "SQL in a Nutshell: A Desktop Quick Reference Guide"
  • +#
  • +#

    If you really want to understand the details of SQL, then this is an excellent book: "Joe Celko's SQL Programming Style (The Morgan Kaufmann Series in Data Management Systems)"

    +#
  • +#
  • Data model design is a complex topic that requires lots of knowledge. If you do a lot of database work then the three books in the series "The Data Model Resource Book" (Vol. 1, 2 and 3) are invaluable (the links are the numbers).
  • +#
+#

+# +# +#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+# + +# +#

Here we are going to cover the background to why we use databases, and set the stage for later, when we will start to +# develop a Python database application.

+#

You will probably wish to clone the repository you can find on +# GitHub. For this first video, the files are in the "stuff" directory. These are the files we refer to, and they +# are included here, below the video:

+# +#

 

+#

Video 1: Relational Databases with Python

+# +# +#

simple_data_write.py

+ +# In[ ]: + + +""" +write a csv file +data is "anonymous" (no schema) + +Creates a file that looks like this: + +John,502-77-3056,2/27/1985,117.45 + +""" + +import csv + +peopledata = ['John', '502-77-3056', '2/27/1985', 117.45] + +with open('simple_data_write.csv', 'w') as people: + peoplewriter = csv.writer(people, quotechar="'", quoting=csv.QUOTE_ALL) + peoplewriter.writerow(peopledata) + + +# ``` +# 'John','502-77-3056','2/27/1985','117.45' +# ``` +#

+#

simple_data_write_headers.py

+ +# In[ ]: + + +""" +write a csv file +data is "anonymous" (no schema) + +Creates a file that looks like this: + +John,502-77-3056,2/27/1985,117.45 + +""" + +import csv + +people_headers = ['Person Name','SSN','BirthDate','Account Balance'] +people_data = [ + ['John', '502-77-3056', '2/27/1985', 117.45], + ['Jane', '756-01-5323', '12/01/1983', 120.9], +] + +with open('simple_data_write_headers.tsv', 'w') as people: + peoplewriter = csv.writer(people, delimiter='|', quoting=csv.QUOTE_NONNUMERIC) + peoplewriter.writerow(people_headers) + for person in people_data: + peoplewriter.writerow(person) + + +# ``` +# "Person Name"|"SSN"|"BirthDate"|"Account Balance" +# "John"|"502-77-3056"|"2/27/1985"|"117.45" +# "Jane"|"756-01-5323"|"12/01/1983"|"120.9" +# +# ``` + +#

 

+#

We have covered the basis of data definition, and why it is important. We now know what a schema is and why it is important. +# Now we can start to write a Python program that uses a database.

+#

 

+#

+# Be sure you cloned the repository we mentioned prior to video 1 from  +# GitHub +# . In this video we will be using the modules in the "src" directory We start with  +# v00_personjob_model.py +#

+#

Key fragments are included here too, below the video.

+# +# +#

Video 2: Using the Model, Using the Person Class, Using the Job Class

+# +#

 Here is the model code:

+# + +# In[ ]: + + +""" + Simple database example with Peewee ORM, sqlite and Python + Here we define the schema + Use logging for messages so they can be turned off +""" + +import logging +from peewee import * + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info('One off program to build the classes from the model in the database') + +logger.info('Here we define our data (the schema)') +logger.info('First name and connect to a database (sqlite here)') + +logger.info('The next 3 lines of code are the only database specific code') + +database = SqliteDatabase('personjob.db') +database.connect() +database.execute_sql('PRAGMA foreign_keys = ON;') # needed for sqlite only + +logger.info('This means we can easily switch to a different database') +logger.info('Enable the Peewee magic! This base class does it all') +logger.info('By inheritance only we keep our model (almost) technology neutral') + +class BaseModel(Model): + class Meta: + database = database + +class Person(BaseModel): + """ + This class defines Person, which maintains details of someone + for whom we want to research career to date. + """ + + logger.info('Note how we defined the class') + + logger.info('Specify the fields in our model, their lengths and if mandatory') + logger.info('Must be a unique identifier for each person') + + person_name = CharField(primary_key = True, max_length = 30) + lives_in_town = CharField(max_length = 40) + nickname = CharField(max_length = 20, null = True) + +class Job(BaseModel): + """ + This class defines Job, which maintains details of past Jobs + held by a Person. + """ + + logger.info('Now the Job class with a simlar approach') + job_name = CharField(primary_key = True, max_length = 30) + logger.info('Dates') + start_date = DateField(formats = 'YYYY-MM-DD') + end_date = DateField(formats = 'YYYY-MM-DD') + logger.info('Number') + + salary = DecimalField(max_digits = 7, decimal_places = 2) + logger.info('Which person had the Job') + person_employed = ForeignKeyField(Person, related_name='was_filled_by', null = False) + +class PersonNumKey(BaseModel): + """ + This class defines Person, which maintains details of someone + for whom we want to research career to date. + """ + + logger.info('An alternate Person class') + logger.info("Note: no primary key so we're give one 'for free'") + + person_name = CharField(max_length = 30) + lives_in_town = CharField(max_length = 40) + nickname = CharField(max_length = 20, null = True) + +database.create_tables([ + Job, + Person, + PersonNumKey + ]) + +database.close() + + +# +# +#

 

+#

Now we have looked at the model, lets look at how we create, read, and delete data from the database, using the Person class. Here +# we use the following code: +#   +# v3_p1_populate_db.py, then  +# v3_p1_populate_db.py and finally  +# v3_p3_add_and_delete.py +# .

+#

Video 3: Using the Person class

+# + +# In[ ]: + + +# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p1.py +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" +import logging +# from personjob_model import * + +def populate_db(): + """ + add person data to database + """ + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + database = SqliteDatabase('personjob.db') + + logger.info('Working with Person class') + logger.info('Note how I use constants and a list of tuples as a simple schema') + logger.info('Normally you probably will have prompted for this from a user') + + PERSON_NAME = 0 + LIVES_IN_TOWN = 1 + NICKNAME = 2 + + people = [ + ('Andrew', 'Sumner', 'Andy'), + ('Peter', 'Seattle', None), + ('Susan', 'Boston', 'Beannie'), + ('Pam', 'Coventry', 'PJ'), + ('Steven', 'Colchester', None), + ] + + logger.info('Creating Person records: iterate through the list of tuples') + logger.info('Prepare to explain any errors with exceptions') + logger.info('and the transaction tells the database to fail on error') + + try: + database.connect() + database.execute_sql('PRAGMA foreign_keys = ON;') + for person in people: + with database.transaction(): + new_person = Person.create( + person_name = person[PERSON_NAME], + lives_in_town = person[LIVES_IN_TOWN], + nickname = person[NICKNAME]) + new_person.save() + logger.info('Database add successful') + + logger.info('Print the Person records we saved...') + for saved_person in Person: + logger.info(f'{saved_person.person_name} lives in {saved_person.lives_in_town} ' + f'and likes to be known as {saved_person.nickname}') + + except Exception as e: + logger.info(f'Error creating = {person[PERSON_NAME]}') + logger.info(e) + logger.info('See how the database protects our data') + + finally: + logger.info('database closes') + database.close() + +populate_db() + + +# In[ ]: + + +# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p2.py +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" + +# from personjob_model import * + +import logging + +def select_and_update(): + """" + show how we can select a specific record, and then search and read through several records + """ + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + database = SqliteDatabase('personjob.db') + + try: + database.connect() + database.execute_sql('PRAGMA foreign_keys = ON;') + + logger.info('Find and display by selecting a spcific Person name...') + aperson = Person.get(Person.person_name == 'Susan') + + logger.info(f'{aperson.person_name} lives in {aperson.lives_in_town} ' + f' and likes to be known as {aperson.nickname}') + + logger.info('Search and display all Person with missing nicknames') + logger.info('Our person class inherits select(). Specify search with .where()') + logger.info('Peter gets a nickname but noone else') + + for person in Person.select().where(Person.nickname == None): + logger.info(f'{person.person_name} does not have a nickname; see: {person.nickname}') + if person.person_name == 'Peter': + logger.info('Changing nickname for Peter') + logger.info('Update the database') + person.nickname = 'Painter' + person.save() + else: + logger.info(f'Not giving a nickname to {person.person_name}') + + logger.info('And here is where we prove it by finding Peter and displaying') + aperson = Person.get(Person.person_name == 'Peter') + logger.info(f'{aperson.person_name} now has a nickname of {aperson.nickname}') + + except Exception as e: + logger.info(e) + + finally: + database.close() + +select_and_update() + + +# In[ ]: + + +# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v3_p3.py + +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" + +import logging + +# from personjob_model import * + +database = SqliteDatabase('personjob.db') + +def add_and_delete(): + """" + show how we can add a record, and delete a record + """ + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + database = SqliteDatabase('personjob.db') + try: + database.connect() + database.execute_sql('PRAGMA foreign_keys = ON;') + + logger.info('Add and display a Person called Fred; then delete him...') + logger.info('Add Fred in one step') + + new_person = Person.create( + person_name = 'Fred', + lives_in_town = 'Seattle', + nickname = 'Fearless') + new_person.save() + + logger.info('Show Fred') + aperson = Person.get(Person.person_name == 'Fred') + + logger.info(f'We just created {aperson.person_name}, who lives in {aperson.lives_in_town}') + logger.info('but now we will delete him...') + + aperson.delete_instance() + + logger.info('Reading and print all Person records (but not Fred; he has been deleted)...') + + for person in Person: + logger.info(f'{person.person_name} lives in {person.lives_in_town} and likes to be known as {person.nickname}') + + except Exception as e: + logger.info(e) + + finally: + database.close() + +add_and_delete() + + +# +# +# +# +# +# +# +# +# +# +# +# +# +#

 

+#

Working with one class is not typical. Usually we will have several. We'll illustrate this by working with the Job class. +# He we will use all the Python modules for the repository that start with v4:

+#

Video 4: Using the Job class

+# +# +# +# +# +# +# +# +# + +# In[ ]: + + +# https://github.com/milesak60/RDBMS-example/blob/master/personjob_learning_v5_p1.py + +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" + +# from personjob_model import * + +import logging + +def populate_db(): + """ + add job data to database + """ + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + database = SqliteDatabase('personjob.db') + + logger.info('Working with Job class') + logger.info('Creating Job records: just like Person. We use the foreign key') + + JOB_NAME = 0 + START_DATE = 1 + END_DATE = 2 + SALARY = 3 + PERSON_EMPLOYED = 4 + + jobs = [ + ('Analyst', '2001-09-22', '2003-01-30',65500, 'Andrew'), + ('Senior analyst', '2003-02-01', '2006-10-22', 70000, 'Andrew'), + ('Senior business analyst', '2006-10-23', '2016-12-24', 80000, 'Andrew'), + ('Admin supervisor', '2012-10-01', '2014-11,10', 45900, 'Peter'), + ('Admin manager', '2014-11-14', '2018-01,05', 45900, 'Peter') + ] + + try: + database.connect() + database.execute_sql('PRAGMA foreign_keys = ON;') + for job in jobs: + with database.transaction(): + new_job = Job.create( + job_name = job[JOB_NAME], + start_date = job[START_DATE], + end_date = job[END_DATE], + salary = job[SALARY], + person_employed = job[PERSON_EMPLOYED]) + new_job.save() + + logger.info('Reading and print all Job rows (note the value of person)...') + for job in Job: + logger.info(f'{job.job_name} : {job.start_date} to {job.end_date} for {job.person_employed}') + + except Exception as e: + logger.info(f'Error creating = {job[JOB_NAME]}') + logger.info(e) + + finally: + logger.info('database closes') + database.close() + +populate_db() + + +# In[ ]: + + +# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p2.py + +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" + +def join_classes(): + """ + demonstrate how to join classes together : matches + """ + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + database = SqliteDatabase('personjob.db') + + logger.info('Working with Job class') + + logger.info('Now resolve the join and print (INNER shows only jobs that match person)...') + logger.info('Notice how we use a query variable in this case') + logger.info('We select the classes we need, and we join Person to Job') + logger.info('Inner join (which is the default) shows only records that match') + + try: + database.connect() + database.execute_sql('PRAGMA foreign_keys = ON;') + query = (Person + .select(Person, Job) + .join(Job, JOIN.INNER) + ) + + logger.info('View matching records from both tables') + for person in query: + logger.info(f'Person {person.person_name} had job {person.job.job_name}') + + except Exception as e: + logger.info(f'Error creating = {job[JOB_NAME]}') + logger.info(e) + + finally: + logger.info('database closes') + database.close() + +join_classes() + + +# In[ ]: + + +# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p3.py + + +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" + +def join_classes(): + """ + demonstrate how to join classes together : no matches too + """ + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + database = SqliteDatabase('personjob.db') + + logger.info('View matching records and Persons without Jobs (note LEFT_OUTER)') + + try: + database.connect() + database.execute_sql('PRAGMA foreign_keys = ON;') + query = (Person + .select(Person, Job) + .join(Job, JOIN.LEFT_OUTER) + ) + + for person in query: + try: + logger.info(f'Person {person.person_name} had job {person.job.job_name}') + + except Exception as e: + logger.info(f'Person {person.person_name} had no job') + + + logger.info('Example of how to summarize data') + logger.info('Note select() creates a count and names it job_count') + logger.info('group_by and order_by control level and sorting') + + query = (Person + .select(Person, fn.COUNT(Job.job_name).alias('job_count')) + .join(Job, JOIN.LEFT_OUTER) + .group_by(Person) + .order_by(Person.person_name)) + + for person in query: + logger.info(f'{person.person_name} had {person.job_count} jobs') + + + except Exception as e: + logger.info(e) + + finally: + database.close() + +join_classes() + + +# In[ ]: + + +# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p4.py +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" + +def show_integrity_add(): + """ + demonstrate how database protects data inegrity : add + """ + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + database = SqliteDatabase('personjob.db') + + try: + database.connect() + database.execute_sql('PRAGMA foreign_keys = ON;') + with database.transaction(): + logger.info('Try to add a new job where a person doesnt exist...') + + addjob = ('Sales', '2010-04-01', '2018-02-08', 80400, 'Harry') + + logger.info('Adding a sales job for Harry') + logger.info(addjob) + new_job = Job.create( + job_name = addjob[JOB_NAME], + start_date = addjob[START_DATE], + end_date = addjob[END_DATE], + salary = addjob[SALARY], + person_employed = addjob[PERSON_EMPLOYED]) + new_job.save() + + except Exception as e: + logger.info('Add failed because Harry is not in Person') + logger.info(f'For Job create: {addjob[0]}') + logger.info(e) + +show_integrity_add() + + +# In[ ]: + + +# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p5.py +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" + +def show_integrity_del(): + """ + demonstrate how database protects data inegrity : delete + """ + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + database = SqliteDatabase('personjob.db') + + try: + database.connect() + database.execute_sql('PRAGMA foreign_keys = ON;') + logger.info('Try to Delete a person who has jobs...') + with database.transaction(): + aperson = Person.get(Person.person_name == 'Andrew') + logger.info(f'Trying to delete {aperson.person_name} who lives in {aperson.lives_in_town}') + aperson.delete_instance() + + except Exception as e: + logger.info('Delete failed because Andrew has Jobs') + logger.info(f'Delete failed: {aperson.person_name}') + logger.info(e) + + finally: + database.close() + +show_integrity_del() + + +# In[ ]: + + +# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p6.py +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" + +def add_with_without_BK(): + """ + demonstrate impact of business keys + """ + + PERSON_NAME = 0 + LIVES_IN_TOWN = 1 + NICKNAME = 2 + people = [ + ('Andrew', 'Sumner', 'Andy'), + ('Peter', 'Seattle', None), + ('Susan', 'Boston', 'Beannie'), + ('Pam', 'Coventry', 'PJ'), + ('Steven', 'Colchester', None), + ] + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + database = SqliteDatabase('personjob.db') + + logger.info('Try creating Person records again: it will fail') + + try: + database.connect() + database.execute_sql('PRAGMA foreign_keys = ON;') + try: + with database.transaction(): + for person in people: + new_person = Person.create( + person_name = person[PERSON_NAME], + lives_in_town = person[LIVES_IN_TOWN], + nickname = person[NICKNAME]) + new_person.save() + logger.info('Database add successful') + + except Exception as e: + logger.info(f'Error creating = {person[PERSON_NAME]}') + logger.info(e) + + logger.info('We make sure duplicates are not unintentionally created this way') + logger.info('BUT: how do we really identify a Person (uniquely)?') + + logger.info('Creating Person records, but in a new table with generated PK...') + try: + with database.transaction(): + for person in people: + new_person = PersonNumKey.create( + person_name = person[PERSON_NAME], + lives_in_town = person[LIVES_IN_TOWN], + nickname = person[NICKNAME]) + new_person.save() + + except Exception as e: + logger.info(f'Error creating = {person[0]}') + logger.info(e) + + logger.info('Watch what happens when we do it again') + + try: + with database.transaction(): + for person in people: + new_person = PersonNumKey.create( + person_name = person[PERSON_NAME], + lives_in_town = person[LIVES_IN_TOWN], + nickname = person[NICKNAME]) + new_person.save() + + except Exception as e: + logger.info(f'Error creating = {person[0]}') + logger.info(e) + + logger.info('Note no PK specified, no PK violation; "duplicates" created!') + for person in PersonNumKey.select(): + logger.info(f'Name : {person.person_name} with id: {person.id}') + + except Exception as e: + logger.info(e) + + finally: + database.close() + +add_with_without_BK() + + +# In[ ]: + + +# https://raw.githubusercontent.com/milesak60/RDBMS-example/master/personjob_learning_v5_p7.py +""" + Learning persistence with Peewee and sqlite + delete the database to start over + (but running this program does not require it) +""" + +def cant_change_PK(): + """ + show that PKs cant be changed (and that there is no error!) + """ + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + + logger.info("Back to Person class: try to change Peter's name") + + aperson = Person.get(Person.person_name == 'Peter') + logger.info(f'Current value is {aperson.person_name}') + + logger.info('Update Peter to Peta, thereby trying to change the PK...') + + try: + try: + with database.transaction(): + aperson = Person.get(Person.person_name == 'Peter') + aperson.person_name = 'Peta' + aperson.save() + logger.info(f'Tried to change Peter to {aperson.person_name}') + + except Exception as e: + logger.info(f'Cant change a PK and caught you trying') # not caught; no error thrown by Peewee + logger.info(e) + + aperson = Person.get(Person.person_name == 'Peter') + logger.info(f'Looked for Peter: found! -> {aperson.person_name}') + + try: + aperson = Person.get(Person.person_name == 'Peta') + + except Exception as e: + logger.info(f'Looking for Peta results in zero records. PK changes are ignored and do not throw an error!!!!') + logger.info(f'Cant change a PK') + logger.info('PK "change" can only be achieved with a delete and new create') + + finally: + database.close() + +cant_change_PK() + + +# +# +# +# +# +# +# +# +#

 

+#

Now we are going to learn about the best way to design the data in our database.  We will use the digram in the "stuff" +# directory, which is also included below, along with the SQL code:

+#

 

+#

Video: Behind the scenes / Database Technologies

+# +#

 

+#

Database diagram:

+#

  +#  

+#

Code samples from the video:

+#

SQL statement

+#
+#
select* from person;
+# 
+#
+#

Start sqlite3 database (from the command line):

+#
+#
sqlite3 personjob.db
+# 
+#
+#

The sqlite> prompt indicates we are ready to enter sqlite commands.

+#
+#
sqlite> .tables
+# job person personnumkey
+# 
+#
+#

  Here is how sqlite sees the schema:

+#
+#
sqlite> .schema
+# 
+# CREATE TABLE IF NOT EXISTS "person" ("person_name" VARCHAR(30) NOT NULL PRIMARY KEY, "lives_in_town" VARCHAR(40) NOT NULL, "nickname" VARCHAR(20));
+# CREATE TABLE IF NOT EXISTS "job" ("job_name" VARCHAR(30) NOT NULL PRIMARY KEY, "start_date" DATE NOT NULL, "end_date" DATE NOT NULL, "salary" DECIMAL(7, 2) NOT NULL, "person_employed_id" VARCHAR(30) NOT NULL, FOREIGN KEY ("person_employed_id") REFERENCES "person" ("person_name"));
+# CREATE INDEX "job_person_employed_id" ON "job" ("person_employed_id");
+# CREATE TABLE IF NOT EXISTS "personnumkey" ("id" INTEGER NOT NULL PRIMARY KEY, "person_name" VARCHAR(30) NOT NULL, "lives_in_town" VARCHAR(40) NOT NULL, "nickname" VARCHAR(20));
+#
+#
+#

 

+#

 

+#
+#
sqlite> .mode column
+# sqlite> .width 15 15 15 15 15
+# sqlite> .headers on
+# 
+#
+#

 

+#
+#
sqlite> select * from person;
+# sqlite> select * from job;
+# 
+#
+#

Enter .quit to leave sqlite.

+#

Lesson Summary

+#

In this lesson we have learned about how we define, store and retrieve data in a relational database using Python, Peewee +# and sqlite.

+# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# + +#

 

+#

Video: Conclusion

+#

  +# PeeWee documentation +#

+#

 

+ +#
+#
+#
+#
+#
+# +# ## Non-ORM Example + +# In[ ]: + + +import sqlite3 +import os + +database_filename = "personjob.db" + +try: + # remove if file exists + os.remove(database_filename) +except: + # ignore if file doesn't exist + pass + +statements = [ + """CREATE TABLE IF NOT EXISTS "person" ("person_name" VARCHAR(30) NOT NULL PRIMARY KEY, "lives_in_town" VARCHAR(40) NOT NULL, "nickname" VARCHAR(20));""", + """CREATE TABLE IF NOT EXISTS "job" ("job_name" VARCHAR(30) NOT NULL PRIMARY KEY, "start_date" DATE NOT NULL, "end_date" DATE NOT NULL, "salary" DECIMAL(7, 2) NOT NULL, "person_employed_id" VARCHAR(30) NOT NULL, FOREIGN KEY ("person_employed_id") REFERENCES "person" ("person_name"));""", + """CREATE INDEX "job_person_employed_id" ON "job" ("person_employed_id");""", + """CREATE TABLE IF NOT EXISTS "personnumkey" ("id" INTEGER NOT NULL PRIMARY KEY, "person_name" VARCHAR(30) NOT NULL, "lives_in_town" VARCHAR(40) NOT NULL, "nickname" VARCHAR(20));""", +] + +with sqlite3.connect(database_filename) as conn: + try: + for stmt in statements: + print(stmt) + conn.execute(stmt) + except Exception as e: + print(e) + + +#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+# +# +# +#

Warm up for the assignment

+#

Before we launch into the assignment, let's be sure that you have everything you need to get started. We'll enhance the modules from the video along the way.

+#

Preparing

+#

Be sure to

+#
+#
pip install peewee
+# 
+#
+#

 first.

+#

Also, be sure that  

+#
+#
sqlite3 -version
+# 
+#
+#

returns the sqlite3 version number, indicating it is installed. It should be, as it's bundled with Python 3.

+#

Clone the repo at 

+#
git@github.com:milesak60/RDBMS.git
+#

although you should already have that from earlier in this lesson.

+#

Make sure everything runs before proceeding to the next step.

+#

Let's add a department

+#

We have details of Persons. We have details of Jobs. Now we need to track in which Department a Person held a Job. For a Department, we need to know it's department number, which is 4 characters long and start with a letter. We need to know the department name (30 characters), and the name of the department manager (30 characters). We also need to know the duration in days that the job was held. Think about this last one carefully.

+#

Make the necessary changes, annotating the code with log statements to explain what's going on. Also, draw a digram to help think through how you will incorporate Department into the programs.

+#

Finally, produce a list using pretty print that shows all of the departments a person worked in for every job they ever had. 

+#

Instructions

+#

Once you've completed the activity from the lesson content, commit your changes and submit:

+#
    +#
  • a link to your repository on GitHub
  • +#
  • the relevant .py file(s)
  • +#
+#

We'll be grading this activity purely on the percentage of included tests that pass.

+#

Submitting Your Work 

+#

Put the file(s) (ex: a_new_file.py) in your student directory in a new subdirectory named for this lesson, and add it to your clone early (git add a_new_file.py). Make frequent commits with good, clear messages about what you're doing and why.

+#

When you're done and ready for the instructors to review your work, push your changes to your GitHub fork (git push) and then go to the GitHub website and make a pull request. Copy the link to the pull request.

+#

Click the Submit Assignment button in the upper right.

+#

Part 1: File(s)

+#

Use the Choose File button to find and select the saved .py file or, if there are multiple files for the assignment, the .zip file.

+#

Part 2: GitHub Link

+#

Paste the GitHub link to the pull request in the comments area.

+#

Click the Submit Assignment button.

+ +#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+#
+# +# +# +#

Instructions

+#

Goal:

+#

Redesign the object oriented mailroom program from class one using Peewee classes so that the data is persisted in sqlite. The approach you used in the OO exercise will naturally lend itself to this. (See Lesson 4, video 2)

+#

Suggestions

+#

You will need ways to add, update and delete data. Update and delete will mean that you need to find the data to update / delete. Perhaps you can do this by allowing a search first, and then selecting the particular instance to delete.

+#

Remember that you need to read from the database, rather than relying on data held in variables when your program is running. To show you understand how this works, run the donor report from a separate program that read the database.

+#

Generally, be sure to adapt the exception handling so that it helps identify any database errors, and consider how you may need to adapt your tests.

+#

Be sure to give a lot of thought to what you should use as the primary key for your Peewee classes. In doing this, just consider data items that are unique in the context of the mailroom application. If you have to resort to generated keys, be sure to note why in the applicable docstring. And talking of which, be sure to define all your classes, as you learned in the videos.

+#

The example programs for the videos will be a good starting point for reminders of syntax, but remember that the primary determinate of the structure of your solution will be a good object oriented Python application. The fact that it will now be persistent should not make too ,much difference. 

+#

+#

Submitting Your Work 

+#

Put the file(s) (ex: a_new_file.py) in your student directory in a new subdirectory named for this lesson, and add it to your clone early (git add a_new_file.py). Make frequent commits with good, clear messages about what you're doing and why.

+#

When you're done and ready for the instructors to review your work, push your changes to your GitHub fork (git push) and then go to the GitHub website and make a pull request. Copy the link to the pull request.

+#

Click the Submit Assignment button in the upper right.

+#

Part 1: File(s)

+#

Use the Choose File button to find and select the saved .py file or, if there are multiple files for the assignment, the .zip file.

+#

Part 2: GitHub Link

+#

Paste the GitHub link to the pull request in the comments area.

+#

Click the Submit Assignment button.

+ +#
👹
diff --git a/Student/Ruohan/lesson07/mailroom_assignment/__pycache__/creat_donor_donation_db.cpython-36.pyc b/Student/Ruohan/lesson07/mailroom_assignment/__pycache__/creat_donor_donation_db.cpython-36.pyc new file mode 100644 index 0000000..a7a1445 Binary files /dev/null and b/Student/Ruohan/lesson07/mailroom_assignment/__pycache__/creat_donor_donation_db.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson07/mailroom_assignment/__pycache__/donor_donation_model.cpython-36.pyc b/Student/Ruohan/lesson07/mailroom_assignment/__pycache__/donor_donation_model.cpython-36.pyc new file mode 100644 index 0000000..8805067 Binary files /dev/null and b/Student/Ruohan/lesson07/mailroom_assignment/__pycache__/donor_donation_model.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson07/mailroom_assignment/creat_donor_donation_db.py b/Student/Ruohan/lesson07/mailroom_assignment/creat_donor_donation_db.py new file mode 100644 index 0000000..eacba72 --- /dev/null +++ b/Student/Ruohan/lesson07/mailroom_assignment/creat_donor_donation_db.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +""" + creat donor and donation dabatbas contains only two table: donor and donation + (And some sample information has been input) +""" + +from donor_donation_model import * +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +database.create_tables([ + Donor, + Donation + ]) + +logger.info('Working with Donor class') + +def get_donor_sample_data(): + donors = [ + ('00001', 'William Gates III', 'Seattle', '1234567890'), + ('00002', 'Cara Delevinge', 'San fransisco', '0983457621'), + ('00003', 'Ellen Degenerous','LA', None), + ('00004', 'Kate Upton', 'LA', '9876543201'), + ('00005', 'Suki Waterhouse', 'London', '654823339'), + ] + return donors + +DONOR_ID = 0 +DONOR_NAME = 1 +CITY = 2 +PHONE_NUMBER = 3 + + +donors = get_donor_sample_data() + +for donor in donors: + try: + with database.transaction(): + new_donor = Donor.create( + donor_id = donor[DONOR_ID], + donor_name = donor[DONOR_NAME], + city = donor[CITY], + phone_number = donor[PHONE_NUMBER]) + new_donor.save() + except Exception as e: + logger.info(f'Error creating = {donor[DONOR_ID]} name is {donor[DONOR_NAME]} with error {e}') + + +logger.info('Working with Donation class') + +def get_donation_sample_data(): + donations = [ + (100000, '2018-03-01', '00001'), + (30000, '2017-11-21', '00002'), + (15000, '2017-04-27','00003'), + (20000, '2018-01-01', '00004'), + (200000, '2017-08-09', '00001') + ] + return donations + +AMOUNT = 0 +DONATION_TIME = 1 +DONATION_DONORID = 2 + +donations = get_donation_sample_data() + +for donation in donations: + try: + with database.transaction(): + new_donation = Donation.create( + amount = donation[AMOUNT], + donation_time = donation[DONATION_TIME], + donation_donorid = donation[DONATION_DONORID]) + new_donation.save() + except Exception as er: + logger.info(f'Error creating = {donation[AMOUNT]} donation time is {donation[DONATION_TIME]} with error {er}') + + +database.close() diff --git a/Student/Ruohan/lesson07/mailroom_assignment/donor_donation.db b/Student/Ruohan/lesson07/mailroom_assignment/donor_donation.db new file mode 100644 index 0000000..567acd6 Binary files /dev/null and b/Student/Ruohan/lesson07/mailroom_assignment/donor_donation.db differ diff --git a/Student/Ruohan/lesson07/mailroom_assignment/donor_donation_db_handle.py b/Student/Ruohan/lesson07/mailroom_assignment/donor_donation_db_handle.py new file mode 100644 index 0000000..6487cc2 --- /dev/null +++ b/Student/Ruohan/lesson07/mailroom_assignment/donor_donation_db_handle.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python + +""" + +""" + +from donor_donation_model import * +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +def num_donation(): + query = (Donor + .select(Donor, fn.COUNT(Donation.amount).alias('donation_count')) + .join(Donation, JOIN.LEFT_OUTER) + .group_by(Donor) + .order_by(Donor.donor_id)) + + for donor in query: + logger.info(f'Donor id {donor.donor_id} with name {donor.donor_name} has made {donor.donation_count} times donation') + +def dotal_donation(): + query = (Donor + .select(Donor, fn.SUM(Donation.amount).alias('donation_total')) + .join(Donation, JOIN.LEFT_OUTER) + .group_by(Donor) + .order_by(Donor.donor_id)) + for donor in query: + logger.info(f'Donor id {donor.donor_id} with name {donor.donor_name} has made ${donor.donation_total} in total') + + +def last_donation(): + query = (Donor + .select(Donor, Donation.amount.alias('last_amount'), fn.MAX(Donation.donation_time).alias('last_time')) + .join(Donation) + .group_by(Donation.donation_donorid) + .order_by(Donation.donation_donorid)) + for donor in query: + logger.info(f'Donor id {donor.donor_id} with name {donor.donor_name} latest donation is ${donor.donation.last_amount}') + +def average_donation(): + query = (Donor + .select(Donor, fn.AVG(Donation.amount).alias('donation_average')) + .join(Donation, JOIN.LEFT_OUTER) + .order_by(Donor.donor_id)) + + for donor in query: + logger.info(f'Donor id {donor.donor_id} with name {donor.donor_name} average donation is ${donor.donation_average}') + + +num_donation() +dotal_donation() +last_donation() +average_donation() + +database.close() + + +# def main(): +# selection_dict = {"1": send_thank_you, +# "2": print_donor_report, +# "3": db.save_letters_to_disk, +# "4": quit} +# +# while True: +# selection = main_menu_selection() +# try: +# selection_dict[selection]() +# except KeyError: +# print("error: menu selection is invalid!") +# +# if __name__ == "__main__": +# +# main() diff --git a/Student/Ruohan/lesson07/mailroom_assignment/donor_donation_model.py b/Student/Ruohan/lesson07/mailroom_assignment/donor_donation_model.py new file mode 100644 index 0000000..606bb4f --- /dev/null +++ b/Student/Ruohan/lesson07/mailroom_assignment/donor_donation_model.py @@ -0,0 +1,30 @@ +import logging +from peewee import * + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + + +database = SqliteDatabase('donor_donation.db') +database.connect() +database.execute_sql('PRAGMA foreign_keys = ON;') + + + +class BaseModel(Model): + class Meta: + database = database + + +class Donor(BaseModel): + donor_id = CharField(primary_key = True, max_length = 12) + donor_name = CharField(max_length = 30) + city = CharField(max_length = 30, null = True) + phone_number = CharField(max_length = 30, null = True) + + +class Donation(BaseModel): + amount = DecimalField(decimal_places = 2) + donation_time = DateField(formats = 'YYYY-MM-DD') + donation_donorid = ForeignKeyField(Donor) diff --git a/Student/Ruohan/lesson07/mailroom_assignment/mailroom_oo_new.py b/Student/Ruohan/lesson07/mailroom_assignment/mailroom_oo_new.py new file mode 100644 index 0000000..f195832 --- /dev/null +++ b/Student/Ruohan/lesson07/mailroom_assignment/mailroom_oo_new.py @@ -0,0 +1,142 @@ +#!/usr/bin/env python3 + +import sys +import string + +class Donor(): + + def __init__(self, name, lst = []): + self.name = name.capitalize() + self.donation = lst + + def add_donations(self, amount): + self.donation.append(amount) + + @property + def total(self): + return sum(self.donation) + + + @property + def times(self): + return len(self.donation) + + @property + def ave(self): + return self.total/self.times + + @property + def last(self): + if len(self.donation) > 0: + return self.donation[-1] + else: + return 0 + + def __str__(self): + return f'{self.name}:{self.donation}' + + +class donor_DB(): + + + def __init__(self, dict= {}): + self._donors = dict + + def find_donor(self, name): + if name.lower() in self._donors: + return self._donors[name.lower()] + return None + + def add_donor(self, name): + if self.find_donor(name): + pass + else: + donor = Donor(name) + self._donors[donor.name.lower()] = donor + + def search_donor(self,name): + return self._donors.get(name.lower()) + + def donor_list(self): + return list(self._donors) + + def _letter(self, d): + letter = f'''Dear {d.name} + Thank you for your generous donation of ${d.last:.2f}. + We appreciate your contribution. + Team R ''' + return letter + + def report(self): + head = '{:20}|{:20}|{:10}|{:20}|'.format('Donor Name','Total Given', 'Num Gifts','Average Gifts') + result = [head] + result.append('-'*len(head)) + for donor in self._donors: + result.append(f"{donor.name:20}|${donor.total:19,.2f}|{donor.times:10}|${donor.ave:19,.2f}|") + return result + + def file_letters(self): + for donor in self._donors: + f_name = donor.name+'.txt' + with open(f_name, 'wt') as f: + print(f'''Dear {donor.name}, + Thank you for your generous donation ${donor.total:.2f}. + We appreciate your contribution. + Team R ''', file = f) + + def quit(self): + sys.exit() + + +def Welcome(): + print('Welcome to Mailroom') + db = donor_DB() + while True: + print('\nwhat do you want to do?') + print('1) Check the donor list\n' + '2) A new donation\n' + '3) Send thank you\n' + '4) Creat a report\n' + '5) Send letters to everyone\n' + '6) quit\n') + response = input('>> ') + if response == '1': + print(db.donor_list()) + print('Return to main menu\n') + elif response == '2': + name = input('Enter the donor name: ').lower() + db.add_donor(name) + amount = input('Enter the donation amount: ') + try: amount = float(amount) + except ValueError: + print('error: donation amount is invalid\n') + db.search_donor(name).add_donations(amount) + print('New donation has been recorded') + print('Return to main menu\n') + elif response == '3': + name = input('Who do you want to send thank you letter to: ').lower() + if db.find_donor(name): + print(db._letter(db.find_donor(name))) + else: + print('Donor is not recorded, please add donor to datasets first(chose 2 in main menu)\n') + print('Return to main menu\n') + continue + elif response == '4': + for line in db.report(): + print(line) + print('Return to main menu\n') + elif response == '5': + db.file_letters() + print('Letters have been saved') + print('Return to main menu\n') + elif response == '6': + print('Exit Now') + db.quit() + else: + print('Invalid input') + print('Return to main menu\n') + continue + + +if __name__ == "__main__": + Welcome() diff --git a/Student/Ruohan/lesson08/assignment08/logs/login_databases_dev.log b/Student/Ruohan/lesson08/assignment08/logs/login_databases_dev.log new file mode 100644 index 0000000..e69de29 diff --git a/Student/Ruohan/lesson08/assignment08/logs/mongodb_script.log b/Student/Ruohan/lesson08/assignment08/logs/mongodb_script.log new file mode 100644 index 0000000..dac6574 --- /dev/null +++ b/Student/Ruohan/lesson08/assignment08/logs/mongodb_script.log @@ -0,0 +1,2 @@ +2018-06-27 22:14:57 - INFO - Here is where we use the connect to mongodb. +2018-06-27 22:14:57 - INFO - Note use of f string to embed the user & password (from the tuple). diff --git a/Student/Ruohan/lesson08/assignment08/logs/neo4j_script.log b/Student/Ruohan/lesson08/assignment08/logs/neo4j_script.log new file mode 100644 index 0000000..e69de29 diff --git a/Student/Ruohan/lesson08/assignment08/logs/nosql_dev.log b/Student/Ruohan/lesson08/assignment08/logs/nosql_dev.log new file mode 100644 index 0000000..7b306cc --- /dev/null +++ b/Student/Ruohan/lesson08/assignment08/logs/nosql_dev.log @@ -0,0 +1,6 @@ +2018-06-26 22:07:42 - INFO - Mongodb example to use data from Furniture module, so get it +2018-06-26 22:07:42 - INFO - Here is where we use the connect to mongodb. +2018-06-26 22:07:42 - INFO - Note use of f string to embed the user & password (from the tuple). +2018-06-26 22:10:21 - INFO - Mongodb example to use data from Furniture module, so get it +2018-06-26 22:10:21 - INFO - Here is where we use the connect to mongodb. +2018-06-26 22:10:21 - INFO - Note use of f string to embed the user & password (from the tuple). diff --git a/Student/Ruohan/lesson08/assignment08/logs/placeholder.log b/Student/Ruohan/lesson08/assignment08/logs/placeholder.log new file mode 100755 index 0000000..2d030d7 --- /dev/null +++ b/Student/Ruohan/lesson08/assignment08/logs/placeholder.log @@ -0,0 +1 @@ +delete me diff --git a/Student/Ruohan/lesson08/assignment08/scr/__pycache__/initial_data.cpython-36.pyc b/Student/Ruohan/lesson08/assignment08/scr/__pycache__/initial_data.cpython-36.pyc new file mode 100644 index 0000000..06d7098 Binary files /dev/null and b/Student/Ruohan/lesson08/assignment08/scr/__pycache__/initial_data.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson08/assignment08/scr/__pycache__/login_database.cpython-36.pyc b/Student/Ruohan/lesson08/assignment08/scr/__pycache__/login_database.cpython-36.pyc new file mode 100644 index 0000000..9bd9fbf Binary files /dev/null and b/Student/Ruohan/lesson08/assignment08/scr/__pycache__/login_database.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson08/assignment08/scr/__pycache__/utilities.cpython-36.pyc b/Student/Ruohan/lesson08/assignment08/scr/__pycache__/utilities.cpython-36.pyc new file mode 100644 index 0000000..d6d4572 Binary files /dev/null and b/Student/Ruohan/lesson08/assignment08/scr/__pycache__/utilities.cpython-36.pyc differ diff --git a/Student/Ruohan/lesson08/assignment08/scr/initial_data.py b/Student/Ruohan/lesson08/assignment08/scr/initial_data.py new file mode 100644 index 0000000..a9b00aa --- /dev/null +++ b/Student/Ruohan/lesson08/assignment08/scr/initial_data.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +""" + Data for mongodb +""" + +def get_donor_donation(): + + donor_donation =[ + { + 'name': 'William Gates III', + 'donation': [10000, 30000, 50000], + }, + { + 'name': 'Cara Delevinge', + 'donation': [2000, 1000, 8000], + }, + { + 'name': 'Ellen Degenerous', + 'donation': [30000, 20000], + }, + { + 'name': 'Kate Upton', + 'donation': [9000, 40000, 5000, 4000], + } + ] + return donor_donation diff --git a/Student/Ruohan/lesson08/assignment08/scr/login_database.py b/Student/Ruohan/lesson08/assignment08/scr/login_database.py new file mode 100644 index 0000000..f45173b --- /dev/null +++ b/Student/Ruohan/lesson08/assignment08/scr/login_database.py @@ -0,0 +1,84 @@ +""" + module that will login to the various demonstration databases consistently +""" + +import configparser +from pathlib import Path +import pymongo +import redis +from neo4j.v1 import GraphDatabase, basic_auth + +import utilities + +log = utilities.configure_logger('default', '../logs/login_databases_dev.log') +config_file = Path(__file__).parent.parent / '.config/config.ini' +config = configparser.ConfigParser() + + +def login_mongodb_cloud(): + """ + connect to mongodb and login + """ + + log.info('Here is where we use the connect to mongodb.') + log.info('Note use of f string to embed the user & password (from the tuple).') + try: + config.read(config_file) + user = config["mongodb_cloud"]["user"] + pw = config["mongodb_cloud"]["pw"] + + except Exception as e: + print(f'error: {e}') + + client = pymongo.MongoClient(f'mongodb://{user}:{pw}' + '@cluster0-shard-00-00-wphqo.mongodb.net:27017,' + 'cluster0-shard-00-01-wphqo.mongodb.net:27017,' + 'cluster0-shard-00-02-wphqo.mongodb.net:27017/test' + '?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin') + + return client + + +def login_redis_cloud(): + """ + connect to redis and login + """ + try: + config.read(config_file) + host = config["redis_cloud"]["host"] + port = config["redis_cloud"]["port"] + pw = config["redis_cloud"]["pw"] + + + except Exception as e: + print(f'error: {e}') + + log.info('Here is where we use the connect to redis.') + + try: + r = redis.StrictRedis(host=host, port=port, password=pw, decode_responses=True) + + except Exception as e: + print(f'error: {e}') + + return r + + +def login_neo4j_cloud(): + """ + connect to neo4j and login + + """ + + log.info('Here is where we use the connect to neo4j.') + log.info('') + + config.read(config_file) + + graphenedb_user = config["neo4j_cloud"]["user"] + graphenedb_pass = config["neo4j_cloud"]["pw"] + graphenedb_url = 'bolt://hobby-opmhmhgpkdehgbkejbochpal.dbs.graphenedb.com:24786' + driver = GraphDatabase.driver(graphenedb_url, + auth=basic_auth(graphenedb_user, graphenedb_pass)) + + return driver diff --git a/Student/Ruohan/lesson08/assignment08/scr/mailroom_mongodb.py b/Student/Ruohan/lesson08/assignment08/scr/mailroom_mongodb.py new file mode 100644 index 0000000..fb6acbd --- /dev/null +++ b/Student/Ruohan/lesson08/assignment08/scr/mailroom_mongodb.py @@ -0,0 +1,234 @@ +#!/usr/bin/env python + +""" + mailroom script for mongodb +""" + +import sys +import math +from textwrap import dedent +import pprint +import login_database +import utilities +from initial_data import get_donor_donation + +log = utilities.configure_logger('default', '../logs/mongodb_script.log') + +class Donor(): + + def __init__(self, name, lst = []): + self.name = name.capitalize() + self.donation = lst + def add_donations(self, amount): + self.donation.append(amount) + + @property + def total(self): + return sum(self.donation) + + + @property + def times(self): + return len(self.donation) + + @property + def ave(self): + return self.total/self.times + + @property + def last(self): + if len(self.donation) > 0: + return self.donation[-1] + else: + return 0 + + def __str__(self): + return f'{self.name}:{self.donation}' + +class DonorMongodb(): + + def __init__(self, donor_donation=None): + with login_database.login_mongodb_cloud() as client: + log.info('Step 1: We are going to use a database called dev') + self.db = client['dev'] + + log.info('And in that database use a collection called donor') + self.donor_data = self.db['donor'] + + log.info('Step 2: Now we add data from arguments') + self.donor_data.insert_many(donor_donation) + + @property + def donors(self): + return self.db.donor_data.find() + + def list_donors(self): + """ + creates a list of the donors as a string, so they can be printed + """ + listing = ["Donor list:"] + for donor in self.donors: + listing.append(donor.name) + return "\n".join(listing) + + def find_donor(self, name): + """ + find a donor in the donor db + """ + name_query = {'name': name} + return self.donor_data.find_one(name_query) + + def add_donor(self, name): + """ + Add a new donor to the donor db + """ + donor = Donor(name) + with login_database.login_mongodb_cloud() as client: + self.db = client['dev'] + self.donor_data = self.db['donor'] + self.donor_data.insert(donor) + return donor + + def gen_letter(self, donor): + """ + Generate a thank you letter for the donor + """ + with login_database.login_mongodb_cloud() as client: + self.db = client['dev'] + self.donor_data = self.db['donor'] + name_query = {'name': donor.name} + donor = self.donor_data.find(name_query) + return dedent('''Dear {0:s}, + Thank you for your very kind donation of ${1:.2f}. + It will be put to very good use. + Sincerely, + -The Team + '''.format(donor['name'], donor['donation'][-1]) + ) + + @staticmethod + def sort_key(item): + return item[1] + + def generate_donor_report(self): + """ + Generate the report of the donors and amounts donated. + :returns: the donor report as a string. + """ + report_rows = [] + with login_database.login_mongodb_cloud() as client: + self.db = client['dev'] + self.donor_data = self.db['donor'] + for donor in self.donor_data.find(): + name = donor['name'] + gifts = donor['donation'] + total_gifts = sum(donor['donation']) + num_gifts = len(gifts) + avg_gift = total_gifts / num_gifts + report_rows.append((name, total_gifts, num_gifts, avg_gift)) + + # sort the report data + report_rows.sort(key=self.sort_key) + report = [] + report.append("{:25s} | {:11s} | {:9s} | {:12s}".format("Donor Name", + "Total Given", + "Num Gifts", + "Average Gift")) + report.append("-" * 66) + for row in report_rows: + report.append("{:25s} ${:10.2f} {:9d} ${:11.2f}".format(*row)) + return "\n".join(report) + + def save_letters_to_disk(self): + """ + make a letter for each donor, and save it to disk. + """ + with login_database.login_mongodb_cloud() as client: + self.db = client['dev'] + self.donor_data = self.db['donor'] + for donor in self.donor_data.find(): + print("Writing a letter to:", donor['name']) + letter = self.gen_letter(donor) + filename = donor['name'].replace(" ", "_") + ".txt" + open(filename, 'w').write(letter) + + +donor_donation = get_donor_donation() +db = DonorMongodb(donor_donation) + + +def main_menu_selection(): + """ + Print out the main application menu and then read the user input. + """ + action = input(dedent(''' + Choose an action: + 1 - Send a Thank You + 2 - Create a Report + 3 - Send letters to everyone + 4 - Quit + > ''')) + return action.strip() + + +def send_thank_you(): + """ + Record a donation and generate a thank you message. + """ + # Read a valid donor to send a thank you from, handling special commands to + # let the user navigate as defined. + while True: + name = input("Enter a donor's name" + "(or 'list' to see all donors or 'menu' to exit)> ").strip() + if name == "list": + print(db.list_donors()) + elif name == "menu": + return + else: + break + + while True: + amount_str = input("Enter a donation amount (or 'menu' to exit)> ").strip() + if amount_str == "menu": + return + try: + amount = float(amount_str) + if math.isnan(amount) or math.isinf(amount) or round(amount, 2) == 0.00: + raise ValueError + except ValueError: + print("error: donation amount is invalid\n") + else: + break + + donor = db.find_donor(name) + if donor is None: + donor = db.add_donor(name) + + donor.add_donation(amount) + print(db.gen_letter(donor)) + + +def print_donor_report(): + print(db.generate_donor_report()) + + +def quit(): + sys.exit(0) + + +def main(): + selection_dict = {"1": send_thank_you, + "2": print_donor_report, + "3": db.save_letters_to_disk, + "4": quit} + + while True: + selection = main_menu_selection() + try: + selection_dict[selection]() + except KeyError: + print("error: menu selection is invalid!") + +if __name__ == "__main__": + + main() diff --git a/Student/Ruohan/lesson08/assignment08/scr/utilities.py b/Student/Ruohan/lesson08/assignment08/scr/utilities.py new file mode 100644 index 0000000..15b1079 --- /dev/null +++ b/Student/Ruohan/lesson08/assignment08/scr/utilities.py @@ -0,0 +1,43 @@ +""" +enable easy and controllable logging +""" + +import logging +import logging.config + + +def configure_logger(name, log_path): + """ + generic logger + """ + logging.config.dictConfig({ + 'version': 1, + 'formatters': { + 'default': {'format': '%(asctime)s - %(levelname)s - %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S'} + }, + 'handlers': { + 'console': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'default', + 'stream': 'ext://sys.stdout' + }, + 'file': { + 'level': 'DEBUG', + 'class': 'logging.handlers.RotatingFileHandler', + 'formatter': 'default', + 'filename': log_path, + 'maxBytes': 1024, + 'backupCount': 3 + } + }, + 'loggers': { + 'default': { + 'level': 'DEBUG', + 'handlers': ['console', 'file'] + } + }, + 'disable_existing_loggers': False + }) + return logging.getLogger(name) + diff --git a/Student/Ruohan/lesson08/nosql_activity.zip b/Student/Ruohan/lesson08/nosql_activity.zip new file mode 100644 index 0000000..70a13bf Binary files /dev/null and b/Student/Ruohan/lesson08/nosql_activity.zip differ diff --git a/Student/Ruohan/lesson08/nosql_activity/.gitignore b/Student/Ruohan/lesson08/nosql_activity/.gitignore new file mode 100755 index 0000000..3e3b980 --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/.gitignore @@ -0,0 +1,116 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +.DS_Store + +#secrets +.config/ + +prof/ +.pytest_cache/ +.idea/ + +test_results/ +logs/ + +data/ + diff --git a/Student/Ruohan/lesson08/nosql_activity/README.md b/Student/Ruohan/lesson08/nosql_activity/README.md new file mode 100755 index 0000000..3068ecc --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/README.md @@ -0,0 +1,8 @@ + +[On GitLab now (no more updates here)](https://gitlab.com/uw-pyclass/nosql) + +# nosql + +python certificate nosql example + +All of the Python code is in the src directory. diff --git a/Student/Ruohan/lesson08/nosql_activity/src/__init__.py b/Student/Ruohan/lesson08/nosql_activity/src/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/Student/Ruohan/lesson08/nosql_activity/src/learn_data.py b/Student/Ruohan/lesson08/nosql_activity/src/learn_data.py new file mode 100755 index 0000000..a4f17f7 --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/src/learn_data.py @@ -0,0 +1,54 @@ +""" + Data for database demonstrations +""" + + +def get_furniture_data(): + """ + demonstration data + """ + + furniture_data = [ + { + 'product': 'Red couch', + 'description': 'Leather low back', + 'monthly_rental_cost': 12.99, + 'in_stock_quantity': 10 + }, + { + 'product_type': 'Couch', + 'color': 'Blue', + 'description': 'Cloth high back', + 'monthly_rental_cost': 9.99, + 'in_stock_quantity': 3 + }, + { + 'product_type': 'Coffee table', + 'color': 'Brown', + 'description': 'Plastic', + 'monthly_rental_cost': 2.50, + 'in_stock_quantity': 25 + }, + { + 'product_type': 'Couch', + 'color': 'Red', + 'description': 'Leather high back', + 'monthly_rental_cost': 15.99, + 'in_stock_quantity': 17 + }, + { + 'product_type': 'Recliner', + 'color': 'Blue', + 'description': 'Leather high back', + 'monthly_rental_cost': 19.99, + 'in_stock_quantity': 6 + }, + { + 'product_type': 'Chair', + 'color': 'Blue', + 'description': 'Plastic', + 'monthly_rental_cost': 1.00, + 'in_stock_quantity': 45 + } + ] + return furniture_data diff --git a/Student/Ruohan/lesson08/nosql_activity/src/learnnosql.py b/Student/Ruohan/lesson08/nosql_activity/src/learnnosql.py new file mode 100755 index 0000000..2cbaa5b --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/src/learnnosql.py @@ -0,0 +1,39 @@ +""" + +Integrated example for nosql databases + +""" + +import learn_data +import mongodb_script +import redis_script +import neo4j_script +import simple_script +import utilities + + +def showoff_databases(): + """ + Here we illustrate basic interaction with nosql databases + """ + + log = utilities.configure_logger('default', '../logs/nosql_dev.log') + + log.info("Mongodb example to use data from Furniture module, so get it") + furniture = learn_data.get_furniture_data() + + mongodb_script.run_example(furniture) + + log.info("Other databases use data embedded in the modules") + + redis_script.run_example() + neo4j_script.run_example() + simple_script.run_example(furniture) + + +if __name__ == '__main__': + """ + orchestrate nosql examples + """ + + showoff_databases() diff --git a/Student/Ruohan/lesson08/nosql_activity/src/login_database.py b/Student/Ruohan/lesson08/nosql_activity/src/login_database.py new file mode 100755 index 0000000..f45173b --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/src/login_database.py @@ -0,0 +1,84 @@ +""" + module that will login to the various demonstration databases consistently +""" + +import configparser +from pathlib import Path +import pymongo +import redis +from neo4j.v1 import GraphDatabase, basic_auth + +import utilities + +log = utilities.configure_logger('default', '../logs/login_databases_dev.log') +config_file = Path(__file__).parent.parent / '.config/config.ini' +config = configparser.ConfigParser() + + +def login_mongodb_cloud(): + """ + connect to mongodb and login + """ + + log.info('Here is where we use the connect to mongodb.') + log.info('Note use of f string to embed the user & password (from the tuple).') + try: + config.read(config_file) + user = config["mongodb_cloud"]["user"] + pw = config["mongodb_cloud"]["pw"] + + except Exception as e: + print(f'error: {e}') + + client = pymongo.MongoClient(f'mongodb://{user}:{pw}' + '@cluster0-shard-00-00-wphqo.mongodb.net:27017,' + 'cluster0-shard-00-01-wphqo.mongodb.net:27017,' + 'cluster0-shard-00-02-wphqo.mongodb.net:27017/test' + '?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin') + + return client + + +def login_redis_cloud(): + """ + connect to redis and login + """ + try: + config.read(config_file) + host = config["redis_cloud"]["host"] + port = config["redis_cloud"]["port"] + pw = config["redis_cloud"]["pw"] + + + except Exception as e: + print(f'error: {e}') + + log.info('Here is where we use the connect to redis.') + + try: + r = redis.StrictRedis(host=host, port=port, password=pw, decode_responses=True) + + except Exception as e: + print(f'error: {e}') + + return r + + +def login_neo4j_cloud(): + """ + connect to neo4j and login + + """ + + log.info('Here is where we use the connect to neo4j.') + log.info('') + + config.read(config_file) + + graphenedb_user = config["neo4j_cloud"]["user"] + graphenedb_pass = config["neo4j_cloud"]["pw"] + graphenedb_url = 'bolt://hobby-opmhmhgpkdehgbkejbochpal.dbs.graphenedb.com:24786' + driver = GraphDatabase.driver(graphenedb_url, + auth=basic_auth(graphenedb_user, graphenedb_pass)) + + return driver diff --git a/Student/Ruohan/lesson08/nosql_activity/src/mongodb_script.py b/Student/Ruohan/lesson08/nosql_activity/src/mongodb_script.py new file mode 100755 index 0000000..daeed3d --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/src/mongodb_script.py @@ -0,0 +1,66 @@ +""" + mongodb example +""" + +import pprint +import login_database +import utilities + +log = utilities.configure_logger('default', '../logs/mongodb_script.log') + + +def run_example(furniture_items): + """ + mongodb data manipulation + """ + + with login_database.login_mongodb_cloud() as client: + log.info('Step 1: We are going to use a database called dev') + log.info('But if it doesnt exist mongodb creates it') + db = client['dev'] + + log.info('And in that database use a collection called furniture') + log.info('If it doesnt exist mongodb creates it') + + furniture = db['furniture'] + + log.info('Step 2: Now we add data from the dictionary above') + furniture.insert_many(furniture_items) + + # make query to retrieve and print just the red products + log.info('Step 3 / 1: Find the products that are described as red') + red_query = {'color': 'Red'} + red_products = furniture.find_one(red_query) + + log.info('print red products') + pprint.pprint(red_products) + + # make query to retrieve and print just the couches + log.info('Step 3 / 2: Find coaches') + couch_query = {'product_type': 'Couch'} + couch_products = furniture.find_one(couch_query) + + log.info('print couches') + pprint.pprint(couch_products) + + + log.info('Step 5: Delete the blue couch (actually deletes all blue couches)') + furniture.remove({"product_type": {"$eq": "Couch"}}, {"color": {"$eq": "Blue"}}) + + log.info('Step 6: Check it is deleted with a query and print') + blue_couch_query = {'product_type': 'Couch', 'color': 'Blue'} + blue_couch_results = furniture.find_one(blue_couch_query) + print('The blue couch is deleted, print should show none:') + pprint.pprint(blue_couch_query) + + log.info('Step 7: Find multiple documents, iterate though the results and print') + + cursor = furniture.find({'monthly_rental_cost': {'$gte': 15.00}}).sort('monthly_rental_cost', 1) + print('Results of search') + log.info('Notice how we parse out the data from the document') + + for doc in cursor: + print(f"Cost: {doc['monthly_rental_cost']} product name: {doc['product']} Stock: {doc['in_stock_quantity']}") + + log.info('Step 8: Delete the collection so we can start over') + db.drop_collection('furniture') diff --git a/Student/Ruohan/lesson08/nosql_activity/src/neo4j_script.py b/Student/Ruohan/lesson08/nosql_activity/src/neo4j_script.py new file mode 100755 index 0000000..40fab5f --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/src/neo4j_script.py @@ -0,0 +1,138 @@ +""" + neo4j example +""" + + +import utilities +import login_database +import utilities +import random + +log = utilities.configure_logger('default', '../logs/neo4j_script.log') + + +def run_example(): + + log.info('Step 1: First, clear the entire database, so we can start over') + log.info("Running clear_all") + + driver = login_database.login_neo4j_cloud() + with driver.session() as session: + session.run("MATCH (n) DETACH DELETE n") + + log.info("Step 2: Add a few people and color") + + with driver.session() as session: + + log.info('Adding a few Person nodes and new Person for activity') + log.info('The cyph language is analagous to sql for neo4j') + names = [('Bob', 'Jones'), + ('Nancy', 'Cooper'), + ('Alice', 'Cooper'), + ('Fred', 'Barnes'), + ('Mary', 'Evans'), + ('Marie', 'Curie'), + ('Ellen', 'DeGeneres'), + ('Cara', 'Delevine'), + ] + for first, last in names: + cyph = "CREATE (n:Person {first_name:'%s', last_name: '%s'})" % (first, last) + session.run(cyph) + + # Add some colors + log.info("Adding some colors") + colors = [('Red'), + ('Yellow'), + ('White'), + ('Blue'), + ('Pink'), + ('Green'), + ('Brown'), + ] + for color in colors: + cyph = "CREAT (n:Color {color: '%s'})" % (color) + session.run(cyph) + + log.info("Step 3: Get all of people and color in the DB:") + cyph = """MATCH (p:Person) + RETURN p.first_name as first_name, p.last_name as last_name + """ + result = session.run(cyph) + print("People in database:") + for record in result: + print(record['first_name'], record['last_name']) + + cyph = """MATCH (p:Color) + RETURN p.color as color + """ + result = session.run(cyph) + print("Color in database:") + for record in result: + print(record['color']) + + #creat associations between Person and color + log.info('Step 4: Create some relationships') + log.info('Create associations between Person and Color') + #assume person has two favorate color, random seleced from colors + i1 = random.randrange(0, len(colors)) + i2 = random.randrange(0, len(colors)) + for first, last in names: + cypher = """ + MATCH (p1:Person {first_name: '%s', last_name: '%s'}) + CREAT (p1)-[favorate_color: FAVORATE_COLOR]->(p2:Color{color: '%s', '%s'}) + RETURN p1.first_name as first_name, p1.last_name as last_name, p2.color as color + """ % (first, last, color[i1], color[i2]) + result = session.run(cypher) + for record in result: + print(record['first_name'],' ',{record['last_name']},' favorate color are ', record['color'] ) + + log.info('Create associations among Person') + log.info("Bob Jones likes Alice Cooper, Fred Barnes and Marie Curie") + + for first, last in [("Alice", "Cooper"), + ("Fred", "Barnes"), + ("Marie", "Curie")]: + cypher = """ + MATCH (p1:Person {first_name:'Bob', last_name:'Jones'}) + CREATE (p1)-[friend:FRIEND]->(p2:Person {first_name:'%s', last_name:'%s'}) + RETURN p1 + """ % (first, last) + session.run(cypher) + + log.info("Step 5: Find all of Bob's friends") + cyph = """ + MATCH (bob {first_name:'Bob', last_name:'Jones'}) + -[:FRIEND]->(bobFriends) + RETURN bobFriends + """ + result = session.run(cyph) + print("Bob's friends are:") + for rec in result: + for friend in rec.values(): + print(friend['first_name'], friend['last_name']) + + log.info("Setting up Marie's friends") + + for first, last in [("Mary", "Evans"), + ("Alice", "Cooper"), + ('Fred', 'Barnes'), + ]: + cypher = """ + MATCH (p1:Person {first_name:'Marie', last_name:'Curie'}) + CREATE (p1)-[friend:FRIEND]->(p2:Person {first_name:'%s', last_name:'%s'}) + RETURN p1 + """ % (first, last) + + session.run(cypher) + + print("Step 6: Find all of Marie's friends?") + cyph = """ + MATCH (marie {first_name:'Marie', last_name:'Curie'}) + -[:FRIEND]->(friends) + RETURN friends + """ + result = session.run(cyph) + print("\nMarie's friends are:") + for rec in result: + for friend in rec.values(): + print(friend['first_name'], friend['last_name']) diff --git a/Student/Ruohan/lesson08/nosql_activity/src/redis_script.py b/Student/Ruohan/lesson08/nosql_activity/src/redis_script.py new file mode 100755 index 0000000..8443b05 --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/src/redis_script.py @@ -0,0 +1,71 @@ +""" + demonstrate use of Redis +""" + + +import login_database +import utilities + + +def run_example(): + """ + uses non-presistent Redis only (as a cache) + + """ + + log = utilities.configure_logger('default', '../logs/redis_script.log') + + try: + log.info('Step 1: connect to Redis') + r = login_database.login_redis_cloud() + log.info('Step 2: cache some data in Redis') + r.set('andy', 'andy@somewhere.com') + + log.info('Step 2: now I can read it') + email = r.get('andy') + log.info('But I must know the key') + log.info(f'The results of r.get: {email}') + + log.info('Step 3: cache more data in Redis') + r.set('pam', 'pam@anywhere.com') + r.set('fred', 'fred@fearless.com') + + #Add some customer data to the cache + log.info('Add some customer data to the cache') + r.set('Cara', '209-234-9878', '98023') + r.set('Ellen', '234-098-1234', '98072') + r.set('Suki', '298-987-3456', '98098') + r.set('Chris', '234-976-7765', '98091') + r.set('Nate', '298-785-3989', '98017') + r.set('Katy', '240-897-6656', '98055') + + #retrieve a zip code and then a phone number for a known customer + log.info('retrieve a zip code and then a phone number for Ellen') + print(f"Ellen's zipcode is {r.lindex('Ellen', 1)} and phone number is {r.lindex('Ellen', 0)}") + + log.info('Step 4: delete from cache') + r.delete('andy') + log.info(f'r.delete means andy is now: {email}') + + log.info( + 'Step 6: Redis can maintain a unique ID or count very efficiently') + r.set('user_count', 21) + r.incr('user_count') + r.incr('user_count') + r.decr('user_count') + result = r.get('user_count') + log.info('I could use this to generate unique ids') + log.info(f'Redis says 21+1+1-1={result}') + + log.info('Step 7: richer data for a SKU') + r.rpush('186675', 'chair') + r.rpush('186675', 'red') + r.rpush('186675', 'leather') + r.rpush('186675', '5.99') + + log.info('Step 8: pull some data from the structure') + cover_type = r.lindex('186675', 2) + log.info(f'Type of cover = {cover_type}') + + except Exception as e: + print(f'Redis error: {e}') diff --git a/Student/Ruohan/lesson08/nosql_activity/src/simple_script.py b/Student/Ruohan/lesson08/nosql_activity/src/simple_script.py new file mode 100755 index 0000000..163f679 --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/src/simple_script.py @@ -0,0 +1,113 @@ +""" +pickle etc +""" + +import pickle +import shelve +import csv +import json + +import pprint +import utilities + +log = utilities.configure_logger('default', '../logs/mongodb_script.log') + + +def run_example(furniture_items): + """ + various persistence and serialization scenarios + + """ + + def run_pickle(): + """ + Write and read with pickle + """ + log.info("\n\n====") + log.info('Step 1: Demonstrate persistence with pickle') + log.info('Write a pickle file with the furniture data') + + pickle.dump(furniture_items, open('../data/data.pkl', 'wb')) + + log.info('Step 2: Now read it back from the pickle file') + read_data = pickle.load(open('../data/data.pkl', 'rb')) + log.info('Step 3: Show that the write and read were successful') + assert read_data == furniture_items + log.info("and print the data") + pprint.pprint(read_data) + + def run_shelve(): + """ + write and read with shelve + + """ + log.info("\n\n====") + log.info("Step 4: Demonstrate working with shelve") + shelf_file = shelve.open('../data/shelve.dat') + log.info("Step 5: store data at key") + shelf_file['key'] = furniture_items + + log.info("Step 6: Now retrieve a COPY of data at key") + read_items = shelf_file['key'] + + log.info("Check it worked") + assert read_items == furniture_items + + log.info("And now print the copy") + pprint.pprint(read_items) + + log.info("Step 7: delete data stored at key to cleanup and close") + del shelf_file['key'] + shelf_file.close() + + def run_csv(): + """ + write and read a csv + """ + log.info("\n\n====") + peopledata = [ + ('John', 'second guitar', 117.45), + ('Paul', 'bass', 22.01), + ('George', 'lead guitar', 45.99), + ('Ringo', 'drume', 77.0), + ('Roger', 'vocals', 12.5), + ('Keith', 'drums', 6.25), + ('Pete', 'guitar', 0.1), + ('John', 'bass', 89.71) + ] + log.info("Step 8: Write csv file") + with open('../data/rockstars.csv', 'w') as people: + peoplewriter = csv.writer(people) + peoplewriter.writerow(peopledata) + + log.info("Step 9: Read csv file back") + with open('../data/rockstars.csv', 'r') as people: + people_reader = csv.reader(people, delimiter=',', quotechar='"') + for row in people_reader: + pprint.pprint(row) + + def run_json(): + log.info("\n\n====") + log.info("Step 10: Look at working with json data") + furniture = [{'product': 'Red couch','description': 'Leather low back'}, + {'product': 'Blue couch','description': 'Cloth high back'}, + {'product': 'Coffee table','description': 'Plastic'}, + {'product': 'Red couch','description': 'Leather high back'}] + + log.info("Step 11: Return json string from an object") + furniture_string = json.dumps(furniture) + + log.info("Step 12: Print the json") + pprint.pprint(furniture_string) + + log.info("Step 13: Returns an object from a json string representation") + furniture_object = json.loads(furniture_string) + log.info("Step 14: print the string") + pprint.pprint(furniture_object) + + run_pickle() + run_shelve() + run_csv() + run_json() + + return diff --git a/Student/Ruohan/lesson08/nosql_activity/src/utilities.py b/Student/Ruohan/lesson08/nosql_activity/src/utilities.py new file mode 100755 index 0000000..15b1079 --- /dev/null +++ b/Student/Ruohan/lesson08/nosql_activity/src/utilities.py @@ -0,0 +1,43 @@ +""" +enable easy and controllable logging +""" + +import logging +import logging.config + + +def configure_logger(name, log_path): + """ + generic logger + """ + logging.config.dictConfig({ + 'version': 1, + 'formatters': { + 'default': {'format': '%(asctime)s - %(levelname)s - %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S'} + }, + 'handlers': { + 'console': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'default', + 'stream': 'ext://sys.stdout' + }, + 'file': { + 'level': 'DEBUG', + 'class': 'logging.handlers.RotatingFileHandler', + 'formatter': 'default', + 'filename': log_path, + 'maxBytes': 1024, + 'backupCount': 3 + } + }, + 'loggers': { + 'default': { + 'level': 'DEBUG', + 'handlers': ['console', 'file'] + } + }, + 'disable_existing_loggers': False + }) + return logging.getLogger(name) +