Has anyone experimented with using Python within LC?

Geoff Canyon gcanyon at gmail.com
Fri Jul 21 03:48:56 EDT 2023


This stack script hacks in support for arbitrary parameters to be passed in
easily.

You can, of course, do the same by including functions in the python
script, and then calling them in the shell statement. That's how I started
this journey, but it requires escaping things to make them
shell-compatible. This might be especially weird to do because of python's
flexible handling of double and single quotes.

This way the data can be passed in as you intend to use it, the only
limitation being that it all starts as text. Still, it works a treat.

Note: this doesn't check for file existence before writing them, although
it seems unlikely that anyone would have files named
e.g. 89914707287616_data.txt. Also, this doesn't re-use any files, so
repeated calls quickly pile up. This is far from a usable function.

The round-trip cost of doing things this way is maybe something like a
tenth of a second(?) so this is likely only for when you have a lot of
backend processing to do.

local fnb
function pythonFile p
   put the filename of this stack into fp
   set the itemdel to "/"
   if fnb is empty then
      put the long seconds into fnb
      replace "." with empty in fnb
      delete char 1 to 2 of fnb
   end if
   if paramCount() > 1 then
      put "exData = {}" & cr into fileHeader
      repeat with i = 2 to paramCount()
         add 1 to fnb
         put fnb & "_data.txt" into fn
         put fn into item -1 of fp
         put param(i) into url ("file:" & fp)
         put merge("with open('[[fp]]', 'r') as f:[[cr]]") after fileHeader
         put merge("    exData[ [[i - 2]] ] = f.read()[[cr]]") after
fileHeader
      end repeat
      put fileHeader & cr before p
   end if
   add 1 to fnb
   put fnb & "_temp.py" into fn
   put fn into item -1 of fp
   put p into url ("file:" & fp)
   return shell(quote & "/Users/gcanyon/opt/anaconda3/bin/python" & quote
&& quote & fp & quote)
end pythonFile

On Thu, Jul 20, 2023 at 6:20 PM Geoff Canyon <gcanyon at gmail.com> wrote:

> I just checked, and this in a stack script works perfectly:
>
> function pythonFile p
>    put the filename of this stack into fp
>    set the itemdel to "/"
>    put the long seconds into fn
>    replace "." with "_" in fn
>    put "_temp.py" after fn
>    put fn into item -1 of fp
>    put p into url ("file:" & fp)
>    return shell(quote & "<path to python>" & quote && quote & fp & quote)
> end pythonFile
>
> In the real world you'd probably want to set up a single external file (or
> set of files? if performance is important and the files need to be used
> repeatedly?) and check to make sure you're not accidentally overwriting
> something.
>
> On Thu, Jul 20, 2023 at 4:07 PM Bob Sneidar via use-livecode <
> use-livecode at lists.runrev.com> wrote:
>
>> Interesting. Ostensibly you could use low level file commands to create
>> they python file? Or does the Python script need to be processed by Python
>> first?
>>
>> Bob S
>>
>>
>> > On Jul 20, 2023, at 12:33 PM, Geoff Canyon via use-livecode <
>> use-livecode at lists.runrev.com> wrote:
>> >
>> > I've now figured out that calling a Python file from within LC is as
>> easy
>> > as:
>> >
>> > put shell(quote & "<path_to_python>" & quote && quote &
>> > "<path_to_python_file>" & quote)
>> >
>> > Then whatever gets print()'ed in Python gets returned to LC. Has anyone
>> > done work this way and has ideas on best practices for some sort of
>> >
>> > 1. generate python code
>> > 2. write python code to a file
>> > 3. write data to a (separate?) file
>> > 4. call the code
>> > 5. process the results
>> >
>> > ...loop?
>> >
>> > gc
>>
>>
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>


More information about the use-livecode mailing list