Skip to content

Instantly share code, notes, and snippets.

@jimr
jimr / find_a_ride.py
Last active August 29, 2015 14:03
Get NextBike rental station numbers for Bath. I'm guessing about the time zone data for when the feed was updated (no docs that I can find).
import datetime
import sys
import urllib2
import xml.etree.ElementTree as ET
url = "https://nextbike.net/maps/nextbike-live.xml"
stations = sys.argv[1:]
xml = urllib2.urlopen(url).read()
root = ET.fromstring(xml)
@jimr
jimr / coord_cleaner.py
Last active August 29, 2015 14:04
Helper function for cleaning up user input geographic coordinates. Call get_coords(crappy_x, crappy_y) to get cleaned coordinates. No guarantees, check your results, etc.
def clean_point(point):
# junk
point = point.strip()
junk = [u'\ufeff', ' ', u'\xa0', u'\u2013'] # [, ,  , –]
for symbol in junk:
point = point.replace(symbol, '')
point = point.lstrip('+')
point = point.lstrip("'") # leading quote chars to fool excel
point = point.lstrip(',')
@jimr
jimr / email_finder.py
Created July 21, 2014 08:40
Good Enough(TM) email address finder
import re
pattern = re.compile(
r'(mailto:)?(?P<email>[^(\s|<|\[))]*@[^\s]*\.[^(\s|>|;|,|\])]*)'
)
test_data = '''
This is a@b.com string with some Email Addresses
<email@addresses.com> in it. There are also
some mailto:test@example.com addresses in here too.
@jimr
jimr / locations.py
Last active August 29, 2015 14:11
Example flickr.py usage for getting user locations
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import flickr
flickr.API_KEY = '<your API key>'
flickr.API_SECRET = '<your API secret>'
# get some photos
photos = flickr.photos_search(tags='manhattan', page='1', per_page='10')
@jimr
jimr / pagination.py
Created December 9, 2014 14:56
flickr.py search pagination usage example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import flickr
flickr.API_KEY = '<your API key>'
flickr.API_SECRET = '<your API secret>'
page = 1
per_page = 10
@jimr
jimr / db_size_check.sql
Created September 16, 2015 13:52
Show the top 25 MySQL database tables by size on disk
SELECT
CONCAT(table_schema, '.', table_name) as 'table',
CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows,
CONCAT(ROUND(data_length / (1024 * 1024 * 1024), 2), 'G') data,
CONCAT(ROUND(index_length / (1024 * 1024 * 1024), 2), 'G') idx,
CONCAT(ROUND((data_length + index_length) / (1024 * 1024 * 1024), 2), 'G') total_size,
ROUND(index_length / data_length, 2) idxfrac
FROM information_schema.TABLES
-- WHERE table_schema = 'some_db'
ORDER BY data_length + index_length DESC
@jimr
jimr / line_diff.py
Last active December 15, 2015 18:19
Python script for finding the first difference between two lines. Useful for (e.g.) diffing minified JS. Usage: git diff -p some/minified/file.js | grep "^[+-]" | tail -n 2 | python diff.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import fileinput
lines = list(fileinput.input())
if len(lines) != 2:
raise Exception("Input must be a pair of lines to diff")
@jimr
jimr / gist:5370940
Created April 12, 2013 09:54
Django test suite discovery that works with `python manage.py test` and `django-jenkins`. If you want to put your tests in a folder instead of `tests.py`, you need to define a `suite` in your `models.py` as below.
# put this in your_app/models.py
def suite():
"""Django test discovery."""
import nose
import unittest
path = os.path.join(os.path.dirname(__file__), 'tests')
suite = unittest.TestSuite()
suite.addTests(nose.loader.TestLoader().loadTestsFromDir(path))
return suite
@jimr
jimr / ical_print.py
Created May 13, 2013 09:27
Pretty formatting of meeting invites (for use in mutt etc).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# To parse ics attachments in mutt, put this in your ~/.mailcap:
#
# text/calendar; /path/to/ical_print.py; copiousoutput
#
# Otherwise, you can either pass a an .ics file as a parameter, or pipe
# something to it on stdin.
#
@jimr
jimr / shape_merge.py
Created August 15, 2013 14:35
Merge zipped shapefiles containing identical (or very similar) attribute tables.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Requires pyshp: https://pypi.python.org/pypi/pyshp
#
import os
import shapefile
import shutil
import tempfile