ErlPort - Erlang port protocol for Python

Fork me on GitHub Erlang and Python integration with ErlPort

How ErlPort works

Make sure you read README first.

When ErlPort based Python script is running with the BIF open_port/2 the port which communicate with the external OS process is created.

Now if we want to send an Erlang term to Python we need to encode it to binary form with the BIF term_to_binary/1 and then send this binary to the port with the BIF port_command/2. On the Python side the binary is read and converted to Python term (see the function erlterms.decode()) with the method Port.read(). If erlport.Port is used with a inheritor class of the Protocol then Erlang terms in the form 'function_name' or {'function_name', ...} will call a method aProtocol.handle_function_name which must be defined in the Protocol inheritor class.

To send a term from Python to Erlang we need to use the method Port.write() which convert the term to the binary form with the function erlterms.encode() and then send this binary to Erlang. In Erlang the binary can be received from the port with the receive expression (see port messages for details) and converted to the term with the BIF binary_to_term/1.

See also rendered ErlPort doctests:

Data type mapping

Erlang to Python data type mapping as defined by erlterms.decode():

Erlang Python
integer() int()
list()/string() list()/String() Note: There is no distinction between lists and strings in Erlang so on the Python side lists known to be strings must be explicitly converted with the String() class
binary() str()
atom() Atom()
tuple() tuple()
float() float()
bitstring() BitBinary()

Python to Erlang data type mapping as defined by erlterms.encode():

Python Erlang
tuple() tuple()
list() list()
unicode()/String() list()
Atom() atom()
BitBinary() bitstring()
str() binary()
True 'true'
False 'false'
int() integer()
float() float()
dict() proplist()/orddict()
None 'none'
datetime.datetime() calendar:t_datetime()
Updated on 2011-07-21