-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patheGo_preprocessing.py
More file actions
80 lines (65 loc) · 2.76 KB
/
eGo_preprocessing.py
File metadata and controls
80 lines (65 loc) · 2.76 KB
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
"""eGo Data prerocessing (eGoDP)
This script opens a oedb database connection and executes different parts of eGo.
Reads python and SQL scripts and gives logging infos during the execution.
Also see corresponding BPML diagram.
"""
__copyright__ = "Reiner Lemoine Institut gGmbH"
__license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__ = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__ = "gplssm"
import pandas as pd
import logging
import time
import os
import codecs
from tools import io
def preprocessing():
# Configure logging
logger = logging.getLogger('EEEE')
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(message)s',
datefmt='%Y-%m-%d %I:%M:%S')
ch.setFormatter(formatter)
logger.addHandler(ch)
# get current time and inform about start
total_time = time.time()
logger.info('ego preprocessing started...')
# list of sql- and python-snippets that process the data in correct order
snippet_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__),
'preprocessing'))
script_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__),
'python_scripts'))
snippets = ['ego_pre_slp_parameters.sql']
# get database connection
conn = io.oedb_session(section='oep')
# iterate over list of sql- and python-snippets and execute them
for snippet in snippets:
# timing and logging
snippet_time = time.time()
logger.info("Execute '{}' ...".format(snippet))
if os.path.splitext(snippet)[1] == '.sql':
snippet_str = open(os.path.join(snippet_dir, snippet)).read()
# execute desired sql snippet
conn.execution_options(autocommit=True).execute(snippet_str)
elif os.path.splitext(snippet)[1] == '.py':
filename = os.path.join(script_dir, snippet)
script_str = open(filename, "rb").read()
# execute desired sql snippet
exec(compile(script_str, filename, 'exec'))
else:
raise NameError('{} is neither a python nor a sql script (at least it '
'has not the right extension). Please add an extension '
'to the script name (.py or .sql)'.format(snippet))
# inform the user
logger.info('...successfully done in {:.2f} seconds.'.format(
time.time() - snippet_time))
# close database connection
conn.close()
logger.info('Data processing script successfully executed in {:.2f} seconds'.format(
time.time() - total_time))
if __name__ == '__main__':
preprocessing()