Python Packages Implementation

What is the (best) approach for declaring or importing python packages for runtime python behaviors?

Would that be done in the initialization file and if so could an example be provided for a python initialization file?

Or, is it declared in the runtime behavior itself and if so wouldn’t that slow down the simulation run if each step such packages are imported repeatedly?

We do support the import of some Python modules, and as you’ve identified, the way you do this at the moment is to import the module within the behavior.

A future version of the engine might allow the user to do something more globally, but at the moment we don’t have support for that. Behaviors aren’t started on different Python processes though, so they should benefit from module caching, meaning that repeated imports shouldn’t have too much overhead.

I’d also raise that at the moment JavaScript behaviors tend to be more performant than Python, so unless you have strong needs for numpy or something you might want to consider rewriting in JavaScript if you’re aiming for performance.

Awesome - thanks!

Yes, JavaScript is the language of choice but I am deferring to Python for a specific behavior for its curve fit package.

Could you provide an example of message handling in Python with an “if” statement for a specific message (instead list filter + length) of as well as code that can capture the data transferred?

You can simply loop over the messages and check for the type of the message (which you specify when sending it). I’m currently not aware of an example at hIndex, but this code snippet should help understand the idea:

The first agent will send two messages of different types to "the_other" agent:

def behavior(state, context):
    state.add_message("the_other", "type_a", {"a": 10})
    state.add_message("the_other", "type_b", {"b": 20})

"the_other" agent then retrieves the messages and stores the message data of "type_b":

def behavior(state, context):
    for message in context.messages():
        if message["type"] == "type_b":
            state.type_b_message = message["data"]

The agent, to whom the message was sent, will now store {"b": 20} in "type_b_message".