-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_utils.py
156 lines (118 loc) · 3.63 KB
/
common_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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
################################################################################
# #
# Common utilities. #
# #
################################################################################
# pandas.
from pandas import DataFrame, read_csv
# json.
from json import load
#!/usr/bin/env python
import os
# subprocess.
import subprocess
# requests.
import requests
# sparql_dataframe.
from sparql_dataframe import get
# const.
from const import ENDPOINT,TEST_QUERY
###################
# #
# Common methods. #
# #
###################
#
# csv_to_df(_f_path: string _dtype: dictionary): -> DataFrame.
#
def csv_to_df(_f_path: str, _dtype: dict) -> DataFrame:
"""
Produce a pandas dataframe form csv file.
Args:
_f_path (string)
_dtype (dict)
Returns:
(DataFrame)
"""
try:
data_frame = read_csv(
_f_path,
keep_default_na = False,
dtype = _dtype,
sep = ',',
encoding = 'utf-8'
)
return data_frame
except Exception:
print('-- ERR: csv file is empty or malformed!')
raise
#
# json_to_df(_f_path: string): -> DataFrame.
#
def json_to_df(_f_path: str) -> DataFrame:
"""
Produce pandas dataframe from json.
Args:
_f_path (string)
Returns:
DataFrame:
"""
try:
with open(_f_path, 'r', encoding='utf-8') as f:
data_frame = load(f)
return data_frame
except Exception:
print('-- ERR: json file is empty or malformed!')
raise
################################
# #
# Create foldern if not exist. #
# #
################################
def create_folder(_folderName: str) -> None:
if isinstance(_folderName, str):
if not os.path.exists(_folderName):
os.makedirs(_folderName)
else:
raise("-- ERR: _folderName is not a string!")
###############################################
# #
# Common methods to interact with Blazegraph. #
# #
###############################################
#
# Check if the blazegraph insance is already active.
#
def blazegraph_instance_is_active() -> bool:
try:
page = requests.get('http://127.0.0.1:9999/blazegraph/#splash')
if page.status_code == 200 :
return True
else:
return False
except Exception as error:
print(error.args)
return False
#
# Check if there is something in blazegraph db yet.
#
def blazegraph_instance_is_empty() -> bool:
df_sparql = get(ENDPOINT, TEST_QUERY, True)
if df_sparql.empty:
return True
else:
return False # TEST: set True only for test! Otherwise False
#
# Start the blazegraph server db instance.
#
def start_blazegraph_server() -> None:
try:
# Join the current directory path with the target folder.
blazegraph_path = os.path.join(os.path.dirname(__file__), './blazegraph' )
subprocess.Popen(
'java -server -Xmx4g -jar blazegraph.jar',
cwd = blazegraph_path,
shell = True,
)
except Exception:
raise