Is there any example about writing Python extension with FBC?

General FreeBASIC programming questions.
D.J.Peters
Posts: 8297
Joined: May 28, 2005 3:28
Contact:

Re: Is there any example about writing Python extension with FBC?

Postby D.J.Peters » May 24, 2021 2:01

Cretin Ho wrote:I wanted to try writing a toy extension for Python in FBC. The point is just to prove it's possible or not. If no one ever think about it then my work will be much more difficult :)
Do you tryed my minimal "python.bi" file ?

The primary point are if you write in python: import MyModule

A extension with "MyModule.pyd" are loaded in the process space of the python environment.

Then initMyModule() from your exported FreeBASIC code are called to init your extension in the python script parsing AST.

NOTE: initMyModule() is "init" and the extension name "MyModule"

for example if your extension are named 2DVectorEngine "init2DVectorEngine" must be exported !

By the way initXYZ() is the one and only exported "sub" and visible by python !
(In contrast to a DLL wrapper where everything has to be exported)

The complete interface of your extension are registered and shared wth python with one call of Py_InitModule().

I tested only python 2.7 (but with Windows and Linux 32/64-bit) let me know if you run in to a problems with python 3.x

Maybe only one function declare and the #define of PYTHON_API_VERSION in python.bi must be changed for 3.x !

Joshy

Code: Select all

sub initMyModule() export
  methods(0).ml_name  = @"beep" ' <-- add a method "beep" (the visible name) without any argument
  methods(0).ml_meth  = @exportBeep ' <-- the real name of your code
  methods(0).ml_flags = METH_NOARGS
  methods(0).ml_doc   = @"Test calling beep."

Code: Select all

  methods(1).ml_name  = @"add"  <-- add a function "add" (the visible name) with arguments
  methods(1).ml_meth  = @exportAdd  ' <-- the real name of your code
  methods(1).ml_flags = METH_VARARGS
  methods(1).ml_doc   = @"Test of adding int numbers."
   ' last methods(2) is the NULL terminator

Code: Select all

  Py_InitModule4("MyModule",@methods(0)) ' <-- register your methods, functions, objects, classes etc.
end sub
dodicat
Posts: 6949
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Is there any example about writing Python extension with FBC?

Postby dodicat » Jun 08, 2021 20:18

I have updated my Quatermass Python experiment.
https://www.freebasic.net/forum/viewtopic.php?f=14&t=27718&p=262155#p262155
You can run Python from either the 32 bit or 64 bit folder.
I have popped in some Python code, mainly from the Rosetta code site.
Also a simplerun and a winrun file, tested on win 10, fb 1.08.0
srvaldez
Posts: 2729
Joined: Sep 25, 2005 21:54

Re: Is there any example about writing Python extension with FBC?

Postby srvaldez » Jun 09, 2021 11:14

Hi dodicat, long time no see
all your examples run as they should, are you aware of the WinPython distribution? https://winpython.github.io/
although the distributions are *.exe you can extract them using 7zip, no need to run the installer
dodicat
Posts: 6949
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Is there any example about writing Python extension with FBC?

Postby dodicat » Jun 09, 2021 11:54

Hi srvaldez.
Yes thanks, I just downloaded the .zips from the python site and took out the .dll's and the python39.zip which python handles in it's zipped form.
I don't have python installed, I don't use python very much.
But it is very easy to run Python code with freebasic via these .dll's
For graphics I think tkinter must be installed which is not too good IMO for out of the box stuff, so I don't have it.
The links from your site offers .exe downloads, as you say. (700 + Megabytes), have you tried any of them yourself, are graphics included?
It'll take me a while to download 700 mb, but I'll give it a try later.
Thanks again.
srvaldez
Posts: 2729
Joined: Sep 25, 2005 21:54

Re: Is there any example about writing Python extension with FBC?

Postby srvaldez » Jun 09, 2021 12:07

dodicat wrote:The links from your site offers .exe downloads, as you say. (700 + Megabytes), have you tried any of them yourself, are graphics included?
It'll take me a while to download 700 mb, but I'll give it a try later.
Thanks again.

yes, graphics are included also included is the Spyder IDE plus many of the popular libraries like NumPy and SciPy are also included
srvaldez
Posts: 2729
Joined: Sep 25, 2005 21:54

Re: Is there any example about writing Python extension with FBC?

Postby srvaldez » Jun 09, 2021 13:11

Drum

Code: Select all

import numpy as np
from scipy import special
def drumhead_height(n, k, distance, angle, t):
   kth_zero = special.jn_zeros(n, k)[-1]
   return np.cos(t) * np.cos(n*angle) * special.jn(n, distance*kth_zero)
theta = np.r_[0:2*np.pi:50j]
radius = np.r_[0:1:50j]
x = np.array([r * np.cos(theta) for r in radius])
y = np.array([r * np.sin(theta) for r in radius])
z = np.array([drumhead_height(1, 1, r, theta, 0.5) for r in radius])

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

Image
dodicat
Posts: 6949
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Is there any example about writing Python extension with FBC?

Postby dodicat » Jun 10, 2021 17:59

Thanks srvaldez, got that running OK. (in python).
The numpy import seems to be made up of many bits and pieces including dll's.
And I see turtle graphics also.
Anyway, if Creton ho can find a use for my link via freebasic then good luck.
dodicat
Posts: 6949
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Is there any example about writing Python extension with FBC?

Postby dodicat » Jun 12, 2021 14:11

srvaldez.
If you put my winrun.bas and the bitmap snake.bmp into the python-3.9.4amd64 folder, then you can run just about any python code including graphics with graphics.py and numpty.
I might try and isolate the components (dll's etc) so fb can use the full range of your link.
Thanks again srvaldez.

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests