forked from data-for-change/anyway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
108 lines (89 loc) · 2.89 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from __future__ import print_function
from csv import DictReader
from datetime import datetime
from dateutil.relativedelta import relativedelta
from flask import Flask
import pyproj
import threading
import sys
import re
def init_flask(name):
"""
initializes a Flask instance with default values
:param name: the name of the instance
"""
app = Flask(name)
app.config.from_object('config')
return app
class ProgressSpinner(object):
def __init__(self):
self.counter = 0
self.chars = ['|', '/', '-', '\\']
def show(self):
"""
prints a rotating spinner
"""
current_char = self.counter % len(self.chars)
sys.stderr.write("\r%s" % self.chars[current_char])
self.counter += 1
class CsvReader(object):
"""
loads and handles csv files
"""
_digit_pattern = re.compile('^-?\d*(\.\d+)?$')
def __init__(self, filename):
self._file = open(filename)
self._lock = threading.RLock()
self._closed = False
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
with self._lock:
if not self._closed:
self._file.close()
def name(self):
"""
the filename of the csv file
:return:
"""
return self._file.name
def close(self):
with self._lock:
if not self._closed:
self._file.close()
def _convert(self, value):
"""
converts an str value to a typed one
"""
if value == '' or value is None:
return None
# the isdigit function doesn't match negative numbers
if CsvReader._digit_pattern.match(value):
return int(float(value))
return value
def __iter__(self):
for line in DictReader(self._file):
converted = dict([(key.upper(), self._convert(val)) for key, val in line.iteritems()])
yield converted
class ItmToWGS84(object):
def __init__(self):
# initializing WGS84 (epsg: 4326) and Israeli TM Grid (epsg: 2039) projections.
# for more info: http://spatialreference.org/ref/epsg/<epsg_num>/
self.wgs84 = pyproj.Proj(init='epsg:4326')
self.itm = pyproj.Proj(init='epsg:2039')
def convert(self, x, y):
"""
converts ITM to WGS84 coordinates
:type x: float
:type y: float
:rtype: tuple
:return: (long,lat)
"""
longitude, latitude = pyproj.transform(self.itm, self.wgs84, x, y)
return longitude, latitude
def time_delta(since):
delta = relativedelta(datetime.now(), since)
attrs = ['years', 'months', 'days', 'hours', 'minutes', 'seconds']
return " ".join('%d %s' % (getattr(delta, attr),
getattr(delta, attr) > 1 and attr or attr[:-1])
for attr in attrs if getattr(delta, attr))