-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
63 lines (54 loc) · 1.63 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
import pickle
import sqlite3
from datetime import datetime
def current_time():
"""
Returns:
The current hour, minutes and second
"""
current_time = datetime.now()
formatted_time = current_time.strftime("%H:%M:%S")
return formatted_time
def create_connection(database: str, logger):
"""
Create a database connection to the SQLite database specified by db_file
Parameters:
database_path: path to the database file
Returns:
Connection object or None
"""
con = None
try:
con = sqlite3.connect(database)
except sqlite3.Error as e:
logger.exception("Error connecting to database: ", e)
return con
def save_pickle(data: dict, output_file: str, logger):
"""
This function stores a dictionary of parsed data as a pickle file.
Args:
data : a dictionary containing the data
output_file : name and path of the output file
path : the path where you want the file to be stored
"""
try:
with open(output_file, "wb") as f:
pickle.dump(data, f)
except Exception as e:
logger.exception("Error occurred while saving pickle file: ", e)
def load_pickle(input_file: str, logger):
"""
This function loads a pickle file.
Args:
input_file: Name and path of the pickle file.
Returns:
The data loaded from the pickle file.
"""
try:
with open(input_file, "rb") as f:
data = pickle.load(f)
return data
except Exception as e:
logger.exception("Error occurred while loading pickle file: ", e)
return None