Hi everyone!  

I've made two small but useful improvements which you can preview in the 
branch 'felix-fix-script-results'.

*1- Return values from commands*

Until now, calling a command with doCommandByName, executeMinibufferCommand 
or doCommand didn’t return the result of the underlying method. 
(c.keyHandler.funcReturn was removed a while ago) That’s now fixed — these 
functions will return whatever the command’s method returns.

For example, the convert-blanks command (which replaces leading spaces with 
tabs) returns True if it made any changes, and False otherwise. Previously, 
calling it with *c.doCommandByName('convert-blanks')* would return None, 
but now it correctly returns the boolean result — just like calling 
*c.convertBlanks()* directly.

*2 - Another feature for user-defined scripts*

I've also added a way for user-defined scripts (those inside @command or 
@button nodes) to return values as well. Since top-level return statements 
aren't allowed in scripts executed with exec, you can now define a global 
variable named result, and Leo will return its value after the script runs. 
For example:

global result
result = 42

This makes it easier to write scripts that communicate results back to 
automation tools, plugins, or other parts of an experimental workflow.

*I've attached a small Leo file demonstrating a use case for this.*

I'm just curious to know if these two changes are okay with 
y’all—especially the second one. 😬

Félix

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/leo-editor/27446762-b6ac-4bb6-81be-66a73299ee85n%40googlegroups.com.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: https://leo-editor.github.io/leo-editor/leo_toc.html -->
<leo_file xmlns:leo="https://leo-editor.github.io/leo-editor/namespaces/leo-python-editor/1.1"; >
<leo_header file_format="2"/>
<globals/>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="felix.20250419160452.1"><vh>Introduction</vh></v>
<v t="felix.20250419155410.1"><vh>buttons</vh>
<v t="felix.20250419155410.2"><vh>@button rinse-reset</vh></v>
<v t="felix.20250419155410.3"><vh>@button process-1</vh></v>
<v t="felix.20250419155410.4"><vh>@button process-2</vh></v>
<v t="felix.20250419155410.5"><vh>@button whole-process</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="felix.20250419155410.1">@language python

</t>
<t tx="felix.20250419155410.2">import random
g.es('Flushing with water')
# Works almost all of the time
global result
result = random.random() &lt; 0.9</t>
<t tx="felix.20250419155410.3">import random
g.es('process 1')
# Works only half of the time!
global result
result = random.random() &lt; 0.5</t>
<t tx="felix.20250419155410.4">import random
g.es('process 2')
# Works only half of the time!
global result
result = random.random() &lt; 0.5</t>
<t tx="felix.20250419155410.5">import time
g.es('perform all steps')

# preemptive cleanup
while not c.doCommandByName('rinse-reset'):
    time.sleep(0.5)  # Wait half a second before retrying, flushing with water

# step 1, make precursor for step 2 (Retry as needed)
while not c.doCommandByName('process-1'):
    time.sleep(0.5)  # Wait before flushing and retrying process 1
    while not c.doCommandByName('rinse-reset'):
        time.sleep(0.5)

# if ok, step 2 (critical step cannot be retried)
if c.doCommandByName('process-2'):
    g.es('Process Finished Successfully! (Retrieve product, do not flush!)')
else:
    g.es('Process 2 Failed, flushing scrapped batch')
    while not c.doCommandByName('rinse-reset'):
        time.sleep(0.5)

</t>
<t tx="felix.20250419160452.1">@language md

# 'doCommandByName' Return-Value Usage Example: Simulating a Chemistry Batch Process with Raspberry Pi GPIO

This example simulates automation of a chemistry batch process using Raspberry Pi GPIO pins connected to motor drivers and sensors.

Each step (@button nodes like rince-reset, process-1, and process-2) can be triggered individually for experimentation, but they're also orchestrated together in a final @button called whole-process.

The doCommandByName return values now allow proper retry logic and error handling at various steps.</t>
</tnodes>
</leo_file>

Reply via email to