Create a new Enum subclass that replaces a collection of global constants
(etype, name, module, filter, source=None, *, boundary=None)
| 2136 | raise TypeError('enum mismatch:\n %s' % '\n '.join(failed)) |
| 2137 | |
| 2138 | def _old_convert_(etype, name, module, filter, source=None, *, boundary=None): |
| 2139 | """ |
| 2140 | Create a new Enum subclass that replaces a collection of global constants |
| 2141 | """ |
| 2142 | # convert all constants from source (or module) that pass filter() to |
| 2143 | # a new Enum called name, and export the enum and its members back to |
| 2144 | # module; |
| 2145 | # also, replace the __reduce_ex__ method so unpickling works in |
| 2146 | # previous Python versions |
| 2147 | module_globals = sys.modules[module].__dict__ |
| 2148 | if source: |
| 2149 | source = source.__dict__ |
| 2150 | else: |
| 2151 | source = module_globals |
| 2152 | # _value2member_map_ is populated in the same order every time |
| 2153 | # for a consistent reverse mapping of number to name when there |
| 2154 | # are multiple names for the same number. |
| 2155 | members = [ |
| 2156 | (name, value) |
| 2157 | for name, value in source.items() |
| 2158 | if filter(name)] |
| 2159 | try: |
| 2160 | # sort by value |
| 2161 | members.sort(key=lambda t: (t[1], t[0])) |
| 2162 | except TypeError: |
| 2163 | # unless some values aren't comparable, in which case sort by name |
| 2164 | members.sort(key=lambda t: t[0]) |
| 2165 | cls = etype(name, members, module=module, boundary=boundary or KEEP) |
| 2166 | return cls |
| 2167 | |
| 2168 | _stdlib_enums = IntEnum, StrEnum, IntFlag |