Cffi python cài đặt

description. The python-cffi package is obsolete. Selecting this package for installation will cause the python27-cffi package, which replaces this one, to be installed instead

categories. _lỗi thời

source package. python-cffi

maintainer[s]. Marco Atzeri [Use to report bugs or ask questions. . ]

Python is one of the most user-friendly programming languages. It is easy to learn, free to use, and you can extend its functionality however you like

Moreover, Python is arguably the most-used programming language in the Data Science and Machine Learning world. Excellent numerical libraries, like NumPy and SciPy, and remarkable deep learning frameworks, like PyTorch and TensorFlow, create a vast arsenal of tools for every programmer that likes to play with data and artificial neural networks

On the other hand, Python is an interpreted language, which can set an upper limit to how fast code can execute. This is not a good thing in the world of AI and big data. So, we do have a contradiction here. How can Python be the most used programming language in the field? What can we do to make it faster?

Well, we can write our demanding functions in another language [e. g. , C or C++] and leverage specific bindings to call these functions from Python. This is what these tremendous numerical libraries or deep learning frameworks do. So, if you are a Data Scientists or a Machine Learning engineer wanting to call CUDA functions, this story is for you

Learning Rate is a newsletter for those who are curious about the world of AI and MLOps. You’ll hear from me every Friday with updates and thoughts on the latest AI news and articles. Subscribe here

CFFI

In the last article, we did precisely this with

$ python3 build_cffi.py
3, a library that is part of the standard Python library. We saw what marshalling is and how Python differs from C in memory management. Finally, we saw how
$ python3 build_cffi.py
3 can help us call C functions with a simple example

Do You Hate How Slow Python Is? This Is How You Can Make It Run Faster

Delegate the challenging tasks to another player

towardsdatascience. com

In this story, let's take it up a notch with CFFI

CFFI so với. ctypes

$ python3 build_cffi.py
3 might be part of the Python standard library, but all this type declaration can be error-prone, and it does not scale to large projects/

Moreover,

$ python3 build_cffi.py
3 allows you to load a shared library directly into your Python program. With
$ python3 build_cffi.py
7, on the other hand, you can build a new Python module and import it just like any other Python library

Installation

$ python3 build_cffi.py
7 is not a part of the standard library. Thus, to use it, we need to install it first. We can do that easily using the Python package manager
$ python3 build_cffi.py
9

$ python3 -m pip install cffi

Please note that it is recommended to use a Python virtual environment to follow this tutorial, to avoid installing Python packages globally. This could break system tools or other projects

A simple example

In this example, we will call a function from's

$ python3 build_cffi.py
0 library. We will use this function to get the square root of a number

The first step is to create a python file that will build a Python module with the functionality we need; a library that provides access to all's goodies

To this end, copy and paste the code below

In this example, you create a binding with the

$ python3 build_cffi.py
1 function from's
$ python3 build_cffi.py
0 library. First, in the
$ python3 build_cffi.py
3 method, you pass the name of the Python module you want to build [e. g. ,
$ python3 build_cffi.py
4]

You also include the

$ python3 build_cffi.py
5 header from's
$ python3 build_cffi.py
0 library and, since we are using a library from the
$ python3 build_cffi.py
7 standard library, we don't have to provide the location of the library in
$ python3 build_cffi.py
8

Finally, you can run this script just like any other Python script

$ python3 build_cffi.py

This will produce

$ python3 build_cffi.py
9 and a
$ python3 build_cffi.py
20 file. Let's see how you can use the output of this call

Calling C from Python

Now you are ready to call the

$ python3 build_cffi.py
1 function from
$ python3 build_cffi.py
7. This is as simple as running the code below

First, from the library you created [i. e. ,

$ python3 build_cffi.py
4] you import the lib module. Inside, you can find the binding to the
$ python3 build_cffi.py
1 function. Finally, you can call this function with any float number

You can create bindings to other functions as well. For example, to create bindings with the

$ python3 build_cffi.py
25 or
$ python3 build_cffi.py
26 functions of the
$ python3 build_cffi.py
0 library, just declare them in
$ python3 build_cffi.py
28

$ python3 build_cffi.py
2Conclusion

Python is one of the most user-friendly programming languages, and it is arguably the most-used programming language in the Data Science and Machine Learning world

On the other hand, Python is an interpreted language, which can set an upper limit to how fast code can execute. But not all hope is lost; we can write our demanding functions in another language [e. g. , C or C++] and leverage specific bindings to call these functions from Python

In a previous article, we talked about

$ python3 build_cffi.py
3 and how we can use it to call
$ python3 build_cffi.py
7 libraries from Python. In this article, we used a more automated approach and saw how we could achieve this with
$ python3 build_cffi.py
7

Tiếp theo. we'll see how we can leverage Cython to speed up our Python code

About the Author

My name is Dimitris Poulopoulos, and I'm a machine learning engineer working for Arrikto. I have designed and implemented AI and software solutions for major clients such as the European Commission, Eurostat, IMF, the European Central Bank, OECD, and IKEA

If you are interested in reading more posts about Machine Learning, Deep Learning, Data Science, and DataOps, follow me on Medium, LinkedIn, or @james2pl on Twitter

Chủ Đề