Author Antony.Lee
Recipients Antony.Lee, bup, ethan.furman, josh.r
Date 2020-09-13.11:46:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1599997604.81.0.597033728535.issue34750@roundup.psfhosted.org>
In-reply-to
Content
To be honest, I don't really remember what exact use case I had in my mind 2 years ago (as I probably worked around it in one way or another).  However, one example that I can think of (and that I have actually implemented before) is auto-conversion of C #defines from a C header file to a Python-level enum (e.g. for semi-automatic generation of a ctypes wrapper):

    # A general header parser (untested, just an example)
    def parse_defines(header_file):
        d = {}
        for line in header_file:
            if line.startswith("#define"):
                _, k, v = line.split()
                d[k] = int(v)
        return d

    # Now wrapping a specific C library
    foo_defines = parse_defines("foo.h")

    class Foo(Enum):
        locals().update({k: v for k, v in foo_defines.items() if k.startswith("FOO_")})

        def some_method(self):
            ...
            # e.g. call a C function that takes a FOO_* as parameter.

Obviously I could always just replace the method by a free function, but that's true for (nearly) all methods.  In other words, it seems a bit "unfair" that it is easy to define methods on enums where all options are explicitly listed, but very hard(?) to do so on enums with programatically defined options.
History
Date User Action Args
2020-09-13 11:46:44Antony.Leesetrecipients: + Antony.Lee, ethan.furman, josh.r, bup
2020-09-13 11:46:44Antony.Leesetmessageid: <1599997604.81.0.597033728535.issue34750@roundup.psfhosted.org>
2020-09-13 11:46:44Antony.Leelinkissue34750 messages
2020-09-13 11:46:44Antony.Leecreate