11import marimo
2+ from graphfaker .logger import logger
23
34__generated_with = "0.13.1"
45app = marimo .App ()
89def _ ():
910 import pandas as pd
1011 import SPARQLWrapper as sw
12+
1113 return (pd ,)
1214
1315
1416@app .cell
1517def _ ():
1618 import requests
19+
1720 return (requests ,)
1821
1922
@@ -36,12 +39,19 @@ def _(WIKIDATA_SPARQL_URL, requests):
3639 def run_sparql_query (query ):
3740 """Fetch data from Wikidata using SPARQL."""
3841 headers = {"Accept" : "application/json" }
39- response = requests .get (WIKIDATA_SPARQL_URL , params = {"query" : query , "format" : "json" }, headers = headers )
42+ response = requests .get (
43+ WIKIDATA_SPARQL_URL ,
44+ params = {"query" : query , "format" : "json" },
45+ headers = headers ,
46+ )
4047 if response .status_code == 200 :
4148 return response .json ()["results" ]["bindings" ]
4249 else :
43- print ("SPARQL Query Failed!" , response .status_code )
50+ logger .error (
51+ "SPARQL Query Failed! Error: %s" , response .status_code , exc_info = True
52+ )
4453 return []
54+
4555 return (run_sparql_query ,)
4656
4757
@@ -70,12 +80,14 @@ def fetch_ceos_and_companies():
7080 G .add_edge (ceo_id , company_id , relationship = "CEO_of" )
7181
7282 return G
83+
7384 return (fetch_ceos_and_companies ,)
7485
7586
7687@app .cell
7788def _ ():
7889 import matplotlib .pyplot as plt
90+
7991 return
8092
8193
@@ -99,7 +111,13 @@ def generate_people(num=10):
99111 G = nx .Graph ()
100112 for i in range (num ):
101113 pid = f"person_{ i } "
102- G .add_node (pid , type = "Person" , name = fake .name (), email = fake .email (), age = random .randint (18 , 80 ))
114+ G .add_node (
115+ pid ,
116+ type = "Person" ,
117+ name = fake .name (),
118+ email = fake .email (),
119+ age = random .randint (18 , 80 ),
120+ )
103121 return G
104122
105123 @staticmethod
@@ -115,20 +133,29 @@ def generate_organizations(num=5):
115133 G = nx .Graph ()
116134 for i in range (num ):
117135 oid = f"org_{ i } "
118- G .add_node (oid , type = "Organization" , name = fake .company (), industry = fake .job ())
136+ G .add_node (
137+ oid , type = "Organization" , name = fake .company (), industry = fake .job ()
138+ )
119139 return G
120140
121141 @staticmethod
122142 def connect_people_to_organizations (G , people_nodes , org_nodes ):
123143 for p in people_nodes :
124144 org = random .choice (org_nodes )
125- G .add_edge (p , org , relationship = random .choice (["works_at" , "consults_for" , "owns" ]))
145+ G .add_edge (
146+ p ,
147+ org ,
148+ relationship = random .choice (["works_at" , "consults_for" , "owns" ]),
149+ )
126150
127151 @staticmethod
128152 def connect_people_to_places (G , people_nodes , place_nodes ):
129153 for p in people_nodes :
130154 place = random .choice (place_nodes )
131- G .add_edge (p , place , relationship = random .choice (["lives_in" , "born_in" ]))
155+ G .add_edge (
156+ p , place , relationship = random .choice (["lives_in" , "born_in" ])
157+ )
158+
132159 return (Graphfaker ,)
133160
134161
@@ -138,8 +165,12 @@ def _(Graphfaker):
138165 G_fake_places = Graphfaker .generate_places (5 )
139166 G_fake_orgs = Graphfaker .generate_organizations (5 )
140167
141- Graphfaker .connect_people_to_organizations (G_people , list (G_people .nodes ), list (G_fake_orgs .nodes ))
142- Graphfaker .connect_people_to_places (G_people , list (G_people .nodes ), list (G_fake_places .nodes ))
168+ Graphfaker .connect_people_to_organizations (
169+ G_people , list (G_people .nodes ), list (G_fake_orgs .nodes )
170+ )
171+ Graphfaker .connect_people_to_places (
172+ G_people , list (G_people .nodes ), list (G_fake_places .nodes )
173+ )
143174 return (G_people ,)
144175
145176
@@ -157,7 +188,7 @@ def _(G_people, nx):
157188
158189@app .cell
159190def _ (ceos_and_companies ):
160- print (ceos_and_companies .nodes (data = True ))
191+ logger . info (ceos_and_companies .nodes (data = True ))
161192 return
162193
163194
@@ -182,6 +213,7 @@ def fetch_places():
182213 G .add_node (city_id , type = "Place" , name = city_name )
183214
184215 return G
216+
185217 return
186218
187219
@@ -211,12 +243,16 @@ class WikiDataQueryResults:
211243 A class that can be used to query data from Wikidata using SPARQL and return the results as a Pandas DataFrame or a list
212244 of values for a specific key.
213245 """
246+
214247 def __init__ (self , query : str ):
215248 """
216249 Initializes the WikiDataQueryResults object with a SPARQL query string.
217250 :param query: A SPARQL query string.
218251 """
219- self .user_agent = "WDQS-example Python/%s.%s" % (sys .version_info [0 ], sys .version_info [1 ])
252+ self .user_agent = "WDQS-example Python/%s.%s" % (
253+ sys .version_info [0 ],
254+ sys .version_info [1 ],
255+ )
220256 self .endpoint_url = "https://query.wikidata.org/sparql"
221257 self .sparql = SPARQLWrapper (self .endpoint_url , agent = self .user_agent )
222258 self .sparql .setQuery (query )
@@ -233,7 +269,7 @@ def __transform2dicts(self, results: List[Dict]) -> List[Dict]:
233269 for result in results :
234270 new_result = {}
235271 for key in result :
236- new_result [key ] = result [key ][' value' ]
272+ new_result [key ] = result [key ][" value" ]
237273 new_results .append (new_result )
238274 return new_results
239275
@@ -244,7 +280,7 @@ def _load(self) -> List[Dict]:
244280 :return: A list of dictionaries, where each dictionary represents a result row and has keys corresponding to the
245281 variables in the SPARQL SELECT clause.
246282 """
247- results = self .sparql .queryAndConvert ()[' results' ][ ' bindings' ]
283+ results = self .sparql .queryAndConvert ()[" results" ][ " bindings" ]
248284 results = self .__transform2dicts (results )
249285 return results
250286
@@ -255,6 +291,7 @@ def load_as_dataframe(self) -> pd.DataFrame:
255291 """
256292 results = self ._load ()
257293 return pd .DataFrame .from_dict (results )
294+
258295 return (WikiDataQueryResults ,)
259296
260297
@@ -273,7 +310,7 @@ def _(data_extracter):
273310@app .cell
274311def _ (data_extracter ):
275312 df = data_extracter .load_as_dataframe ()
276- print (df .head ())
313+ logger . info (df .head ())
277314 return
278315
279316
0 commit comments