Last active
October 27, 2015 20:47
-
-
Save psychemedia/f7385255f89137c503b5 to your computer and use it in GitHub Desktop.
Code Club week 7 code resources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from IPython.display import HTML | |
| import folium | |
| def inline_map(map): | |
| """ | |
| Embeds the HTML source of the map directly into the IPython notebook. | |
| This method will not work if the map depends on any files (json data). Also this uses | |
| the HTML5 srcdoc attribute, which may not be supported in all browsers. | |
| """ | |
| map._build_map() | |
| return HTML('<iframe srcdoc="{srcdoc}" style="width: 100%; height: 510px; border: none"></iframe>'.format(srcdoc=map.HTML.replace('"', '"'))) | |
| def embed_map(map, path="map.html"): | |
| """ | |
| Embeds a linked iframe to the map into the IPython notebook. | |
| Note: this method will not capture the source of the map into the notebook. | |
| This method should work for all maps (as long as they use relative urls). | |
| """ | |
| map.create_map(path=path) | |
| return HTML('<iframe src="files/{path}" style="width: 100%; height: 510px; border: none"></iframe>'.format(path=path)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import requests | |
| def getFoodRatingData(name,address): | |
| params={'name':name,'address':address} | |
| r=requests.get('http://api.ratings.food.gov.uk/Establishments', | |
| headers={"x-api-version":2}, | |
| params=params) | |
| return r | |
| def parseFoodRatingData(jdata): | |
| df=pd.DataFrame() | |
| for establishment in jdata['establishments']: | |
| info={} | |
| for item in ['BusinessName','FHRSID','PostCode','RatingValue','RatingDate']: | |
| info[item]= establishment[item] | |
| for item in establishment['geocode']: | |
| info[item]= establishment['geocode'][item] | |
| for item in establishment['scores']: | |
| info[item]= establishment['scores'][item] | |
| df=df.append(info,ignore_index=True) | |
| return df | |
| def getAndParseFoodRatingData(name,address): | |
| r=getFoodRatingData(name,address) | |
| jdata=json.loads(r.content) | |
| df=parseFoodRatingData(jdata) | |
| return df |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "metadata": { | |
| "name": "", | |
| "signature": "sha256:09c43f8363f52e1a5113ab06a87e0f67fc683c75835697259a2590319c07be41" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Code Club Week 7" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Regular Expressions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import re\n", | |
| "?re" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "txt= 'The cat sat on the mat 11 times.'\n", | |
| "pattern=re.compile(r'^The (\\w*)')\n", | |
| "pattern.findall(txt)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "txt= 'The cat sat on the mat 11 times.'\n", | |
| "pattern=re.compile(r'\\s(.at)')\n", | |
| "pattern.findall(txt)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "pattern2=re.compile(r'^The (\\w*) sat (.*) (\\d*) times\\.$')\n", | |
| "grouper=pattern2.match(txt)\n", | |
| "grouper.groups()" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "grouper.group(2)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "pattern2.match(txt).group(1)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "whatwhere=re.compile(r'^The (?P<what>\\w*) sat (?P<where>.*) (\\d*) times\\.$')\n", | |
| "whatwhere.match(txt).group('where')" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "whatwhere.match(txt).group(0)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "whatwhere.match(txt).group(3)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "whatwhere.match(txt).groups()" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "re.sub(r'The (\\w*) (.*) ([^\\s]*) ([\\d]{1,}.*)\\.',r'The \\3 \\2 \\1? Was it really \\4?',txt)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Time" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "from datetime import datetime\n", | |
| "\n", | |
| "#Time and datetimes can be represented as special datetime objects\n", | |
| "now=datetime.now()\n", | |
| "now" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "#We can index in using .microsecond, .second, .minute, .hour, .day, .month, .year\n", | |
| "now.microsecond" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "#We can format a date in a variety of ways..\n", | |
| "\n", | |
| "#Reference: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior\n", | |
| "\n", | |
| "now.strftime(\"%d/%m/%Y\")" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "now_sentence=now.strftime(\"%A, %B %d, %Y (week %W)\")\n", | |
| "now_sentence" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "#Shortcuts\n", | |
| "now_str=now.strftime(\"%F\")\n", | |
| "now_str" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "#Parse the date back\n", | |
| "datetime.strptime(now_str,\"%Y-%m-%d\")" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "datetime.strptime(now_sentence,\"%A, %B %d, %Y (week %W)\")" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "from dateutil.parser import parse\n", | |
| "?parse\n", | |
| "\n", | |
| "parse('11/2/13',dayfirst=True)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "parse('11-10-09',dayfirst=None,yearfirst=None)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "parse('January 22nd, 1983')" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "parse('January 22nd, 1983, 10:23am')" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Code Club Eats" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import pandas as pd" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "To grab a Google Spreadsheet file as a CSV file, use the URL pattern (SHEETNUMBER starts at 0):" | |
| ] | |
| }, | |
| { | |
| "cell_type": "raw", | |
| "metadata": {}, | |
| "source": [ | |
| "https://docs.google.com/spreadsheets/d/<KEY>/export?gid=SHEETNUMBER&format=csv" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "url='https://docs.google.com/a/okfn.org/spreadsheets/d/1M14S4hqG4F5P8H78VdOMMeoITOPBpVZEGoiCvXEFBQg/export?gid=0&format=csv'\n", | |
| "cceats=pd.read_csv(url)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "cceats" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Food Standards Agency - www.food.gov.uk\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import requests\n", | |
| "import json" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "#http://api.ratings.food.gov.uk/help\n", | |
| "#http://docs.python-requests.org/en/latest/user/quickstart/\n", | |
| "\n", | |
| "params={'name':\"McDonald's\",'address':'SW12 9AU'}\n", | |
| "r=requests.get('http://api.ratings.food.gov.uk/Establishments',\n", | |
| " headers={\"x-api-version\":2},\n", | |
| " params=params)\n", | |
| "j=json.loads(r.content)\n", | |
| "j" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def getFoodRatingData(name,address):\n", | |
| " params={'name':name,'address':address}\n", | |
| " r=requests.get('http://api.ratings.food.gov.uk/Establishments',\n", | |
| " headers={\"x-api-version\":2},\n", | |
| " params=params)\n", | |
| " return r\n", | |
| "\n", | |
| "def parseFoodRatingData(jdata):\n", | |
| " df=pd.DataFrame()\n", | |
| " for establishment in jdata['establishments']:\n", | |
| " info={}\n", | |
| " for item in ['BusinessName','FHRSID','PostCode','RatingValue','RatingDate']:\n", | |
| " info[item]= establishment[item] \n", | |
| " for item in establishment['geocode']:\n", | |
| " info[item]= establishment['geocode'][item] \n", | |
| " for item in establishment['scores']:\n", | |
| " info[item]= establishment['scores'][item] \n", | |
| " df=df.append(info,ignore_index=True)\n", | |
| " return df\n", | |
| "\n", | |
| "def getAndParseFoodRatingData(name,address):\n", | |
| " r=getFoodRatingData(name,address)\n", | |
| " jdata=json.loads(r.content)\n", | |
| " df=parseFoodRatingData(jdata)\n", | |
| " return df" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "parseFoodRatingData(j)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "getAndParseFoodRatingData('Sacro Cuore','NW10 3NB')" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "adf=pd.DataFrame()\n", | |
| "for place in cceats.iterrows():\n", | |
| " adf=adf.append(getAndParseFoodRatingData(place[1]['Name'],place[1]['Postcode']))\n", | |
| "adf[['FHRSID','BusinessName','PostCode','RatingDate','RatingValue','latitude','longitude',\n", | |
| " 'Structural','Hygiene','ConfidenceInManagement']]" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Mapping" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "#!pip install folium\n", | |
| "import folium" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "from IPython.display import HTML\n", | |
| "import folium\n", | |
| "\n", | |
| "def inline_map(map):\n", | |
| " \"\"\"\n", | |
| " Embeds the HTML source of the map directly into the IPython notebook.\n", | |
| " \n", | |
| " This method will not work if the map depends on any files (json data). Also this uses\n", | |
| " the HTML5 srcdoc attribute, which may not be supported in all browsers.\n", | |
| " \"\"\"\n", | |
| " map._build_map()\n", | |
| " return HTML('<iframe srcdoc=\"{srcdoc}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(srcdoc=map.HTML.replace('\"', '"')))\n", | |
| "\n", | |
| "def embed_map(map, path=\"map.html\"):\n", | |
| " \"\"\"\n", | |
| " Embeds a linked iframe to the map into the IPython notebook.\n", | |
| " \n", | |
| " Note: this method will not capture the source of the map into the notebook.\n", | |
| " This method should work for all maps (as long as they use relative urls).\n", | |
| " \"\"\"\n", | |
| " map.create_map(path=path)\n", | |
| " return HTML('<iframe src=\"files/{path}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(path=path))" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "ccd=pd.read_csv('/Users/ajh59/Downloads/Code-Club-s-Fave-Restaurants-Sheet1-csv.csv')\n", | |
| "ccd" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "fmap=folium.Map(location=[51.5, 0], zoom_start=9)\n", | |
| "\n", | |
| "for row in ccd.iterrows():\n", | |
| " latlon = [ row[1]['latitude'], row[1]['longitude'] ]\n", | |
| " fmap.simple_marker( latlon )\n", | |
| "inline_map(fmap)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "fmap=folium.Map(location=[51.5, 0], zoom_start=9)\n", | |
| "\n", | |
| "for row in adf.iterrows():\n", | |
| " latlon = [ row[1]['latitude'], row[1]['longitude'] ]\n", | |
| " fmap.simple_marker( latlon, clustered_marker=True,\n", | |
| " popup='Name: {name}<br/>Score: {score}'.format(name=row[1]['BusinessName'],\n", | |
| " score=row[1]['RatingValue']) )\n", | |
| "inline_map(fmap)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [], | |
| "language": "python", | |
| "metadata": { | |
| "activity": false | |
| }, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [] | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment