scoder / lupa

Lua in Python
http://pypi.python.org/pypi/lupa
Other
1.01k stars 135 forks source link

question: using python functions on lua tables #259

Open amm0x41r opened 7 months ago

amm0x41r commented 7 months ago

Hello, Sorry for the noob question, but I can't quite figure it out. I'm trying to use the Python JSON lib instead of any of the lua ones due to issues with package installation on target, but I can't get even the minimal example to work. Here's what I'm trying:

import lupa
import json

lua = lupa.LuaRuntime(unpack_returned_tuples=True)
lua.execute("json = {}")
lua.globals()["json"]["encode"] = json.dumps
lua.globals()["json"]["decode"] = json.loads
lua_function = """
function construct_json_list()
    local out = {"a", "b", "c"}
    return json.encode("throwaway", out)
end
"""
lua.execute(lua_function)
print(lua.globals()["construct_json_list"]())

This simply throws TypeError: Object of type _LuaTable is not JSON serializable. Obviously json.dumps does not support Lua Tables.

tl;dr how to convert a lua table to a python object?

Any help is appreciated.

amm0x41r commented 7 months ago

Ok, so this seems to work:

def lua_table_to_python_obj(obj):
    if type(obj).__name__ == "_LuaTable":
        # Check if the Lua table is array-like
        keys = list(obj.keys())
        if keys == list(range(1, len(keys) + 1)):
            # Convert to Python list if keys are consecutive integers starting from 1
            return [lua_table_to_python(obj[key]) for key in keys]
        else:
            # Convert to Python dictionary otherwise
            return {key: lua_table_to_python(obj[key]) for key in obj}
    return obj

Is this truly what I'm meant to do? recursively convert tables to objects by hand? or am I missing something obvious?

scoder commented 6 months ago

I'd use lupa.lua_type(obj) == 'table', and try to avoid the list(range()), but yes, it would usually be something like this, depending on your specific needs. It's not clear that an implementation in Lupa would cover a sufficiently large number of use cases, but since users can always implement their own mapping, it might still work out.

Want to provide a PR that adds this as a method of the _LuaTable class, say, .copy_to_python()?

I'd expect that this can be made quite fast when using the Lua C-API instead of going through Lupa's Python API.