You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
re = polyglot.eval(string="RegExp()", language="js")
84
+
>>> pattern = js_re.compile(".*(?:we have (?:a )?matching strings?(?:[!\\?] )?)(.*)")
67
85
68
-
pattern = re.compile(".*(?:we have (?:a )?matching strings?(?:[!\\?] )?)(.*)")
86
+
>>>if pattern.exec("This string does not match"):
87
+
...raiseSystemError("that shouldn't happen")
69
88
70
-
if pattern.exec("This string does not match"):
71
-
raiseSystemError("that shouldn't happen")
89
+
>>> md = pattern.exec("Look, we have matching strings! This string was matched by Graal.js")
72
90
73
-
md = pattern.exec("Look, we have matching strings! This string was matched by Graal.js")
74
-
ifnot md:
75
-
raiseSystemError("this should have matched")
76
-
77
-
print("Here is what we found: '%s'"% md[1])
78
-
```
79
-
80
-
To run it, pass the `--jvm --polyglot` option to the `graalpy` launcher:
81
-
```shell
82
-
graalpy --jvm --polyglot polyglot_example.py
91
+
>>>"Here is what we found: '%s'"% md[1]
92
+
"Here is what we found: 'This string was matched by Graal.js'"
83
93
```
84
94
85
95
This program matches Python strings using the JavaScript regular expression object.
86
-
Python reads the captured group from the JavaScript result and prints:
87
-
*Here is what we found: 'This string was matched by Graal.js'*.
96
+
Python reads the captured group from the JavaScript result and prints it.
88
97
89
98
As a more complex example, see how you can read a file using R, process the data in Python, and use R again to display the resulting data image, using both the R and Python libraries in conjunction.
90
99
To run this example, first install the required R library:
@@ -135,31 +144,36 @@ time.sleep(10)
135
144
136
145
Finally, to interoperate with Java (only when running on the JVM), you can use the `java` module:
137
146
```python
138
-
import java
139
-
BigInteger = java.type("java.math.BigInteger")
140
-
myBigInt = BigInteger.valueOf(42)
141
-
# public Java methods can just be called
142
-
myBigInt.shiftLeft(128)
143
-
# Java method names that are keywords in Python can be accessed using `getattr`
>>># Java method names that are keywords in Python can be accessed using `getattr`
154
+
>>>getattr(myBigInt, "not")()
155
+
<JavaObject[java.math.BigInteger] at ...>
156
+
>>> byteArray = myBigInt.toByteArray()
157
+
>>># Java arrays can act like Python lists
158
+
>>>list(byteArray)
159
+
[42]
148
160
```
149
161
150
162
For packages under the `java` package, you can also use the normal Python import
151
163
syntax:
152
164
```python
153
-
import java.util.ArrayList
154
-
from java.util import ArrayList
155
-
156
-
java.util.ArrayList == ArrayList
157
-
158
-
al = ArrayList()
159
-
al.add(1)
160
-
al.add(12)
161
-
print(al)
162
-
# prints [1, 12]
165
+
>>>import java.util.ArrayList
166
+
>>>from java.util import ArrayList
167
+
>>>
168
+
>>> java.util.ArrayList == ArrayList
169
+
True
170
+
>>> al = ArrayList()
171
+
>>> al.add(1)
172
+
True
173
+
>>> al.add(12)
174
+
True
175
+
>>> al
176
+
[1, 12]
163
177
```
164
178
165
179
In addition to the `type` builtin method, the `java` module exposes the following
@@ -173,19 +187,18 @@ Builtin | Specification
173
187
`is_symbol(obj)` | returns `True` if `obj` if the argument is a Java host symbol, representing the constructor and static members of a Java class, as obtained by `java.type`
174
188
175
189
```python
176
-
import java
177
-
ArrayList = java.type('java.util.ArrayList')
178
-
my_list = ArrayList()
179
-
print(java.is_symbol(ArrayList))
180
-
# prints True
181
-
print(java.is_symbol(my_list))
182
-
# prints False, my_list is not a Java host symbol
183
-
print(java.is_object(ArrayList))
184
-
# prints True, symbols are also host objects
185
-
print(java.is_function(my_list.add))
186
-
# prints True, the add method of ArrayList
187
-
print(java.instanceof(my_list, ArrayList))
188
-
# prints True
190
+
>>> ArrayList = java.type('java.util.ArrayList')
191
+
>>> my_list = ArrayList()
192
+
>>> java.is_symbol(ArrayList)
193
+
True
194
+
>>> java.is_symbol(my_list)
195
+
False
196
+
>>> java.is_object(ArrayList)
197
+
True
198
+
>>> java.is_function(my_list.add)
199
+
True
200
+
>>> java.instanceof(my_list, ArrayList)
201
+
True
189
202
```
190
203
191
204
See [Polyglot Programming](https://github.com/oracle/graal/blob/master/docs/reference-manual/polyglot-programming.md) and [Embed Languages](https://github.com/oracle/graal/blob/master/docs/reference-manual/embedding/embed-languages.md) for more information about interoperability with other programming languages.
0 commit comments