Commits

Matt Bone committed f2bd9fd

some more examples

  • Participants
  • Parent commits 9aacb28

Comments (0)

Files changed (2)

File Sample Project.ipynb

+{
+ "metadata": {
+  "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+  {
+   "cells": [
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "import requests"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "crime_data = requests.get(\"https://data.cityofchicago.org/api/views/ijzp-q8t2/rows.json?accessType=DOWNLOAD\")\n",
+      "cta_data = request.get(\"https://data.cityofchicago.org/api/views/w8km-9pzd/rows.json?accessType=DOWNLOAD\")"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    }
+   ],
+   "metadata": {}
+  }
+ ]
+}

File XML Programming.ipynb

      "collapsed": false,
      "input": [
       "import json\n",
-      "import xml.etree.ElementTree as ET"
+      "import xml.etree.ElementTree as ET\n",
+      "import xml.sax\n",
+      "import xml.sax.handler"
      ],
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 5
+     "prompt_number": 12
     },
     {
      "cell_type": "code",
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 6
+     "prompt_number": 2
     },
     {
      "cell_type": "code",
        ]
       }
      ],
-     "prompt_number": 7
+     "prompt_number": 3
     },
     {
      "cell_type": "markdown",
       {
        "metadata": {},
        "output_type": "pyout",
-       "prompt_number": 8,
+       "prompt_number": 4,
        "text": [
         "[{'end_page': 5,\n",
         "  'journal': 'Journal of Article Greatness',\n",
        ]
       }
      ],
-     "prompt_number": 8
+     "prompt_number": 4
     },
     {
      "cell_type": "markdown",
        ]
       }
      ],
-     "prompt_number": 18
+     "prompt_number": 5
     },
     {
      "cell_type": "markdown",
       {
        "metadata": {},
        "output_type": "pyout",
-       "prompt_number": 19,
+       "prompt_number": 6,
        "text": [
-        "[<Element 'article' at 0x2685c90>]"
+        "[<Element 'article' at 0x2b37c50>]"
        ]
       }
      ],
-     "prompt_number": 19
+     "prompt_number": 6
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "Now let's look at some "
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "article_xml = ET.tostring(articles)"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 8
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "class MyHandler(xml.sax.handler.ContentHandler):\n",
+      "    \n",
+      "    def startDocument(self):\n",
+      "        print \"start document\"\n",
+      "    \n",
+      "    def startElement(self, name, attrs):\n",
+      "        print \"start element: {}\".format(name)\n",
+      "        if len(attrs):\n",
+      "            for key, value in attrs.items():\n",
+      "                print \"    {}: {}\".format(key, value)\n",
+      "        \n",
+      "    def endElement(self, name):\n",
+      "        print \"end element: {}\".format(name)\n",
+      "    \n",
+      "    def endDocument(self):\n",
+      "        print \"end document\""
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 22
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "xml.sax.parseString(article_xml, MyHandler())"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "start document\n",
+        "start element: articles\n",
+        "start element: article\n",
+        "    journal: Journal of Article Greatness\n",
+        "    title: My Great Article\n",
+        "    start_page: 4\n",
+        "    end_page: 5\n",
+        "end element: article\n",
+        "start element: article\n",
+        "    journal: Journal of Article Greatness\n",
+        "    title: My Second Great Article\n",
+        "    start_page: 8\n",
+        "    end_page: 9\n",
+        "end element: article\n",
+        "end element: articles\n",
+        "end document\n"
+       ]
+      }
+     ],
+     "prompt_number": 23
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "Sax handlers are used for their side-effects."
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "class CountElementHandler(xml.sax.handler.ContentHandler):\n",
+      "    element_count = 0\n",
+      "    \n",
+      "    def startElement(self, name, attrs):\n",
+      "        self.element_count += 1"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 24
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "handler = CountElementHandler()\n",
+      "xml.sax.parseString(article_xml, handler)\n",
+      "print handler.element_count"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "3\n"
+       ]
+      }
+     ],
+     "prompt_number": 25
     },
     {
      "cell_type": "code",