Code Monkey home page Code Monkey logo

py-lua's Introduction

py-lua

workflow codecov

Bridge between Python and Lua. This project has been started to provide easy binding between python and lua programming languages. Currently tested with lua version 5.4 and python.

Currently py-lua supports:

  • Importing python module into lua program.
  • Getting string, float, boolean and None data types from python.
  • Convertion between python dict and lua table.
  • Calling and extracting return values of functions with simple data types.
  • Working with python generators and lua threads.
  • Support to use Object Oriented Programming between the languages.

Example

using python inside lua

# mymodule.py
PI = 3.14
e = 2.71

def add(x, y):
    return x + y

def sub(x, y):
    return x - y

def concatenate(x, y):
    return x + y

class Rectangle:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def get_area(self):
        return self.x * self.y

    def __str__(self):
        return f"Rectangle(x={self.x}, y={self.y})"
-- main.lua
require("pylua")

pymodule = Python.PyLoad("mymodule")

print("12 + 13 = ", pymodule.add(12, 13))
-- 12 + 13 = 25
print("e - pi = ", pymodule.sub(pymodule.PI, pymodule.e))
-- 3.17 - 2.71 = 0.43
print(pymodule.concatenate("Lua loves ", "Python"))
-- Lua loves Python

rect = pymodule.Rectangle(4, 6)
print(rect.get_area())
-- 24
print(tostring(rect))
-- Rectangle(x=4.0, y=6.0)

Using lua inside python

-- mymodule.lua

function celsius_to_fahrenheit(x)
    return (x * 1.8) + 32
end

function fahrenheit_to_celsius(x)
    return (x - 32) * .5556
end
# main.py
import pylua
    
lua_module = pylua.LuaLoad("mymodule.lua")

ctf_40 = lua_module.celsius_to_fahrenheit(40)
ftc_n40 = lua_module.fahrenheit_to_celsius(-40)

print(f"{ctf_40 = }, {ftc_n40 = }") # ctf_40 = 104.0, ftc_n40 = -40.0032

Building

To compile py-lua. First install the required dependencies:

  • python3
  • lua5.4
  • premake5
  • gcc (linux)
  • Visual Studio Code (windows)

Clone the repository

git clone https://github.com/Vipul-Cariappa/py-lua.git

Update premake5.lua file: Replace /path/to/python/header and /path/to/lua/header with actual header paths and replace /path/to/python/shared_libraries and /path/to/lua/shared_libraries with actual paths to shared libraries.

After updating paths run premake with desired action:

premake5 [action]

You can now use compiler of your choice to build the pylua shared library.

Copy the shared library pylua.so file to the working directory of your project.

If you face any problems while building please ask for help here.

Contribution

All contributions are welcomed. You can fork the project and create new PR, if you want to contribute. If the PR is related to a bud, creating a issue before creating the PR is recommended.

Discussions

If you have any problems or have any feature request please feel free to post new discussion. I will try to get at all of them.

py-lua's People

Contributors

vipul-cariappa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

luxingxiao

py-lua's Issues

Can't Import some module

Describe the bug
The bug is i can't import some of modules. regular python module is ok some like os , sys or .. and i get results.

To Reproduce
I use your example and create two file one lua file one python file.

Expected behavior
I expected can handle all module on pylua.

System Environment (please complete the following information):

  • OS: linux Ubuntu 16.04
  • Python Version: 3.8
  • Lua Version: 5.3

Additional context
When i use to import asyncio i get error :

Python Traceback:
File "./test.py", line 1, in
  <module>
File "/usr/lib/python3.8/asyncio/__init__.py", line 8, in
  <module>
File "/usr/lib/python3.8/asyncio/base_events.py", line 40, in
  <module>
File "/usr/lib/python3.8/asyncio/events.py", line 14, in
  <module>
File "/usr/lib/python3.8/contextvars.py", line 1, in
  <module>

  ImportError('/usr/lib/python3.8/lib-dynload/_contextvars.cpython-38-x86_64-linux-gnu.so: undefined symbol: PyContext_Type')

stack traceback:
        [C]: in field 'PyLoad'
        test.lua:4: in main chunk
        [C]: in ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.