Execute python script from Assembly - python

I am planning on making an OS from scratch using Python. However I only know how to make it by writing in Assembly.
Is it possible for me to still write the kernel in Assembly, convert it to a binary and during boot execute the Python script?
I hope this made sense

I think you would be interested in this project:
https://github.com/Maratyszcza/PeachPy
A comment from LtU:
PeachPy is a Python framework for writing high-performance assembly kernels.
PeachPy aims to simplify writing optimized assembly kernels while preserving all optimization opportunities of traditional assembly.
You can use the same code to generate assembly for Windows, Unix, and Golang assembly. The library handles the various ABIs automatically. I haven't seen >this cool project before.
Among the cool features is the ability to invoke the generated assembly as regular Python functions. Nice.

Related

Designing an interface between C++ and Python

I have code for a physics simulation in C++. I am trying to solve a control problem using Deep Reinforcement Learning. Most of the popular packages such as keras, pytorch, are based on Python. So, my machine learning code resides in Python and the physics simulator code is in C++.
At every iteration of the machine learning algorithm, I require a call to the C++ program and want the program to persist its state (maintain the variable values). One way I came up with was reading and writing variable values to files. But this didn't seem scalable considering the high number of variables in the program. I googled and found Boost Python library for wrapping my entire C++ code, so that it might be available to the Python program. My question is by doing this do I lose all the speed up of running the code in C++?
Also, I came across numba, which according to the creators is specifically designed for speed up for Scientific Computing and can reach speeds as fast as native C++. But, this would essentially mean re-writing the entire code in Python. Will this be a better choice?
I am on a strict timeline for my project and any advice on which way I should go will be much appreciated.

Migrate Halcon code to OpenCV

I am developing a solution using a comercial computer vision software called Halcon. I am thinking on migrating or convert my solution to OpenCV in Python. I will like to start developing my other computer vision solution in Halcon because the IDE is incredible, and them generate a script to migrate them to OpenCV.
Does anyone know any library for this task?
I will like to start developing an open source SDK to convert Halcon to OpenCV. I and thinking to start developing all internal function from Halcon to Python. Any advice?
I wouldn't invest time in such an effort. These are some reasons:
For simple functions (blur, erode, dilate) Halcon and OpenCV have different implementations of the same function and probably you will have slightly different outputs (one may round up and the other round down, for example). A complex program that runs properly in Halcon may fail in OpenCV for these small differences (a butterfly effect).
Probably there are complex functions (pattern matching algorithms, deep learning), that exist in one environment and don't exist in the other.
OpenCV users typically don't have Halcon IDE. I like OpenCV because it is open source and free, I won't consider using Halcon IDE because it is not. Moreover OpenCV has many functions and it improves very fast (I don't know Halcon). If Halcon was much better than OpenCV (more popular, faster, cross platform, with all OpenCV functions implemented in Halcon) and my project had the money, I would use Halcon IDE for development and I would use it in production as well (and completely ignore OpenCV). In either case, I wouldn't use a tool that converts one to the other. It is like writing the Linux kernel with Visual Studio, maybe someone does it, but I guess it is not the majority.
If I had to spend time in an open source project, I would write a better OpenCV interface (as Vladimir Perković suggests). There are some efforts in that direction (probably there is something else if you search):
Interactive Visual Debugging: https://docs.opencv.org/3.3.0/d7/dcf/tutorial_cvv_introduction.html I haven't tried that, but I have written and used similar tools in the past and is usually enough.
Debug images in Visual Studio: https://docs.opencv.org/2.4/doc/tutorials/introduction/windows_visual_studio_image_watch/windows_visual_studio_image_watch.html
What I really want to see some day is a visual programming language editor, where you drag and drop boxes and connect them (like in a flowchart). Something similar to LabVIEW, VPL, but open source and for OpenCV.
It looks like the future (or the present) is web based, and there are many flowchart programming tools that are open source like Node-Red, No-Flo UI or Apache NiFi. Probably you can modify them to use OpenCV functions (a quick search returns this and this)
Blender is a 3D editor (with a video editor and many other features) that you can program in python and also has a flowchart programming language ("Compositing Nodes"). I don't see a better place to integrate OpenCV!
It depends on which Halcon functionalities are you using and why you want to do it. The question appears to be very general. I would recommend you to convert your Halcon Program to C++ and write a wrapper function to pass arguments to/from your openCV program.This would be the simplest option to provide interaction between your opencv and halcon program. Hope it helps.
This is unfortunately not possible because Halcon itself is not an open source library and every single function is locked.
The reason behind is runtime licencing.

Use Cython as Python to C Converter

I have huge Python modules(+8000 lines) .They basically have tons of functions for interacting with a hardware platform via serial port by reading and writing to hardware registers.
They are not numerical algorithms. So application is just reading/writing to hardware registers/memory. I use these libraries to write custom scripts. Eventually, I need to move all these stuff to be run in an embedded processor on my hardware to have finer control, then I just kick off the event from PC and the rest is in hardware.
So I need to convert them to C.If I can have my scripts be converted to C by an automatic tool, that would save me a huge time. This is why I got attracted to Cython. Efficiency is not important my codes are not number crunchers. But generated code should be relatively small to fit in my limited memory (few hundreds of Kilobytes).
Can I use Cython as converter for my custom Python scripts to C? My guess is yes, in which case can I use these .c files to be run in my hardware? My guess is not since I have to have Cython in my hardware as well for them to run. But if just creates some .c files, I can go through and make it standalone since code is not using much of features of Python it just use it as a fast to implement script.
Yes, at its core this is what Cython does. But ...
You don't need Cython, however, you do need libpython. You may feel like it doesn't use that many Python features, but I think if you try this you'll find it's not true -- you won't be able to separate your program from its dependence on libpython while still using the Python language.
Another option is PyPy, specifically it's translation toolchain, NOT the PyPy Python interpreter. It lets you translate RPython, a subset of the Python language, into C. If you really aren't using many Python language features or libraries, this may work.
PyPy is mostly known as an alternative Python implementation, but it is also a set of tools for compiling dynamic languages into various forms. This is what allows the PyPy implementation of Python, written in (R)Python, to be compiled to machine code.
If C++ is available, Nuitka is a Python to C++ compiler that works for regular Python, not just RPython (which is what shedskin and PyPy use).
If C++ is available for that embedded platform, there is shed skin, it converts python into c++.

How to export python functions and reuse them in another app

I was wondering if there is any way to export python functions to dll. There is py2exe and I can successfully create exe file. My program should be used by another program written in delphi (there is possibility of importing dll's in delphi).
So I was wondering what would be the best way to connect those 2 applications.
Now I can only create exe, execute process in delphi and communicate in some way. But I don't think that's nice way. Maybe somebody have any experience in this subject?
There are some pretty big challenges to making languages work well together. As a simple alternative to trying to hook python code directly into delphi, you could consider using something like an xmlrpc server to provide python functionality remotely.
http://docs.python.org/library/xmlrpclib.html
Of course, any protocol could be used; xmlrpc just has some useful server utilities in python and presumably has a client library in delphi.
You can re-use python functions via modules. Integrating with other language is a different task all together.
py2exe packs all dependent modules and additional dlls required by an application so that it can be easily distributed without creating any installation dependencies for the user.
Cross - language integration requires some work. To integrate with "C", there are various ways like cython etc. If there is similar facility available with delphi, you might be able to use it.
Check out some of these references, it will make it more clear to you on what direction to take.
http://wiki.python.org/moin/IntegratingPythonWithOtherLanguages
http://www.atug.com/andypatterns/pythonDelphiTalk.htm
http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=python+delphi (Google search)

How to compile Python scripts for use in FORTRAN?

Although I found many answers and discussions about this question, I am unable to find a solution particular to my situation. Here it is:
I have a main program written in FORTRAN.
I have been given a set of python scripts that are very useful.
My goal is to access these python scripts from my main FORTRAN program. Currently, I simply call the scripts from FORTRAN as such:
CALL SYSTEM ('python pyexample.py')
Data is read from .dat files and written to .dat files. This is how the python scripts and the main FORTRAN program communicate to each other.
I am currently running my code on my local machine. I have python installed with numpy, scipy, etc.
My problem:
The code needs to run on a remote server. For strictly FORTRAN code, I compile the code locally and send the executable to the server where it waits in a queue. However, the server does not have python installed. The server is being used as a number crunching station between universities and industry. Installing python along with the necessary modules on the server is not an option. This means that my “CALL SYSTEM ('python pyexample.py')” strategy no longer works.
Solution?:
I found some information on a couple of things in thread Is it feasible to compile Python to machine code?
Shedskin, Psyco, Cython, Pypy, Cpython API
These “modules”(? Not sure if that's what to call them) seem to compile python script to C code or C++. Apparently not all python features can be translated to C. As well, some of these appear to be experimental. Is it possible to compile my python scripts with my FORTRAN code? There exists f2py which converts FORTRAN code to python, but it doesn't work the other way around.
Any help would be greatly appreciated. Thank you for your time.
Vincent
PS: I'm using python 2.6 on Ubuntu
One way or another, you'll need to get the Python runtime on your server, otherwise it won't be possible to execute Python bytecode. Ignacio is on the right track with suggesting invoking libpython directly, but due to Fortran's parameter-passing conventions, it will be a lot easier for you to write a C wrapper to handle the interface between Fortran and the CPython embedding API.
Unfortunately, you're doing this the hard way -- it's a lot easier to write a Python program that can call Fortran subroutines than the other way around.
You don't want any of those. What you should do is use FORTRAN's FFI to talk with libpython and frob its API.

Categories

Resources