-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
34 lines (30 loc) · 1.04 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
import configparser
import datetime as dt
from sqlalchemy.exc import OperationalError
from sqlalchemy import create_engine
def read_config(config_file):
config_parser = configparser.ConfigParser()
try:
config_parser.read(config_file)
except configparser.Error as e:
print(f"Reading from the config file failed: {e}")
raise
return config_parser
def next_year():
return [dt.datetime.now().isocalendar()[0] + 1]
def blob_names(countries, years):
try:
for country in countries:
for year in years:
yield country, year
except TypeError as e:
print(f"Wrong input - check config. Years and countries must be in '[]': {e}")
raise
def get_connection(user, password, host, port, database):
conn_string = f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}'
try:
db = create_engine(conn_string)
return db.connect()
except OperationalError as e:
print(f"Connection to database failed. Check config: {e}")
raise