Tables as kwargs for Python Classes
I've seen #35 already, but that only implements it for Python functions, any ways that this could be implemented for classes?
Could you be more specific about what you are asking?
Something like this
# Python
class MyClass:
def __init__(self, **kwargs): pass
-- Lua
MyClass{hello="hello"}
Don't the decorators work for you?
TypeError: cannot create 'cython_function_or_method' instances
I get the error, when one of my extensions to discord.py is loaded and subclasses a class, I wrapped with the decorator. Seems like the decorators are don't work when subclassing.
And when you wrap the init method instead of the class?
I have yet to try that, I will when I find time
I'm not sure if I'm doing anything wrong, but after testing it, it doesn't seem to work.
So for my use case I'm trying to make the classes and functions of a package be compatible with named parameters.
So I do this by doing this
import discord
def _update_classes(self, discord_pkg):
for discord_obj in self._get_package_objs(discord_pkg):
if inspect.isclass(discord_obj):
for cls_object in self._get_class_objs(discord_obj):
try:
cls_object = getattr(discord_obj, cls_object)
setattr(discord_obj, cls_object.__name__, lupa.unpacks_lua_table(cls_object))
except Exception:
pass
I get this error when trying to run the bot, since I the function above is called before the bot is started.
Traceback (most recent call last):
File "C:/Users/taluk/Documents/Coding Projects/Python/Musix/musix.py", line 27, in <module>
Musix(command_prefix='hey musix ').run('TOKEN')
File "lupa\_lupa.pyx", line 468, in lupa._lupa.unpacks_lua_table_method.wrapper
File "C:\Users\taluk\Documents\Coding Projects\Python\Musix\venv\lib\site-packages\discord\client.py", line 598, in run
return future.result()
File "C:\Users\taluk\Documents\Coding Projects\Python\Musix\venv\lib\site-packages\discord\client.py", line 579, in runner
await self.start(*args, **kwargs)
File "C:\Users\taluk\Documents\Coding Projects\Python\Musix\venv\lib\site-packages\discord\client.py", line 542, in start
await self.login(*args, bot=bot)
File "lupa\_lupa.pyx", line 466, in lupa._lupa.unpacks_lua_table_method.wrapper
TypeError: wrapper() got an unexpected keyword argument 'bot'
This is the library I'm using https://github.com/Rapptz/discord.py
By wrapping the methods, you are changing their signature so that they can no longer be called with Python keyword call semantics but only from Lua. Python calls don't pass a Lua table as argument.
I would suggest writing one or more dedicated wrapper objects that you only apply when passing objects into Lua, and not in general.
Thinking about this more, is it not possible to support normal Python calls also? Just to make it easier couldn't you just dump the arguments to a Lua table object and then call it from there.
Also how come some functions can be ran in both Python and Lua, and some can only be ran in Lua with the decorator?