forked from diogolundberg/pandas-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
67 lines (56 loc) · 1.67 KB
/
helpers.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
# -*- coding: utf-8 -*-
"""
Helpers
~~~~~~~
"""
''' Import statements '''
import sys, bz2, gzip, zipfile, os
import rarfile
from decimal import Decimal, ROUND_HALF_UP
from os.path import splitext, basename, exists
'''
Used for finding environment variables through configuration
if a default is not given, the site will raise an exception
'''
def get_env_variable(var_name, default=-1):
try:
return os.environ[var_name]
except KeyError:
if default != -1:
return default
error_msg = "Set the %s os.environment variable" % var_name
raise Exception(error_msg)
def d(x):
return Decimal(x).quantize(Decimal(".01"), rounding=ROUND_HALF_UP)
def get_file(full_path):
file_name = basename(full_path)
file_path_no_ext, file_ext = splitext(file_name)
extensions = {
'.bz2': bz2.BZ2File,
'.gz': gzip.open,
'.zip': zipfile.ZipFile,
'.rar': rarfile.RarFile
}
try:
file = extensions[file_ext](full_path)
except KeyError:
file = open(full_path)
except IOError:
return None
if file_ext == '.zip':
file = zipfile.ZipFile.open(file, file_path_no_ext)
elif file_ext == '.rar':
file = rarfile.RarFile.open(file, file_path_no_ext)
# print "Reading from file", file_name
return file
def format_runtime(x):
# convert to hours, minutes, seconds
m, s = divmod(x, 60)
h, m = divmod(m, 60)
if h:
return "{0} hours and {1} minutes".format(int(h), int(m))
if m:
return "{0} minutes and {1} seconds".format(int(m), int(s))
if s < 1:
return "< 1 second"
return "{0} seconds".format(int(s))