Giter VIP home page Giter VIP logo

pybridge's Introduction

pybridge

A bridge to call Python from Pharo (only POC at the moment)

Installation

Pharo part

Metacello new
	baseline: 'PyBridge';
	repository: 'github://aranega/pybridge/src';
	load

Python part

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

Usage

Python part

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

Pharo part

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.

Examples

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' ].

pybridge's People

Contributors

aranega avatar stevencostiou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pybridge's Issues

Clarify setup

A few questions:

  • Why is the python stuff in a separate branch? I don't know much about Python conventions but this seems odd (see https://github.com/aranega/pybridge/tree/python#python-part)
  • Is pybridge compatible with others e.g. PythonBridge? I wonder because they both use the pipenv
  • Is pipenv install done from the image folder or git repo root? I guess the latter is implied from the relative path, but just to confirm...

Problem with dict ...

Just installed as described in Readme.md
Running server.py inside pipenv as described in Readme.md

code:

| mydict | mydict := PyBridge createInstance: #builtins::dict. mydict at: 'k1' put: 'value_1'.

Error:

Does not understand: aMessage
where aMessage is:

__setitem__: 'k1' value: 'value_1'

Error printing: #(139762652919728)

Under inspection: object has no __dict__

Pipfile python version

Based on my limited research, it seems the conservative best practice is to stay one major version behind latest Python, which seems to be 3.8.6. Any reason to default to 3.7?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.