forked from 0hoo/snowball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
40 lines (28 loc) · 777 Bytes
/
utils.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
import statistics
def mean_or_zero(iter):
try:
return statistics.mean(iter)
except statistics.StatisticsError:
return 0
def parse_float(str):
try:
return float(str.replace(',', '').replace('%', ''))
except (ValueError, AttributeError):
return 0
def parse_int(str):
try:
return int(str.replace(',', ''))
except (ValueError, AttributeError):
return 0
def attr_or_key_getter(name, obj, default_value=0):
try:
return getattr(obj, name)
except AttributeError:
return obj.get(name, default_value)
def first_or_none(iter):
try:
return iter[0]
except IndexError:
return None
def float_or_none(x):
return None if not x else float(x.replace(',', ''))