# Has anyone experimented with using Python within LC?

Geoff Canyon gcanyon at gmail.com
Sat Jul 22 13:11:52 EDT 2023

```Both are turing complete, so there's technically nothing one can do that
the other can't, but some examples:

Suppose you want to generate prime numbers. In LiveCode you might write
this algorithm: https://github.com/gcanyon/primelist

Generating the primes up to 1,000,000 takes over 1.3 seconds on my M1
MacBook.
In Python, using a less efficient algorithm, the same takes 0.3 seconds.
Import the numpy library and use a more efficient algorithm like so:

def prime6(upto=1000000):
primes=arange(3,upto+1,2)
isprime=ones((upto-1)//2,dtype=bool)
for factor in primes[:int(sqrt(upto))//2]:
if isprime[(factor-2)//2]: isprime[(factor*3-2)//2::factor]=0
return insert(primes[isprime],0,2)

And you can get the same result in 0.002 seconds -- over 500 times faster.

In LiveCode, if you want to handle large integer math, you write your own
library, which takes a ton of time and work to optimize. I did this five
years ago: https://github.com/gcanyon/bignum which enables this:

on mouseUp
repeat 1000 times
put "1234567890" after x
end repeat
put the long seconds into T
get bigTimes(x,x)
put the long seconds - T into T
put T && length(it)
end mouseUp

I worked hard to optimize that library, and I'm happy to say that runs in

This runs in 0.0004 seconds:

from time import perf_counter_ns
from math import log10

n = 1234567890
for _ in range(999):
n = n * 10**10 + 1234567890

start = perf_counter_ns()
n2 = n*n
end = perf_counter_ns()
print((end - start)/10**9,log10(n2),n2%10**10)

Python has numerous syntactic niceties. For example, if you want a function
to return a fibonacci list, multiple assignments are nice:

def fib(n):
i = j = 1
R = []
for _ in range(n):
R.append(i)
i,j = j,i+j
return(R)

print(fib(5))
[1, 1, 2, 3, 5]

And list comprehensions are wonderful. If you want a dictionary containing
perfect squares for odd numbers:

L = {i:i**2 for i in range(1,20) if i % 2 == 1}
print(L)
{1: 1, 3: 9, 5: 25, 7: 49, 9: 81, 11: 121, 13: 169, 15: 225, 17: 289, 19:
361}

Obviously you could do that with a "step", but the if can be more complex
than that.

So reasons you might use Python include:

1. Efficient and convenient syntax
2. Speed

gc

On Fri, Jul 21, 2023 at 7:10 PM Bob Sneidar via use-livecode <
use-livecode at lists.runrev.com> wrote:

> Out of curiosity, what can be done in in Python that cannot be done in LC
> Script?
>
> Sent from my iPhone
>
> > On Jul 21, 2023, at 18:37, Alan Stenhouse via use-livecode <
> use-livecode at lists.runrev.com> wrote:
> >
> > ﻿Hi Geoff
> >
> > Sorry for not replying earlier but looks like you've got it sorted. I
> did a bit on this years ago and am contemplating it again. Just looked for
> my old stack and see this script for running a python script and putting
> the output into the "output" fld:
> >
> >
> > on runPythonScript pScript
> >   put the tempname &".py" into tFile
> >   put pScript into URL ("binfile:" & tFile)
> >   put shell("<path_to_python>" && tFile) into fld "output"
> > end runPythonScript
> >
> > Was starting to provide an interface for experiment parameters for the
> scripts I was running (for some Turi ML classifications) but the project
> paused...
> >
> > cheers
> > Alan
> > _______________________________________________
> > 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
> _______________________________________________
> 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
>
```