lupa icon indicating copy to clipboard operation
lupa copied to clipboard

Tables as kwargs for Python Classes

Open showment opened this issue 6 years ago • 10 comments

I've seen #35 already, but that only implements it for Python functions, any ways that this could be implemented for classes?

showment avatar Sep 24 '19 02:09 showment

Could you be more specific about what you are asking?

scoder avatar Sep 24 '19 18:09 scoder

Something like this

# Python
class MyClass:
   def __init__(self, **kwargs): pass 
-- Lua
MyClass{hello="hello"}

showment avatar Sep 26 '19 22:09 showment

Don't the decorators work for you?

scoder avatar Sep 27 '19 13:09 scoder

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.

showment avatar Sep 29 '19 23:09 showment

And when you wrap the init method instead of the class?

scoder avatar Sep 30 '19 04:09 scoder

I have yet to try that, I will when I find time

showment avatar Sep 30 '19 05:09 showment

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

showment avatar Dec 24 '19 05:12 showment

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.

scoder avatar Dec 24 '19 07:12 scoder

Alright thanks

Sent with GitHawk

showment avatar Dec 24 '19 17:12 showment

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?

showment avatar Dec 26 '19 02:12 showment