This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ethan.furman
Recipients barry, eli.bendersky, ethan.furman, serhiy.storchaka
Date 2015-03-16.17:59:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1426528768.02.0.935733833961.issue23673@psf.upfronthosting.co.za>
In-reply-to
Content
Patch adds Enum._convert which is a class method that handles:

  - creating the new Enum
  - adding the appropriate members
  - adding the new Enum to the module's namespace (which is a passed parameter)
  - replacing the __reduce_ex__ method to return just the member's name

The change to Enum is:

    @classmethod
    def _convert(cls, name, module, filter, source=None):
        """
        Create a new Enum subclass that replaces a collection of global constants
        """
        # convert all constants from source (or module) that pass filter() to
        # a new Enum called name, and export the enum and its members back to
        # module;
        # also, replace the __reduce_ex__ method so unpickling works in
        # previous Python versions
        module_globals = vars(sys.modules[module])
        if source:
            source = vars(source)
        else:
            source = module_globals
        members = {name: value for name, value in source.items()
                if filter(name)}
        cls = cls(name, members, module=module)
        cls.__reduce_ex__ = _reduce_ex_by_name
        module_globals.update(cls.__members__)
        module_globals[name] = cls
        return cls

In use it looks like:

   IntEnum._convert(
        'AddressFamily',
        __name__,
        lambda C: C.isupper() and C.startswith('AF_'))

or

  _IntEnum._convert(
        '_SSLMethod', __name__,
        lambda name: name.startswith('PROTOCOL_'),
        source=_ssl)


ssl.py, socket.py, signal.py, and http/__init__.py have been updated to use this method.
History
Date User Action Args
2015-03-16 17:59:28ethan.furmansetrecipients: + ethan.furman, barry, eli.bendersky, serhiy.storchaka
2015-03-16 17:59:28ethan.furmansetmessageid: <1426528768.02.0.935733833961.issue23673@psf.upfronthosting.co.za>
2015-03-16 17:59:27ethan.furmanlinkissue23673 messages
2015-03-16 17:59:26ethan.furmancreate