Skip to content

Instantly share code, notes, and snippets.

View signed0's full-sized avatar

Nathan Villaescusa signed0

View GitHub Profile
@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: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: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 / 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 / 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 / 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