Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My submission of the bitly: https://gist.github.com/SeanOC/27d0816861d740d3b9a5 in an IPython Notebook. Please view it here: http://nbviewer.ipython.org/gist/agconti/27da2d147a3a6de66aea
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Bitly Test - Andrew Conti"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"1. Text Blocking"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def text_blocking(text_array):\n",
" output = []\n",
" text = ''\n",
" for char in range(0, len(text_array[0])): \n",
" for text_element in range(0, len(text_array)):\n",
" text += text_array[text_element][char]\n",
" output.append(text)\n",
" text = \"\" \n",
" return output\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"text_array = [\"AAA\",\n",
" \"BBB\",\n",
" \"CCC\"]\n",
"text_blocking(text_array)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"['ABC', 'ABC', 'ABC']"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"text_array = [\"AAAAAAAAAAAAA\"]\n",
"text_blocking(text_array)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"text_array = [\"A\",\n",
" \"A\",\n",
" \"A\",\n",
" \"A\",\n",
" \"A\"]\n",
"text_blocking(text_array)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"['AAAAA']"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"2. Race Average"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import datetime\n",
"\n",
"\n",
"class RaceAverage(object):\n",
" ''' Calculates the average times of competitors in a sailboat race.'''\n",
" def __init__(self, start_time):\n",
" self.start_time = self.parse_time(start_time)\n",
"\n",
" def parse_time(self, time):\n",
" ''' Parses input to datetime objects. '''\n",
" time, day = time.split(', DAY ')\n",
" date = datetime.datetime.strptime(time, \"%I:%M %p\")\n",
" date += datetime.timedelta(days=(int(day) - 1))\n",
" return date\n",
"\n",
" def completion_time(self, date):\n",
" ''' Calculates race completion time in minutes'''\n",
" race_duration = date - self.start_time\n",
" return race_duration.total_seconds() / 60\n",
"\n",
" def avgMinutes(self, times):\n",
" '''\n",
" Calculates the average number of minutes taken by\n",
" the competitors to complete the race. Times are\n",
" rounded up from 0.5.\n",
" '''\n",
" race_completion_times = []\n",
" for time in times:\n",
" finish_time = self.parse_time(time)\n",
" race_completion_times.append(self.completion_time(finish_time))\n",
" average_time = int(round(sum(race_completion_times) / len(race_completion_times)))\n",
" return average_time\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"race_average = RaceAverage('08:00 AM, DAY 1')\n",
"times = [\"12:00 PM, DAY 1\",\n",
" \"12:01 PM, DAY 1\"]\n",
"race_average.avgMinutes(times)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"241"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"race_average = RaceAverage('08:00 AM, DAY 1')\n",
"times = [\"02:00 PM, DAY 19\",\n",
" \"02:00 PM, DAY 20\",\n",
" \"01:58 PM, DAY 20\"]\n",
"race_average.avgMinutes(times)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"27239"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"3. Hot Phrases "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import json\n",
"import urllib\n",
"import urllib2\n",
"\n",
"\n",
"class Service(object):\n",
" def __init__(self, host, endpoints, params):\n",
" self.host = host\n",
" self.endpoints = endpoints\n",
" self.params = params\n",
"\n",
" def generate_url(self, endpoint):\n",
" self.url = self.host + endpoint\n",
"\n",
" def create_request(self, **kwargs):\n",
" self.request = urllib2.Request(self.url + \"?\" + urllib.urlencode(self.params))\n",
"\n",
" def get_data_from_api(self, endpoint):\n",
" self.generate_url(endpoint)\n",
" self.create_request()\n",
" self.response = urllib2.urlopen(self.request).read()\n",
"\n",
" def pretty_print_response(self, data):\n",
" print json.dumps(self.response, sort_keys=True, indent=4, separators=(',', ': '))\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class HotPhrases(Service):\n",
" \n",
" def parse_hot_phrases(self): \n",
" data = json.loads(self.response)['data']['phrases']\n",
" phrases = [x[\"phrase\"] for x in data]\n",
" return phrases\n",
" \n",
" def get_hot_phrases(self, endpoint, limit):\n",
" self.get_data_from_api(endpoint)\n",
" phrases = self.parse_hot_phrases()\n",
" for phrase in phrases[:limit]:\n",
" print phrase\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"service_parameters = {\n",
" \"host\": 'https://api-ssl.bitly.com',\n",
" \"endpoints\": ['/v3/realtime/hot_phrases'],\n",
" \"params\": {\n",
" \"access_token\": \"a2c09b3944b5e02f50ad49da64e0abe7280f8d95\"\n",
" }\n",
"}\n",
"hot_phrases = HotPhrases(**service_parameters)\n",
"hot_phrases.get_hot_phrases(hot_phrases.endpoints[0], 5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"nfl draft\n",
"conchita wurst\n",
"openly gay player\n",
"openly gay\n",
"louis rams\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"4. High Value Links"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class HighValue(Service):\n",
" def parse_high_value_links(self):\n",
" self.high_value_links = json.loads(self.response)['data']['values']\n",
"\n",
" def get_high_value_links(self, endpoint):\n",
" self.get_data_from_api(endpoint)\n",
" self.parse_high_value_links()\n",
"\n",
" def get_link_clicks(self, endpoint, link):\n",
" ''' Gets number of clicks for a single link '''\n",
" self.params['link'] = link\n",
" self.get_data_from_api(endpoint)\n",
" return json.loads(self.response)['data']['link_clicks']\n",
"\n",
" def get_high_value_links_clicks(self, endpoint):\n",
" ''' Gets number of clicks for a list of links '''\n",
" self.high_value_links_clicks = [self.get_link_clicks(endpoint, i) for i in self.high_value_links]\n",
"\n",
" def show_high_value_links(self, link_endpoint, click_endpoint):\n",
" self.get_high_value_links(link_endpoint)\n",
" self.get_high_value_links_clicks(click_endpoint)\n",
" high_value_link_list = zip(self.high_value_links, self.high_value_links_clicks)\n",
" for link in high_value_link_list:\n",
" print \"%s - %s\" % link\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"service_parameters = {\n",
" \"host\": 'https://api-ssl.bitly.com',\n",
" \"endpoints\": ['/v3/realtime/hot_phrases',\n",
" '/v3/highvalue',\n",
" '/v3/link/clicks'],\n",
" \"params\": {\n",
" \"access_token\": \"a2c09b3944b5e02f50ad49da64e0abe7280f8d95\",\n",
" \"limit\": 10\n",
" }\n",
"}\n",
"high_value = HighValue(**service_parameters)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"high_value.show_high_value_links(high_value.endpoints[1], high_value.endpoints[2])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"http://bit.ly/PbKEGW - 4039\n",
"http://bit.ly/1iA2xME - 141619\n",
"http://bit.ly/1jpwHli - 170\n",
"http://bit.ly/1fPLHJo - 10996\n",
"http://bit.ly/Rq6wjz - 981\n",
"http://bit.ly/1jItTzc - 230\n",
"http://bit.ly/1fBuiEg - 2122\n",
"http://bit.ly/QhFP0q - 192642\n",
"http://bit.ly/1kVu5rR - 225\n",
"http://bit.ly/1qfP94u - 58901\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 13
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment