Run a simple python script (string) from within a folder
https://www.mediafire.com/file/c6sdiqvn ... n.zip/file
Here is the python help file.
http://www.mediafire.com/file/n8h2x0r2s ... a.zip/file
No need to expand it, just put it in the pythonrun folder (from the previous link)
Here is an updated test file
Code: Select all
#inclib "python37"
'=====================================
Declare Sub Pyinit Cdecl Alias "Py_Initialize"()
Declare Function runstring Cdecl Alias "PyRun_SimpleString"(Byval s As zstring Ptr) As Integer
Declare Sub setname Cdecl Alias "Py_SetProgramName"(As String)
Declare Function PyMain Cdecl Alias "Py_Main"(As Long=0,Byref As zstring Ptr =0) As Long
Declare Sub Pyfinish Cdecl Alias "Py_Finalize"()
Sub runfile(filename As String)
runstring("exec(open('"+filename+"').read())")
End Sub
Sub savefile(filename As String,file_content As String)
Dim As Integer n
n=Freefile
If Open (filename For Binary Access Write As #n)=0 Then
Put #n,,file_content
Close
Else
Print "Unable to save " + filename
End If
End Sub
'=====================================
Dim As String f
f= "def fib(n):"+Chr(10)
f+=" v1, v2, v3 = 1, 1, 0"+Chr(10)
f+=" for rec in bin(n)[3:]:" +Chr(10)
f+=" calc = v2*v2" + Chr(10)
f+=" v1, v2, v3 = v1*v1+calc, (v1+v3)*v2, calc+v3*v3"+Chr(10)
f+=" if rec=='1': v1, v2, v3 = v1+v2, v1, v2"+Chr(10)
f+=" return v2"+Chr(10)
f+="for i in range(0,200):" +Chr(10)
f+=" print('fibonacci',i,'=',fib(i))"
setname "first" 'advised to set a program name
pyinit() 'start python (must do)
runstring (f)
Print
'string script by srvaldez
f="class Expr:"+Chr(10)
f+=" def __add__(self, other):"+Chr(10)
f+=" return Plus(self, other)"+Chr(10)
f+=" def __mul__(self, other):"+Chr(10)
f+=" return Times(self, other)"+Chr(10)
f+="class Int(Expr):"+Chr(10)
f+=" def __init__(self, n):"+Chr(10)
f+=" self.n = n"+Chr(10)
f+=" def d(self, v):"+Chr(10)
f+=" return Int(0)"+Chr(10)
f+=" def __str__(self):"+Chr(10)
f+=" return str(self.n)"+Chr(10)
f+="class Var(Expr):"+Chr(10)
f+=" def __init__(self, var):"+Chr(10)
f+=" self.var = var"+Chr(10)
f+=" def d(self, v):"+Chr(10)
f+=" return Int(self.var == v and 1 or 0)"+Chr(10)
f+=" def __str__(self):"+Chr(10)
f+=" return self.var"+Chr(10)
f+="class Plus(Expr):"+Chr(10)
f+=" def __init__(self, a, b):"+Chr(10)
f+=" self.e1 = a"+Chr(10)
f+=" self.e2 = b"+Chr(10)
f+=" def d(self, v):"+Chr(10)
f+=" return Plus(self.e1.d(v), self.e2.d(v))"+Chr(10)
f+=" def __str__(self):"+Chr(10)
f+=" return ""(%s +%s)""% (self.e1, self.e2)"+Chr(10)
f+="class Times(Expr):"+Chr(10)
f+=" def __init__(self, a, b):"+Chr(10)
f+=" self.e1 = a"+Chr(10)
f+=" self.e2 = b"+Chr(10)
f+=" def d(self, v):"+Chr(10)
f+=" return Plus(Times(self.e1, self.e2.d(v)), Times(self.e1.d(v), self.e2))"+Chr(10)
f+=" def __str__(self):"+Chr(10)
f+=" return ""(%s *%s)""% (self.e1, self.e2)"+Chr(10)
f+="if __name__ == ""__main__"":"+Chr(10)
f+=" x = Var(""x"")"+Chr(10)
f+=" a = Var(""a"")"+Chr(10)
f+=" b = Var(""b"")"+Chr(10)
f+=" c = Var(""c"")"+Chr(10)
f+=" e = a * x * x + b * x + c"+Chr(10)
f+=" print (""d(%s, x) =%s""% (e, e.d(""x"")))"+Chr(10)
Print
Print "result from srvaldez's script:"
runstring f
Print
Print "result from running a file from disk:"
'save time.py to disk
savefile("time.py","import time"+Chr(10)+"print('Today is', time.ctime())")
'run a .py file from disk
runfile("time.py")
Print
Print "factorial of 1000 = "
Dim As Double t=Timer
runstring("import math"+Chr(10)+"x=1000"+Chr(10)+"print(math.factorial(x))")
Print "time taken ";Timer-t
Print
Kill "time.py" 'optional
Pyfinish '(should do)
Print "Now run the interpreter .. exit() to finish "
Print PyMain()
Sleep
pythonXX.dll
pythonXX.zip - keep zipped
vcruntime140.dll
And for the help file pydoc_data.zip from above.(optional)
And the code above to call some basic functions.
Updated June 2021
https://www.mediafire.com/file/9rykf6rv ... 9.zip/file