21. Python Integration¶
This notebook explores some additional features related to Python integration in RAW.
For ease of demonstration, we will use the Python API directly.
from rawapi import new_raw_client cli = new_raw_client()
One notable feature is the integration of RAW types with Python types.
For instance, RAW collections automatically become Python collections:
r = cli.query(' SELECT * FROM ["Hello", "World"] ') print(list(r))
Collections can even be converted onto Numpy arrays, for ease of integration with AI/ML tools:
cli.query(' (a: [[1,1],[2,2]]) ', np_arrays=True)
OrderedDict([('a', [[1, 1], [2, 2]])])
RAW records automatically become Python dictionaries:
r = cli.query(' (field1: 1, field2: "One") ') r
OrderedDict([('field1', 1), ('field2', 'One')])
Finally, RAW queries can also call out to Python functions directly.
This allows executing Python code as part of the RAW query.
For this, use the
python(<arguments list>) -> $$$ <Python Code $$$ syntax in RQL.
The code in-between
$$$ is Python code being executed server-side.
cli.query(""" sum_in_python := \python(a: INT): STRING -> $$$ return "This is Python code summing the number: " + str(a + 1) $$$; sum_in_python(2) """)
'This is Python code summing the number: 3'