Commits

Matt Bone committed 5856a70

latest

Comments (0)

Files changed (1)

database_examples.ipynb

      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 1
+     "prompt_number": 20
     },
     {
      "cell_type": "code",
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 2
+     "prompt_number": 21
     },
     {
      "cell_type": "code",
       {
        "metadata": {},
        "output_type": "pyout",
-       "prompt_number": 3,
+       "prompt_number": 22,
        "text": [
-        "<sqlite3.Cursor at 0x17e1810>"
+        "<sqlite3.Cursor at 0x26cfc00>"
        ]
       }
      ],
-     "prompt_number": 3
+     "prompt_number": 22
     },
     {
      "cell_type": "code",
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 4
+     "prompt_number": 23
     },
     {
      "cell_type": "code",
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 5
+     "prompt_number": 24
     },
     {
      "cell_type": "code",
       {
        "metadata": {},
        "output_type": "pyout",
-       "prompt_number": 6,
+       "prompt_number": 25,
        "text": [
-        "<sqlite3.Cursor at 0x17e1810>"
+        "<sqlite3.Cursor at 0x26cfc00>"
        ]
       }
      ],
-     "prompt_number": 6
+     "prompt_number": 25
     },
     {
      "cell_type": "code",
       {
        "metadata": {},
        "output_type": "pyout",
-       "prompt_number": 7,
+       "prompt_number": 26,
        "text": [
-        "<sqlite3.Cursor at 0x17e1810>"
+        "<sqlite3.Cursor at 0x26cfc00>"
        ]
       }
      ],
-     "prompt_number": 7
+     "prompt_number": 26
     },
     {
      "cell_type": "code",
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 8
+     "prompt_number": 27
     },
     {
      "cell_type": "markdown",
        "output_type": "pyout",
        "prompt_number": 9,
        "text": [
-        "[{'age': 29, 'id': 1, 'name': 'matt'}, {'age': 25, 'id': 3, 'name': 'same'}]"
+        "[{'age': 29, 'id': 1, 'name': 'matt'}, {'age': 25, 'id': 3, 'name': 'sam'}]"
        ]
       }
      ],
     {
      "cell_type": "code",
      "collapsed": false,
+     "input": [
+      "from itertools import product\n",
+      "students_and_grades = list(product(student_data, grade_data))"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 28
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "students_and_grades"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 29,
+       "text": [
+        "[({'age': 29, 'id': 1, 'name': 'matt'},\n",
+        "  {'course': 'markup', 'grade': '85', 'id': 1, 'student_id': 1}),\n",
+        " ({'age': 29, 'id': 1, 'name': 'matt'},\n",
+        "  {'course': 'compilers', 'grade': '77', 'id': 2, 'student_id': 1}),\n",
+        " ({'age': 29, 'id': 1, 'name': 'matt'},\n",
+        "  {'course': 'markup', 'grade': '90', 'id': 3, 'student_id': 2}),\n",
+        " ({'age': 29, 'id': 1, 'name': 'matt'},\n",
+        "  {'course': 'compilers', 'grade': '95', 'id': 4, 'student_id': 2}),\n",
+        " ({'age': 29, 'id': 1, 'name': 'matt'},\n",
+        "  {'course': 'markup', 'grade': '85', 'id': 5, 'student_id': 3}),\n",
+        " ({'age': 29, 'id': 1, 'name': 'matt'},\n",
+        "  {'course': 'operating systems', 'grade': '82', 'id': 6, 'student_id': 3}),\n",
+        " ({'age': 33, 'id': 2, 'name': 'alice'},\n",
+        "  {'course': 'markup', 'grade': '85', 'id': 1, 'student_id': 1}),\n",
+        " ({'age': 33, 'id': 2, 'name': 'alice'},\n",
+        "  {'course': 'compilers', 'grade': '77', 'id': 2, 'student_id': 1}),\n",
+        " ({'age': 33, 'id': 2, 'name': 'alice'},\n",
+        "  {'course': 'markup', 'grade': '90', 'id': 3, 'student_id': 2}),\n",
+        " ({'age': 33, 'id': 2, 'name': 'alice'},\n",
+        "  {'course': 'compilers', 'grade': '95', 'id': 4, 'student_id': 2}),\n",
+        " ({'age': 33, 'id': 2, 'name': 'alice'},\n",
+        "  {'course': 'markup', 'grade': '85', 'id': 5, 'student_id': 3}),\n",
+        " ({'age': 33, 'id': 2, 'name': 'alice'},\n",
+        "  {'course': 'operating systems', 'grade': '82', 'id': 6, 'student_id': 3}),\n",
+        " ({'age': 25, 'id': 3, 'name': 'sam'},\n",
+        "  {'course': 'markup', 'grade': '85', 'id': 1, 'student_id': 1}),\n",
+        " ({'age': 25, 'id': 3, 'name': 'sam'},\n",
+        "  {'course': 'compilers', 'grade': '77', 'id': 2, 'student_id': 1}),\n",
+        " ({'age': 25, 'id': 3, 'name': 'sam'},\n",
+        "  {'course': 'markup', 'grade': '90', 'id': 3, 'student_id': 2}),\n",
+        " ({'age': 25, 'id': 3, 'name': 'sam'},\n",
+        "  {'course': 'compilers', 'grade': '95', 'id': 4, 'student_id': 2}),\n",
+        " ({'age': 25, 'id': 3, 'name': 'sam'},\n",
+        "  {'course': 'markup', 'grade': '85', 'id': 5, 'student_id': 3}),\n",
+        " ({'age': 25, 'id': 3, 'name': 'sam'},\n",
+        "  {'course': 'operating systems', 'grade': '82', 'id': 6, 'student_id': 3})]"
+       ]
+      }
+     ],
+     "prompt_number": 29
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "[(student['name'], grade['grade']) for (student, grade) in students_and_grades if student['id'] == grade['student_id'] and student['age'] < 30]"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 15,
+       "text": [
+        "[('matt', '85'), ('matt', '77'), ('sam', '85'), ('sam', '82')]"
+       ]
+      }
+     ],
+     "prompt_number": 15
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "from jinja2 import Template"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 16
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "example_template = Template(\"\"\"\\\n",
+      "{% for student, grades in students_and_grades %}\n",
+      "<student name=\"{{ student['name'] }}\">\n",
+      "    <grades>\n",
+      "{% for grade in grades %}\n",
+      "  <grade>{{ grade['course'] }}: {{ grade['grade'] }}</gre>\n",
+      "{% endfor %}\n",
+      "<grades>\n",
+      "</student>\n",
+      "{% endfor %}\n",
+      "\"\"\")\n",
+      "\n",
+      "grouped_grades = [(student, [grade for grade in grade_data if student['id'] == grade['student_id']]) for student in student_data]\n",
+      "rendered = example_template.render(students_and_grades=grouped_grades)\n",
+      "print rendered"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "\n",
+        "<student name=\"matt\">\n",
+        "    <grades>\n",
+        "\n",
+        "  <grade>markup: 85</gre>\n",
+        "\n",
+        "  <grade>compilers: 77</gre>\n",
+        "\n",
+        "<grades>\n",
+        "</student>\n",
+        "\n",
+        "<student name=\"alice\">\n",
+        "    <grades>\n",
+        "\n",
+        "  <grade>markup: 90</gre>\n",
+        "\n",
+        "  <grade>compilers: 95</gre>\n",
+        "\n",
+        "<grades>\n",
+        "</student>\n",
+        "\n",
+        "<student name=\"sam\">\n",
+        "    <grades>\n",
+        "\n",
+        "  <grade>markup: 85</gre>\n",
+        "\n",
+        "  <grade>operating systems: 82</gre>\n",
+        "\n",
+        "<grades>\n",
+        "</student>\n",
+        "\n"
+       ]
+      }
+     ],
+     "prompt_number": 34
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "list(product(['a', 'b'], [1, 2]))"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 30,
+       "text": [
+        "[('a', 1), ('a', 2), ('b', 1), ('b', 2)]"
+       ]
+      }
+     ],
+     "prompt_number": 30
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
      "input": [],
      "language": "python",
      "metadata": {},