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 |
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
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 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
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
# 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
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'), |
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 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() |
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
'''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. | |
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 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: |
OlderNewer