A bridge to call Python from Pharo (only POC at the moment)
Metacello new
baseline: 'PyBridge';
repository: 'github://aranega/pybridge/src';
load
The python code is located in the python
branch.
The project had been built using pipenv
. Simply checkout in the python
branch and do:
$ pipenv install
You need to launch the server that will be listening for orders from Pharo.
Simply checkout to the python
branch and run the following command:
$ pipenv shell
(pybridge) $ python server.py
You can create objects of a dedicated Python type in Pharo using the PyBridgeObject
class.
For example, you can open a playground and type:
myobject := PyBridge createInstance: #A. "A here is a class from the server, it's only a test class"
myobject myval. "Returns a PyBridgeObjectLiteral with the value 0"
otherObject := PyBridge createInstance: #A. "Creates another A object"
myobject myref: otherObject. "Links the two object instances"
myobject myref. "Returns otherObject"
!! Careful, this API will change and evolved. It's use here simply as a POC.
Here are some examples that show how to interact with Python from Pharo using PyBridge.
In this example, a python list
is created, elements are put inside.
We then iterate on the collection and show elements in the Transcript
.
list := PyBridge load: #builtins::list. "could have been also #'builtins.list'"
mylist := list new.
mylist append: 3.
mylist extend: #(4 5 6 7).
mylist do: [ :each | Transcript crShow: each asString ].
In this example, a shortcut is used to create an instance of a Python dictionary.
Then, few values are added inside and we iterate over each keys and values printing them in Transcript
.
mydict := PyBridge createInstance: #builtins::dict.
mydict at: #k1 put: 'value1'.
mydict at: #k2 put: 2.
"Unpacking is (kinda) supported"
mydict items do: [:key :val | Transcript crShow: '(', key value, ',', value asString, ')'].
Here, we compile and disassemble some Python code.
builtins := PyBridge load: #builtins.
disModule := PyBridge load: #dis.
codeObject := builtins compile: 'x = 0' filename: 'testcompile' mode: 'exec'.
(disModule Bytecode: codeObject) do: [ :each | Transcript crShow: each opname asString ].
This example opens and write a String into a file.
builtins := PyBridge loadClass: #builtins.
file := builtins open: 'test.myfile' mode: #w.
file with: [ :f | f write: 'test' ].