A modern two-way bridge between Python and Lua
Project description
A modern two-way bridge between Python and Lua.
Major Features
For Python Users
Integrate Lua into Python using CFFI as backend, which runs fast on both CPython and PyPy.
Run multiple Lua runtimes in one Python process.
Link to multiple lua libraries installed in system and use their luarocks.
Zero-copy data sharing between Python and Lua.
Seamless operations on Lua objects.
Hackable and extendable; customizable interacting behaviors.
Parallel numerical calculation using Lua to break the limit of Python’s GIL.
For Lua Users
Enrich Lua’s abilities by using Python’s modules.
Link to CPython and PyPy.
Seamless operations on Python objects.
Above all, ffilupa has plenty of fun!
Why ffilupa
Compare to lupa
lupa uses Cython as it’s backend, which is less friendly to PyPy and not extendable.
lupa doesn’t support Lua as the host language, which means you can’t use it in a Lua program.
lupa doesn’t support seamless operations on Lua objects.
lupa is not under actively development.
lupa inspired ffilupa a lot.
Compare to LunaticPython
Well, LunaticPython is too old and out of development for a long time.
LunaticPython doesn’t support multiple Lua runtimes.
LunaticPython leaks new features.
Installation
Before installing ffilupa, please check whether you have installed the development library of lua. On Ubuntu, you can install liblua5.3-dev or liblua5.2-dev:
$ sudo apt install [liblua5.3-dev|liblua5.2-dev]
On Mac OS X, you can use Homebrew:
$ brew install lua pkg-config
During installation, ffilupa will automatically find lua libraries through pkg-config.
Make sure you have installed Python 3.5+ in your system, including it’s development files and the suitable C compiler. On Ubuntu:
$ sudo apt install python3-dev
On Mac OS X:
$ brew install python
You’d better install the dependencies of ffilupa:
$ pip install cffi semantic_version
It’s optional; ffilupa will install them if you haven’t installed before.
Install stable version
For Python Users
$ pip install ffilupa
Install development version from Git branch
For Python Users
$ pip install git+https://github.com/TitanSnow/ffilupa.git
For Lua Users
Make sure you have installed luarocks.
$ git clone https://github.com/TitanSnow/ffilupa.git $ cd ffilupa $ luarocks make
FAQ about installation
How to deal with the exception ‘Required lua lib not found’?
Please check the installation of Lua. ffilupa currently only supports Lua 5.2 and 5.3. Then reinstall ffilupa in order to find the recently installed Lua libraries.
Does ffilupa support Windows?
ffilupa can support Windows, but not now. It might support Windows in next minor release.
Usage
For Python Users
A Brief Look
>>> import ffilupa
>>> lua = ffilupa.LuaRuntime()
>>> lua_func = lua.eval('''
... function(a, b) -- a plus b
... return a + b
... end
... ''')
>>> lua_func(22, 33)
55
Access Globals of Lua
>>> def greeting(name='World'): # greeting someone
... print('Hello, {}!'.format(name))
>>> lua._G.greeting = greeting
>>> lua.execute('greeting()')
Hello, World!
>>> lua.execute('greeting("John")')
Hello, John!
Zero-copy Data Sharing
>>> poem = {
... 'the': 'quick',
... 'brown': 'fox',
... 'jumps': 'over',
... }
>>> lua_func = lua.eval('''
... function(poem) -- finish the poem
... poem['lazy'] = 'doges'
... end
... ''')
>>> lua_func(poem)
>>> poem['lazy']
'doges'
Deal with Lua Table
>>> table = lua.table_from(poem)
>>> lua_func = lua.eval('''
... function(poem) -- shuffle the poem
... local new_poem = {}
... for k, v in pairs(poem) do
... new_poem[v] = k
... end
... return new_poem
... end
... ''')
>>> new_poem = lua_func(table)
>>> for k in sorted(new_poem):
... print(k, new_poem[k], end=' ')
doges lazy fox brown over jumps quick the
For Lua Users
A Brief Look
ffilupa = require 'ffilupa'
Fraction = ffilupa.import_module('fractions').Fraction
a = Fraction(1, 2)
b = Fraction(1, 3)
c = a + b -- c == 5/6
Extend Lua’s Abilities
Path = ffilupa.import_module('pathlib').Path
p = Path('.')
p = p / 'ffilupa'
for _, filename in pairs(p:iterdir()) do
print(filename) -- print all filename in ./ffilupa
end
Acknowledgements
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for ffilupa-4.0.0.dev2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59a0381017b7a91d31fb44f4460bb1741928b1d8b0f6de289d89846f724ddfdf |
|
MD5 | 2d2142b05aafb49dafd489589b57f29a |
|
BLAKE2b-256 | b12ffe1799a995e06308d43f0cba68577c066b98462a62150c190848c2527564 |
Hashes for ffilupa-4.0.0.dev2-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7ee69084630385f3cd0c6f7c7d5e81ede0f2257a8b6da7f1d11c2dfd9621b90 |
|
MD5 | aba5aca84668edaf9fb14ef54c96085c |
|
BLAKE2b-256 | f6f99dbbb4f1f6b311e7ec9cb2a5a086d403d2a607cd7e82ea717a166eb20f4b |
Hashes for ffilupa-4.0.0.dev2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 839650fae5840281996cde27dee79d38e6e4e99a0992fc09f8781f3684ddf376 |
|
MD5 | 54fd79d311161b47961e3482f7f69ae8 |
|
BLAKE2b-256 | 1c9d47b030c26c5763938f27e7abd8e100a61e8fb12ca42aa8bd537e3ab6f9e0 |
Hashes for ffilupa-4.0.0.dev2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 011d05068af0206521d2b86e6cb610a667f4622ed54c2e1a4cb529d26aa573e8 |
|
MD5 | 91793495406a020cd26cb4e16aed0721 |
|
BLAKE2b-256 | a86c5ae3c25ff2eeb9acc2248d1232e913272faac2e04d48febbcb62ea3b3d68 |
Hashes for ffilupa-4.0.0.dev2-cp36-abi3-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82f54d7ef4c743c86a98b57aabfac17b4fbbc0a9bb6488e8b5204d0e0463028b |
|
MD5 | dd9688859d1a78bfea591870da4b7bca |
|
BLAKE2b-256 | c6b9c66dba87720f4ed527afc5a0dc4ee011f9f52ec6cb74d6ac88a8241fdc1f |
Hashes for ffilupa-4.0.0.dev2-cp36-abi3-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c1fff3b15123c1ee31d2d8b1a3c98afe4b797d3594f934848a14ed681b9c869 |
|
MD5 | ca22e9db15e5e21c1a3c34aeff1f2121 |
|
BLAKE2b-256 | 55b309b67abbfe2cdf6fbe14a7a8207c413ab9695484804847d3ef6a2b7c5757 |