Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197488

Re: Dynamic classes (Posting On Python-List Prohibited)

From Lawrence D'Oliveiro <ldo@nz.invalid>
Newsgroups comp.lang.python
Subject Re: Dynamic classes (Posting On Python-List Prohibited)
Date 2025-05-22 00:19 +0000
Organization A noiseless patient Spider
Message-ID <100lqj6$33d8i$7@dont-email.me> (permalink)
References <mailman.67.1747767982.3008.python-list@python.org> <dynamic-20250521121804@ram.dialup.fu-berlin.de>

Show all headers | View raw


On 21 May 2025 11:20:55 GMT, Stefan Ram wrote:

> ... I have this setup where I lay out a table to map out my markup
> language [1] and then use some dynamic code to spin up the classes
> based on that table [0] ...

The main way I like to do it is start with a basic class object,
assign some basic attributes like __name__ and __doc__, and
dynamically attach additional members to it with setattr.

Here’s a partial example, taken from <https://gitlab.com/ldo/dbussy>.
Note the heavy use of lexical binding:

    class proxy(BusPeer.Object.ProxyInterface) :
        # class that will be constructed, to be instantiated for a given connection,
        # destination and path.

        # class field _iface_name contains interface name.
        __slots__ = ("_parent", "_conn", "_dest", "_path", "_timeout")

        def __init__(self, *, parent, connection, dest, path, timeout = DBUS.TIMEOUT_USE_DEFAULT) :
            if is_async :
                assert connection.loop != None, "no event loop to attach coroutines to"
            #end if
            self._parent = parent
            self._conn = connection
            self._dest = dest
            self._path = path
            self._timeout = timeout
        #end __init__

        # rest filled in dynamically below.

    #end proxy

    def def_method(intr_method) :
        # constructs a method-call method.

        if is_async :

            async def call_method(self, *args, **kwargs) :
                message = dbus.Message.new_method_call \
                  (
                    destination = self._dest,
                    path = dbus.unsplit_path(self._path),
                    iface = self._iface_name,
                    method = intr_method.name
                  )
                _append_args(message, intr_method, args, kwargs)
                if intr_method.expect_reply :
                    reply = await self._conn.send_await_reply(message, self._timeout)
                    result = reply.expect_return_objects(intr_method.out_signature)
                else :
                    message.no_reply = True
                    self._conn.send(message)
                    result = None
                #end if
                return \
                    result
            #end call_method

        else :

            def call_method(self, *args, **kwargs) :
                message = dbus.Message.new_method_call \
                  (
                    destination = self._dest,
                    path = dbus.unsplit_path(self._path),
                    iface = self._iface_name,
                    method = intr_method.name
                  )
                _append_args(message, intr_method, args, kwargs)
                if intr_method.expect_reply :
                    reply = self._conn.send_with_reply_and_block(message, self._timeout)
                    result = reply.expect_return_objects(intr_method.out_signature)
                else :
                    message.no_reply = True
                    self._conn.send(message)
                    result = None
                #end if
                return \
                    result
            #end call_method

        #end if

    #begin def_method
        call_method.__name__ = intr_method.name
        call_method.__doc__ = \
            (
                "method, %(args)s, %(result)s"
            %
                {
                    "args" :
                        (
                            lambda : "no args",
                            lambda : "args %s" % dbus.unparse_signature(intr_method.in_signature),
                        )[len(intr_method.in_signature) != 0](),
                    "result" :
                        (
                            lambda : "no result",
                            lambda : "result %s" % dbus.unparse_signature(intr_method.out_signature),
                        )[len(intr_method.out_signature) != 0](),
                }
            )
        setattr(proxy, intr_method.name, call_method)
    #end def_method

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar


Thread

Re: Dynamic classes Left Right <olegsivokon@gmail.com> - 2025-05-20 08:03 +0200
  Re: Dynamic classes ram@zedat.fu-berlin.de (Stefan Ram) - 2025-05-21 11:20 +0000
    Re: Dynamic classes (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-05-22 00:19 +0000

csiph-web