Skip to content

Instantly share code, notes, and snippets.

View signed0's full-sized avatar

Nathan Villaescusa signed0

View GitHub Profile
@signed0
signed0 / log_time.py
Created February 1, 2012 19:45
Execution time decorator
'''From http://www.daniweb.com/software-development/python/code/216610
Modified to use logging instead of print statments
Usage:
@log_time
def my_function():
pass
'''
import logging
@signed0
signed0 / geographic_length.py
Created February 2, 2012 21:57
Geographic Linestring Length
from math import sin, cos, sqrt, asin, radians
RADIUS_EARTH_M = 6371200.0 #The Earth's mean radius in meters
def geographic_length(coords):
'''Returns the length of a linestring in meters'''
coords = ((radians(x), radians(y)) for x, y in coords)
rad_dist = sum(_haversine_distance(*c) for c in pairs(coords))
@signed0
signed0 / pairs.py
Created February 2, 2012 22:03
Pairs Iterator
def pairs(iterable):
'''Generate pairs of items in iterable'''
i = iter(iterable)
prev = i.next()
for j in i:
yield prev, j
prev = j
@signed0
signed0 / gist:1731010
Created February 3, 2012 16:38
Lazy loding method decorator
''''
From http://code.activestate.com/recipes/363602-lazy-property-evaluation/
Usage:
class MyClass():
@lazyloaded
def config(self):
return {'yay': 'nay'}
@signed0
signed0 / gist:1741823
Created February 5, 2012 01:35
Cut a linestring at a distance
from math import sqrt
def cut(coords, distance):
'''Splits a cartesian linestring at a given distance
Returns None for either part if the length is 0
'''
if distance <= 0:
return None, list(tuple(j) for j in coords)
@signed0
signed0 / gist:1787885
Created February 10, 2012 08:54
Python natural sort key
# Inspired by http://code.activestate.com/recipes/285264-natural-string-sorting/
import re
digits = re.compile(r'(\d+|\D+)')
def natural_sort_key(item):
if item is None or item == '':
return None
pieces = digits.findall(item)
@signed0
signed0 / gist:1829916
Created February 14, 2012 20:20
Human readable date range
def daterange(v0, v1):
assert v0 <= v1
if v0 == v1:
return v0.strftime('%b %d, %Y')
if v0.year == v1.year:
if v0.month == v1.month:
parts = (v0.strftime('%b %d'),
v1.strftime('%d'),
@signed0
signed0 / gist:1830094
Created February 14, 2012 20:28
Human readable time fields
from datetime import datetime
def datetime_format(value):
'''Provides pretty datetime strings
The amount of information will change depending on how long ago the value is from now
If the value is today then just the time will be returned
If the value is this year then the year will be left off
'''
now = datetime.utcnow()
@signed0
signed0 / gist:2031157
Created March 13, 2012 19:53
Google Polyline encoder & decoder
'''Provides utility functions for encoding and decoding linestrings using the
Google encoded polyline algorithm.
'''
def encode_coords(coords):
'''Encodes a polyline using Google's polyline algorithm
See http://code.google.com/apis/maps/documentation/polylinealgorithm.html
for more information.
@signed0
signed0 / gist:2240930
Created March 29, 2012 17:57
Splits a list of items into chunks of length n
from itertools import islice
def batch(items, n):
'''Splits a list of items into chunks of length n'''
i = iter(items)
while True:
items = tuple(islice(i, n))
if len(items) == 0: