Re: Dynamic classes

[email protected] (Stefan Ram) 21 May 2025 11:20:55 GMT
Newsgroups comp.lang.python
Organization Stefan Ram
Message-ID <[email protected]>
Left Right <[email protected]> wrote or quoted:
>I find that it's generally more convenient to do this using similar code:
>def constructor(flag1, flag2):
>    class _Hidden:

  That tracks, but 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], and I am not
  really sure how your way would fit in with that.

  [0]

globals_dict = globals()
for name, props in element_properties.items():
    cls = type(name.capitalize(), (Element,), {'element_properties': props})
    globals_dict[name.capitalize()] = cls

  [1]

element_properties = {
    'title': {
        'level': IS_BLOCK,
        'contents': CANT_CONTAIN_ELEMENTS,
        'open_tag': None,
        'close_tag': None,
        'use_check': False,
    },
    'author': {
        'level': IS_BLOCK,
        'contents': CANT_CONTAIN_ELEMENTS,
        'open_tag': None,
        'close_tag': None,
        'use_check': False,
    },
    'language': {
        'level': IS_BLOCK,
        'contents': CANT_CONTAIN_ELEMENTS,
        'open_tag': None,
        'close_tag': None,
        'use_check': False,
    },
    'h1': {
        'level': IS_BLOCK,
        'contents': CAN_CONTAIN_ELEMENTS,
        'open_tag': '<h1>',
        'close_tag': '</h1>',
        'use_check': True,
    },
    'toc': {
        'level': IS_BLOCK,
        'contents': CANT_CONTAIN_ELEMENTS,
        'open_tag': None,
        'close_tag': None,
        'use_check': False,
    },
    'head': {
        'level': IS_BLOCK,
        'contents': CANT_CONTAIN_ELEMENTS,
        'open_tag': None,
        'close_tag': None,
        'use_check': False,
    },
    'par': {
        'level': IS_BLOCK,
. . .