-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
149 lines (108 loc) · 3.82 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
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
"""Utility functions used for the project"""
import json
from SPARQLWrapper import SPARQLWrapper
# ----------------------------- Utilities for backend and frontend -----------------------------
def ul_fromlist(python_list: list) -> str:
"""Formatting list for HTML
Args:
python_list (list): python list to format for HTML
Returns:
str: HMTL code for the list
"""
return '<ul>' + '<li>'+'<li>'.join(python_list) + '</ul>'
def p_fromlist(python_list: list) -> str:
"""Formatting list into paragraph in HTML
Args:
python_list (list): python list to format
Returns:
str: HMTL code for the list
"""
return '<p>' + ', '.join(python_list) + '</p>'
def tags_fromlist(python_list: list) -> str:
"""Format python list into a set of tags in HTML
Args:
python_list (list): python list to format
Returns:
str: Tag formatted list in HTML
"""
strr = ''
for tag in python_list:
strr += f"<button type='button' class='btn btn-outline-info mr-2 mb-1'>{tag}</button>"
return strr
def score_bar(value: float, label: str = '') -> str:
"""Score bar in HTML
Args:
value (float): Value of the score (between 0 and 100)
Returns:
str: HTML code for the score bar
"""
return f"""<div class='progress'>
<div class='progress-bar progress-bar-striped progress-bar-animated'
role='progressbar'
aria-valuenow='{value}'
aria-valuemin='0'
aria-valuemax='100'
style='width: {value}%'>
{label + value}%
</div>
</div>"""
# ----------------------------- Utilities for sparql queries -----------------------------
def pprint(python_dict: dict) -> None:
"""Pretty printer for json objects, only for dev purposes
Args:
dict (dict): input dictionnary in json format
"""
print(print(json.dumps(python_dict, indent=4, sort_keys=True)))
def get_sparql() -> SPARQLWrapper:
"""Utility function for SPARQL endpoint
Returns:
SPARQLWrapper: instance of a wrapper to make queries
"""
return SPARQLWrapper("http://query.wikidata.org/sparql")
def get_prefix() -> str:
"""Beginning of all queries (prefixes used in the different queries in the project)
Returns:
str: Prefix lines
"""
return """
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
"""
def search(variable: str, target: str) -> str:
"""Search serice using mwapi on wikidata
Args:
variable (str): variable name (?film, ?director...)
target (str): value to search for
Returns:
str: service query
"""
if variable is None or target is None:
return ""
return f"""
SERVICE wikibase:mwapi {{
bd:serviceParam wikibase:api "EntitySearch" .
bd:serviceParam wikibase:endpoint "www.wikidata.org" .
bd:serviceParam mwapi:search "{target}" .
bd:serviceParam mwapi:language "en" .
{variable} wikibase:apiOutputItem mwapi:item .
}}
"""
def resp_format(res_list: list) -> list[dict]:
"""Format query result into a dictionnary
Args:
res_list (list): list of elements in the query response
Returns:
list: formatted response (list[dict])
"""
return [
{
key: elm[key]['value'].split(";") if "List" in key
else elm[key]['value']
for key in elm
}
for elm in res_list
]