This file contains 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
# 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) |
This file contains 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 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) |
This file contains 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 http://code.activestate.com/recipes/363602-lazy-property-evaluation/ | |
Usage: | |
class MyClass(): | |
@lazyloaded | |
def config(self): | |
return {'yay': 'nay'} |
This file contains 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
def pairs(iterable): | |
'''Generate pairs of items in iterable''' | |
i = iter(iterable) | |
prev = i.next() | |
for j in i: | |
yield prev, j | |
prev = j |
This file contains 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 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)) | |
This file contains 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 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 |
NewerOlder