### Install Pyrlang using pip Source: https://pyrlang.github.io/Pyrlang/build-library Installs the Pyrlang library and automatically includes the 'pyrlang-term' dependency. This is the simplest method for getting started. ```bash pip3 install pyrlang ``` -------------------------------- ### Interact with GenServer-like Python Processes from Erlang Source: https://pyrlang.github.io/Pyrlang/cookbook Shows how to use `gen_server:call` from Erlang to communicate with a `GenServer`-based Python process. It demonstrates calling a specific function ('hello') and a general call. ```erlang Server = {my_gen_server, 'py@127.0.0.1'}. gen_server:call(Server, hello). gen_server:call(Server, somethingelse). ``` -------------------------------- ### Start Pyrlang Node and Send Message Source: https://pyrlang.github.io/Pyrlang/cookbook Initializes a Pyrlang node, registers a fake process, and sends a message to an Erlang shell. Requires the `pyrlang` and `term` libraries. The `eng.run_forever()` call keeps the node running to process messages. ```python from pyrlang import Node from term import Atom def main(): node = Node(node_name="py@127.0.0.1", cookie="COOKIE") fake_pid = node.register_new_process() # To be able to send to Erlang shell by name first give it a # registered name: `erlang:register(shell, self()).` # To see an incoming message in shell: `flush().` node.send_nowait(sender=fake_pid, receiver=(Atom('erl@127.0.0.1'), Atom('shell')), message=Atom('hello')) eng.run_forever() if __name__ == "__main__": main() ``` -------------------------------- ### Install documentation build dependencies Source: https://pyrlang.github.io/Pyrlang/build-library Installs Sphinx and other required packages for building the project documentation using the provided requirements file. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install Pyrlang from source Source: https://pyrlang.github.io/Pyrlang/build-library Clones the Pyrlang repository from GitHub and installs it locally. This method is useful for developers who need to work with the source code. ```bash git clone git@github.com:Pyrlang/Pyrlang.git cd Pyrlang pip install . ``` -------------------------------- ### Create a GenServer-like Process in Pyrlang Source: https://pyrlang.github.io/Pyrlang/cookbook Implements a process that mimics Erlang's `gen_server:call` behavior by inheriting from `GenServer`. It uses the `@call` decorator to map specific functions to incoming calls and handles a fallback for unknown messages. ```python from pyrlang.gen.server import GenServer from pyrlang.gen.decorators import call from pyrlang.terms import Atom class MyGenServer(GenServer): def __init__(self): super().__init__() node = self.node_db.get() node.register_name(self, Atom('my_gen_server')) # optional @call(1, lambda msg: msg == 'hello') def hello(self, msg): return self.pid_ @call(2, lambda msg: True) def catch_all_call(self, msg): ret = "I don't understand" return ret ``` -------------------------------- ### Send Messages to Python Objects from Erlang Source: https://pyrlang.github.io/Pyrlang/cookbook Demonstrates sending messages from Erlang to Python processes. It shows how to send to a registered name (e.g., 'my_process') and directly to a Python process's PID if known in Erlang. ```erlang %% Sending to an atom name {my_process, 'py@127.0.0.1'} ! hello. %% Sending directly to a Python PID PyProcessPid ! hello. ``` -------------------------------- ### Connect Erlang and Python Nodes Source: https://pyrlang.github.io/Pyrlang/cookbook Demonstrates initiating connections between Erlang and Python nodes. This can be done from Erlang using `net_adm:ping` or by sending messages to a remote name using the tuple format `{Name, Node}`. The Python side handles connection establishment automatically when messages are sent to remote PIDs or registered names. ```erlang net_adm:ping('py@127.0.0.1'). ``` ```erlang {Name, 'py@127.0.0.1'} ! hello. ``` -------------------------------- ### Define and Register a Python Process in Pyrlang Source: https://pyrlang.github.io/Pyrlang/cookbook Defines a custom Python process inheriting from `Process`, optionally registering a name with the node. Incoming messages are handled in `handle_one_inbox_message`. This enables receiving messages from Erlang. ```python from pyrlang.process import Process from pyrlang.terms import Atom class MyProcess(Process): def __init__(self) -> None: super().__init__() node = self.node_db.get() node.register_name(self, Atom('my_process')) # optional def handle_one_inbox_message(self, msg): print("Incoming", msg) ``` -------------------------------- ### Send Message from Python to Remote Node Source: https://pyrlang.github.io/Pyrlang/cookbook Illustrates sending messages from Python to a remote node. For remote PIDs, the `sender` can be `None` and the connection is established automatically. For remote named processes `{Name, Node}`, the `sender` PID is required. This example shows sending to a registered name `'shell'` on an Erlang node after registering it. ```python from term import Atom # Assuming 'node' is an initialized Pyrlang Node object # Send to a remote PID: # node.send(sender=None, # receiver=receiver_pid, # message=Atom('hello')) # Send to a remote named process: # fake_pid = node.register_new_process(None) # create a fake pid # node.send(sender=fake_pid, # receiver=(Atom('erl@127.0.0.1'), Atom('shell')), # message=Atom('hello')) ``` -------------------------------- ### Start Erlang Node Source: https://pyrlang.github.io/Pyrlang/calling_python This snippet demonstrates how to start an Erlang node with a specific name and cookie, which is a prerequisite for establishing a connection with a Python node for remote calls. ```bash # Your Unix shell: Start Erlang node with name and cookie $ erl -name erl@127.0.0.1 -setcookie COOKIE ``` -------------------------------- ### Notebook-Style Python Calls from Erlang Source: https://pyrlang.github.io/Pyrlang/calling_python This example shows how to create a Python context, import modules, call functions (with and without keyword arguments), perform object method calls, and manage remote results. It illustrates both immediate retrieval and storing results for later use. ```erlang %% Create a remote notebook object (context) on Python side Ctx = py:new_context('py@127.0.0.1'), %% Import datetime and call datetime.now(), kwargs are empty by default. %% Note that binaries, ASCII strings and atoms all work. %% First element in the list is module name to be imported DT1 = py:call(Ctx, [<<"datetime">>, "datetime", now], []), timer:sleep(2000), %% Import datetime and call datetime.now() again but now with kwargs #{} DT2 = py:call(Ctx, [datetime, datetime, <<"now">>]], [], #{}), %% Subtract two datetimes Diff = py:call(Ctx, [DT2, '__sub__'], [DT1], #{}), %% Call Diff.total_seconds() and retrieve the value without storing it %% in remote history. Result1 = py:call(Ctx, [Diff, <<"total_seconds">>]], [], #{}, #{immediate => true}), %% Or retrieve the diff and store it remotely then retrieve Result2Ref = py:call(Ctx, [Diff, <<"total_seconds">>]], []), Result2 = py:retrieve(Ctx, Result2Ref), %% Done with the remote context. Remote notebook object will be dropped. py:destroy(Ctx). ``` -------------------------------- ### Send Local Message from Python Source: https://pyrlang.github.io/Pyrlang/cookbook Demonstrates sending messages locally within a Pyrlang node using `Node.send()`. The `sender` argument is unused for local sends. The `receiver` is typically an `Atom` representing a registered process name, and `message` can be any Erlang-compatible term. ```python from term import Atom # Assuming 'node' is an initialized Pyrlang Node object # node.send(sender=None, # argument unused # receiver=Atom('my_erlang_process'), # message=(123, 4.5678, [Atom('test')])) ``` -------------------------------- ### RPC Call from Erlang to Python Source: https://pyrlang.github.io/Pyrlang/cookbook Shows how to perform Remote Procedure Calls (RPC) from Erlang to a Python node. Pyrlang's special `'rex'` process handles these calls. Standard RPC calls use `rpc:call/4`, while gen_server style calls use `gen_server:call/2` with a specific tuple format. Module and function names can be atoms, strings, or binaries. Errors are translated to Erlang exit exceptions. ```erlang rpc:call('py@127.0.0.1', time, time, []). ``` ```erlang gen_server:call({rex, 'py@127.0.0.1'}, {call, time, time, [], self()}). ``` -------------------------------- ### Install pyrlang-term dependency Source: https://pyrlang.github.io/Pyrlang/build-library Installs the 'pyrlang-term' package manually. This package is required by Pyrlang for encoding and decoding Erlang's external term format. ```bash pip3 install pyrlang-term ``` -------------------------------- ### Register Process in Erlang Shell Source: https://pyrlang.github.io/Pyrlang/cookbook A snippet to be run in an Erlang shell to register the current process (the shell) with the name 'shell'. This allows Python nodes to send messages to the shell using `{Atom('shell'), NodeName}` tuple format. ```erlang erlang:register(shell, self()). ``` -------------------------------- ### Exit Pyrlang Process Source: https://pyrlang.github.io/Pyrlang/cookbook Explains methods for exiting Pyrlang processes. Local processes can be exited using `Process.exit()` or `Node.exit_process()`. Linking to other processes means they will exit if the linked process exits. Remote nodes can call `erlang:exit` with the process's PID. ```python # Example usage (assuming 'process' is a Pyrlang Process object) # process.exit() # node.exit_process(pid_or_name) ``` -------------------------------- ### Erlang String as List of Integers (Python) Source: https://pyrlang.github.io/Pyrlang/data_types Demonstrates how Erlang strings are fundamentally lists of integers. This example shows a Python representation that might be mistaken for a Unicode object, highlighting potential conversion issues. ```python some_module.with_function("this is a unicode object") ``` -------------------------------- ### Batch Python Calls from Erlang Source: https://pyrlang.github.io/Pyrlang/calling_python This snippet demonstrates how to construct and execute a batch of Python calls. It shows how to create a new batch, add sequential calls to it (linking results as inputs), and then run the entire batch on a specified Python context. ```erlang %% Create an empty batch and begin adding calls to it S0 = py:batch_new(), {S1, R1} = py:batch_call(S0, [<<"datetime">>, "datetime", now], []), {S2, R2} = py:batch_call(S1, [datetime, datetime, <<"now">>]], [], #{}), %% Subtract two datetimes {S3, Diff} = py:batch_call(S2, [R2, '__sub__'], [R1], #{}), %% Call Diff.total_seconds() and retrieve the value without storing it %% in remote history. {S4, _R4} = py:batch_call(S3, [Diff, <<"total_seconds">>]], []), %% Create a remote notebook object (context) on Python side Ctx = py:new_context('py@127.0.0.1'), %% will retrieve because immediate=true by default Result = py:batch_run(Ctx, S4), %% Done with the remote context. Remote notebook object will be dropped. py:destroy(Ctx). ``` -------------------------------- ### Run Batch Remotely (Erlang) Source: https://pyrlang.github.io/Pyrlang/calling_python Executes a sequence of calls defined in a batch on a remote node using a given context. Supports options like timeout and immediate execution. The `immediate` option controls whether the actual value or a reference is returned. ```erlang py:batch_run(Context, Batch, Options). ``` -------------------------------- ### Create New Batch (Erlang) Source: https://pyrlang.github.io/Pyrlang/calling_python Initializes a new, empty batch for accumulating remote Python calls. This function requires no arguments and returns an empty batch object. ```erlang Batch = py:batch_new(). ``` -------------------------------- ### Erlang String as List of Integers (Erlang) Source: https://pyrlang.github.io/Pyrlang/data_types Illustrates the fundamental representation of Erlang strings as lists of integers, specifically those with values less than 256. This is how Erlang internally interprets strings. ```erlang some_module:with_function("this is a list of integers really"). ``` -------------------------------- ### Create New Context (Erlang) Source: https://pyrlang.github.io/Pyrlang/calling_python Establishes a new context for executing remote Python calls on a specified node. This context manages the spawning of remote processes and stores call result history. Optional configuration options can be provided. ```erlang Context = py:new_context(Node). Context = py:new_context(Node, Options). ``` -------------------------------- ### Append Call to Batch (Erlang) Source: https://pyrlang.github.io/Pyrlang/calling_python Appends a Python function call to an existing batch. It takes the batch, a Python path, and arguments (optional keyword arguments) and returns an updated batch along with a reference to the result. ```erlang py:batch_call(Batch, Path, Args) -> {Batch1, ResultRef}. py:batch_call(Batch, Path, Args, KwArgs) -> {Batch1, ResultRef}. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.