-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_io.py
executable file
·100 lines (91 loc) · 3.62 KB
/
util_io.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
import sqlite3
import pandas as pd
import os
import glob
import shutil
homedir = os.getcwd() + '/csv_FY/'
def connect(db):
conn = sqlite3.connect('{0}/db/{1}.db'.format(homedir, db))
return conn
# print dictionary d with 'limit' number of records
def print_dict(d, limit):
count = 0
iterator = iter(d)
while count < limit:
key = next(iterator)
print '{0} -> {1}'.format(key, d[key])
count += 1
def view_building(b, col):
conn = connect('all')
df = pd.read_sql('SELECT DISTINCT Building_Number, Fiscal_Year, Fiscal_Month, year, month, [Gross_Sq.Ft], [Region_No.], Cat, [{1}] FROM EUAS_monthly WHERE Building_Number = \'{0}\''.format(b, col), conn)
return df
def create_header(title, templatepath=None, assetdir=None):
lines = []
if templatepath is None:
lines.append('<!DOCTYPE html>')
lines.append('<html>')
lines.append('<head>')
lines.append('<title>{0}</title>'.format(title))
lines.append('<h1>{0}</h1>'.format(title))
else:
with open (os.getcwd() + templatepath, 'r') as rd:
temp = rd.readlines()
head_end_idx = (map(lambda x: '</head>' in x, temp)).index(True)
lines = temp[:head_end_idx]
for i in range(len(lines)):
lines[i] = lines[i].replace("<title>Page title - Sitename</title>", "<title>{0}</title>".format(title))
lines[i] = lines[i].replace("assets/", "{0}assets/".format(assetdir))
return lines
def dir2html(dirname, suffix, title, outfile, files=None, templatepath = None, assetdir=None, style='width:700px;height:auto;', withname=True):
if files is None:
files = glob.glob(dirname + suffix)
lines = create_header(title, templatepath, assetdir)
if '.png' in suffix or '.PNG' in suffix:
if withname:
template = '<h2>name</h2>\n<img src="file" alt="No Data" style="{0}">'.format(style)
else:
template = '<img src="file" alt="No Data" style="{0}">'.format(style)
lines.append('<h1>{0}</h1>'.format(title))
for f in files:
relative = f[- len(f) + len(dirname):]
filename = f[f.rfind('/') + 1: f.find(suffix[1:])]
line = template.replace('file', relative)
line = line.replace('name', filename)
lines.append(line)
lines.append('</body>')
lines.append('</html>')
with open(dirname + outfile, 'w+') as wt:
wt.write('\n'.join(lines))
print 'end'
return
def csv2html(path, rename_dict=None, format_dict=None):
df = pd.read_csv(path)
if not rename_dict is None:
df.rename(columns=rename_dict, inplace=True)
if not format_dict is None:
for k in format_dict:
df[k] = df[k].map(format_dict[k])
df.to_html(path.replace('.csv', '.html'))
return
# def dir2html(dirname, suffix, title, outfile):
# files = glob.glob(dirname + suffix)
# lines = []
# lines.append('<!DOCTYPE html>')
# lines.append('<html>')
# lines.append('<head>')
# lines.append('<title>{0}</title>'.format(title))
# lines.append('<h1>{0}</h1>'.format(title))
# if '.png' in suffix:
# template = '<h2>name</h2>\n<img src="file" alt="No Data" style="width:700px;height:auto;">'
# for f in files:
# relative = f[- len(f) + len(dirname):]
# filename = f[f.rfind('/') + 1: f.find(suffix[1:])]
# line = template.replace('file', relative)
# line = line.replace('name', filename)
# lines.append(line)
# lines.append('</body>')
# lines.append('</html>')
# with open(dirname + outfile, 'w+') as wt:
# wt.write('\n'.join(lines))
# print 'end'
# return