Cython

Description: Программирование на супер модном мега крутом языке Питон.

dyvniy M
Topic author, Администратор
Администратор
Avatar
dyvniy M
Topic author, Администратор
Администратор
Age: 36
Reputation: 1
Loyalty: 1
Posts: 3091
Joined: Wed, 10 Oct 2012
With us: 6 years 6 months
Профессия: Программист
Location: Россия, Москва
ICQ Website Skype VK

#1by dyvniy » Mon, 12 Nov 2018, 00:09:22

Способ ускорения питона.
Может получиться сделать из него экзешник
https://cython.readthedocs.io/en/latest/src/tutorial/cython_tutorial.html
Spoiler
Cython Hello World
As Cython can accept almost any valid python source file, one of the hardest things in getting started is just figuring out how to compile your extension.

So lets start with the canonical python hello world:

print("Hello World")
Save this code in a file named helloworld.pyx. Now we need to create the setup.py, which is like a python Makefile (for more information see Source Files and Compilation). Your setup.py should look like:

from distutils.core import setup
from Cython.Build import cythonize

setup(
ext_modules = cythonize("helloworld.pyx")
)
To use this to build your Cython file use the commandline options:

$ python setup.py build_ext --inplace
Which will leave a file in your local directory called helloworld.so in unix or helloworld.pyd in Windows. Now to use this file: start the python interpreter and simply import it as if it was a regular python module:

>>> import helloworld
Hello World
https://pypi.org/project/Cython/
pip install Cython --install-option="--no-cython-compile"
Image

dyvniy M
Topic author, Администратор
Администратор
Avatar
dyvniy M
Topic author, Администратор
Администратор
Age: 36
Reputation: 1
Loyalty: 1
Posts: 3091
Joined: Wed, 10 Oct 2012
With us: 6 years 6 months
Профессия: Программист
Location: Россия, Москва
ICQ Website Skype VK

#2by dyvniy » Wed, 27 Feb 2019, 16:07:21

ctypes
https://stackoverflow.com/questions/252417/how-can-i-use-a-dll-file-from-python
we can use python as dll without any C++ wrapper
Spoiler
For ease of use, ctypes is the way to go.

The following example of ctypes is from actual code I've written (in Python 2.5). This has been, by far, the easiest way I've found for doing what you ask.

import ctypes

# Load DLL into memory.

hllDll = ctypes.WinDLL ("c:\\PComm\\ehlapi32.dll")

# Set up prototype and parameters for the desired function call.
# HLLAPI

hllApiProto = ctypes.WINFUNCTYPE (
ctypes.c_int, # Return type.
ctypes.c_void_p, # Parameters 1 ...
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p) # ... thru 4.
hllApiParams = (1, "p1", 0), (1, "p2", 0), (1, "p3",0), (1, "p4",0),

# Actually map the call ("HLLAPI(...)") to a Python name.

hllApi = hllApiProto (("HLLAPI", hllDll), hllApiParams)

# This is how you can actually call the DLL function.
# Set up the variables and call the Python name with them.

p1 = ctypes.c_int (1)
p2 = ctypes.c_char_p (sessionVar)
p3 = ctypes.c_int (1)
p4 = ctypes.c_int (0)
hllApi (ctypes.byref (p1), p2, ctypes.byref (p3), ctypes.byref (p4))
The ctypes stuff has all the C-type data types (int, char, short, void*, and so on) and can pass by value or reference. It can also return specific data types although my example doesn't do that (the HLL API returns values by modifying a variable passed by reference).

In terms of the specific example shown above, IBM's EHLLAPI is a fairly consistent interface.

All calls pass four void pointers (EHLLAPI sends the return code back through the fourth parameter, a pointer to an int so, while I specify int as the return type, I can safely ignore it) as per IBM's documentation here. In other words, the C variant of the function would be:

int hllApi (void *p1, void *p2, void *p3, void *p4)
This makes for a single, simple ctypes function able to do anything the EHLLAPI library provides, but it's likely that other libraries will need a separate ctypes function set up per library function.

The return value from WINFUNCTYPE is a function prototype but you still have to set up more parameter information (over and above the types). Each tuple in hllApiParams has a parameter "direction" (1 = input, 2 = output and so on), a parameter name and a default value - see the ctypes doco for details

Once you have the prototype and parameter information, you can create a Python "callable" hllApi with which to call the function. You simply create the needed variable (p1 through p4 in my case) and call the function with them.
Image


Forum name: Python
Description: Программирование на супер модном мега крутом языке Питон.

Quick reply


Enter the code exactly as it appears. All letters are case insensitive.
Confirmation code
:) ;) :hihi: :P :hah: :haha: :angel: :( :st: :_( :cool: 8-| :beee: :ham: :rrr: :grr: :* :secret: :stupid: :music: View more smilies
   

Return to “Python”

Who is online (over the past 15 minutes)

Users browsing this forum: 1 guest