Skip to content

Commit 6a9fd5d

Browse files
author
Ghislain Fourny
committed
Add JSONiq magic.
1 parent 579b9b9 commit 6a9fd5d

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "jsoniq"
7-
version = "0.2.0a1"
7+
version = "0.2.0a2"
88
description = "Python edition of RumbleDB, a JSONiq engine"
99
requires-python = ">=3.11"
1010
dependencies = [

src/jsoniq/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
from jsoniq.session import RumbleSession
2+
from jsoniq.magic import JSONiqMagic
23

3-
__all__ = ["RumbleSession"]
4+
__all__ = ["RumbleSession", "JSONiqMagic"]
5+
6+
def load_ipython_extension(ipython):
7+
rumble = RumbleSession.builder.getOrCreate();
8+
ipython.register_magics(JSONiqMagic)

src/jsoniq/magic.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from IPython.core.magic import Magics, cell_magic, magics_class
2+
import time, json
3+
from .session import RumbleSession
4+
from py4j.protocol import Py4JJavaError
5+
6+
@magics_class
7+
class JSONiqMagic(Magics):
8+
def run(self, line, cell=None, timed=False):
9+
if cell is None:
10+
data = line
11+
else:
12+
data = cell
13+
14+
start = time.time()
15+
try:
16+
rumble = RumbleSession.builder.getOrCreate();
17+
response = rumble.jsoniq(data);
18+
except Py4JJavaError as e:
19+
print(e.java_exception.getMessage())
20+
return
21+
except Exception as e:
22+
print("Query unsuccessful.")
23+
print("Usual reasons: firewall, misconfigured proxy.")
24+
print("Error message:")
25+
print(e.args[0])
26+
return
27+
except:
28+
print("Query unsuccessful.")
29+
print("Usual reasons: firewall, misconfigured proxy.")
30+
return
31+
end = time.time()
32+
if(timed):
33+
print("Response time: %s ms" % (end - start))
34+
35+
if ("DataFrame" in response.availableOutputs()):
36+
print(response.pdf())
37+
elif ("Local" in response.availableOutputs()):
38+
count = response.getAsRDD().count()
39+
if count > 200:
40+
print("The query output %s items, which is too many to display. Displaying the first 200 items:" % count)
41+
for e in response.first():
42+
print(json.dumps(json.loads(e.serializeAsJSON()), indent=2))
43+
else:
44+
for e in response.json():
45+
print(json.dumps(e, indent=2))
46+
elif ("PUL" in response.availableOutputs()):
47+
print("The query output a Pending Update List.")
48+
else:
49+
print("No output available.")
50+
51+
@cell_magic
52+
def jsoniq(self, line, cell=None):
53+
return self.run(line, cell, False)
54+
55+
@cell_magic
56+
def timedjsoniq(self, line, cell=None):
57+
return self.run(line, cell, True)

0 commit comments

Comments
 (0)