Seite 1 von 1

Möglich ? Purebasic, NumPy bzw. Python

Verfasst: 05.05.2025 11:11
von Franky_FR
Ich habe mich bisher noch kaum mit Python beschäftigt, bin aber gerade auf Cython gestoßen, also Compiler und C für Python.
Interessanter Gedanke : wir können ja schon immer ASM und jetzt C in PB nutzen.Wäre es nicht möglich, diese Möglichkeit um Python Code zu ergänzen, diesen mit dem generierten C Code an Cython weiterzuleiten, und somit auch Python nutzen zu können ? Mann bedenke die Möglichkeiten : NumPy, DeepData, Neuronale Netze ... und die anderen schönen Dinge in Python ...
Unsinn oder machbar ? :)

Re: Möglich ? Purebasic, NumPy bzw. Python

Verfasst: 05.05.2025 14:25
von NicTheQuick
Du willst in Purebasic Python programmieren und dann CPython nutzen um den Python-Teil zu kompilieren und mit Purebasic zu mischen?
Das klingt sehr komplex. Und wenn es dir nur darum geht NumPy oder PyTorch zu nutzen, dann musst du erst mal schauen, ob CPython damit überhaupt umgehen kann. Das ist ja auch nur ein Nischenprojekt. Abgesehen davon braucht man kein Python um z.B. PyTorch zu nutzen, sondern man nutzt einfach direkt LibTorch mit C++. Das ist dann aber auch wieder kompliziert, weil PureBasic kein OOP kann und dann müsste man schauen wie man das ganze wrappt. NumPy ist tatsächlich nur für Python gedacht, aber es hat eine C-API, die man ansprechen kann, aber wohl nicht den vollen Funktionsumfang mitbringt.

Ich sehe auch keinen Vorteil für solche Sachen PureBasic zu nutzen. Diese Welt ist viel zu schnelllebig und ich bin sicher niemand würde freiwillig irgendwelche PureBasic-Bindings dafür warten wollen. Ein ambitioniertes Projekt wäre es aber allemal.

Re: Möglich ? Purebasic, NumPy bzw. Python

Verfasst: 06.05.2025 10:11
von Franky_FR
NicTheQuick hat geschrieben: 05.05.2025 14:25 ...um den Python-Teil zu kompilieren und mit Purebasic zu mischen?
Das klingt sehr komplex.
Hallo Nic,
vielleicht ist das nicht so komplex. Auf der Webseite steht:
Cython gives you the combined power of Python and C to let you

write Python code that calls back and forth from and to C or C++ code natively at any point.
easily tune readable Python code into plain C performance by adding static type declarations, also in Python syntax."
Integrate natively with existing code and data from legacy, low-level or high-performance libraries and applications.
Und ebenfalls,vielleicht besonders wichtig:

All of this makes Cython the ideal language for wrapping external C libraries, embedding CPython into existing applications, and for fast C modules that speed up the execution of Python code.

In der Cython Wiki steht zum Kompilieren folgendes:

Cython code must, unlike Python, be compiled. This happens in two stages:

A .pyx or .py file is compiled by Cython to a .c file, containing the code of a Python extension module.

The .c file is compiled by a C compiler to a .so file (or .pyd on Windows) which can be import-ed directly into a Python session.
setuptools takes care of this part. Although Cython can call them for you in certain cases.

There are several ways to build Cython code:
Run the cythonize command-line utility. This is a good approach for compiling a single Cython source file directly to an extension. A source file can be built “in place” (so that the extension module is created next to the source file, ready to be imported) with cythonize -i filename.pyx.
or
Run the cython command-line utility manually to produce the .c file from the .pyx file, then manually compiling the .c file into a shared object library or DLL suitable for import from Python. (These manual steps are mostly for debugging and experimentation.) "
Ich habe mal die beiden Kommandozeilen Varianten rausgesucht.
Wenn man nun einmal überlegt, entweder Fred oder mit einem Tool würde man den Pythoncode per Kommandozeile übergeben und mit dem C-Code von PB kombinieren. Das sollte eigentlich möglich sein, und man könnte es mit Kommandozeilen-Aufrufen erledigen.Wahrscheinlich gibt es auch elegantere Wege.
Der umgekehrte Weg geht natürlich auch: In Cython den C-Code erzeugen und in den PB-C-Code integrieren, wenn man selber frickeln will.


Ich sehe auch keinen Vorteil für solche Sachen PureBasic zu nutzen. Diese Welt ist viel zu schnelllebig und ich bin sicher niemand würde freiwillig irgendwelche PureBasic-Bindings dafür warten wollen. Ein ambitioniertes Projekt wäre es aber allemal.
Unter Umständen muss man dann nicht viel Wartung betreiben. Vielleicht würde in PB ja so etwas wie beim C-Code ( !) oder ein Befehl wie
BEGINPython und ENDPython eingefügt. Und der Vorteil dürfte klar sein: die große Befehlsvielfalt von PB mit der Power von Python-Modulen,
die so in PB nicht zu finden sind, da gibt es ja einige.
Ich bin leider nicht ein erfahrener Programmierer, schreibe nur gelegentlich ein kurzes Programm.
Du hast nicht zufällig Lust, mal ein kleines Experiment beim Kombinieren von PB-C-Code und Py-Code mit Cython zu starten ? :)