Create a new Enum subclass that replaces a collection of global constants
(etype, name, module, filter, source=None, *, boundary=None)
| 2167 | raise TypeError('enum mismatch:\n %s' % '\n '.join(failed)) |
| 2168 | |
| 2169 | def _old_convert_(etype, name, module, filter, source=None, *, boundary=None): |
| 2170 | """ |
| 2171 | Create a new Enum subclass that replaces a collection of global constants |
| 2172 | """ |
| 2173 | # convert all constants from source (or module) that pass filter() to |
| 2174 | # a new Enum called name, and export the enum and its members back to |
| 2175 | # module; |
| 2176 | # also, replace the __reduce_ex__ method so unpickling works in |
| 2177 | # previous Python versions |
| 2178 | module_globals = sys.modules[module].__dict__ |
| 2179 | if source: |
| 2180 | source = source.__dict__ |
| 2181 | else: |
| 2182 | source = module_globals |
| 2183 | # _value2member_map_ is populated in the same order every time |
| 2184 | # for a consistent reverse mapping of number to name when there |
| 2185 | # are multiple names for the same number. |
| 2186 | members = [ |
| 2187 | (name, value) |
| 2188 | for name, value in source.items() |
| 2189 | if filter(name)] |
| 2190 | try: |
| 2191 | # sort by value |
| 2192 | members.sort(key=lambda t: (t[1], t[0])) |
| 2193 | except TypeError: |
| 2194 | # unless some values aren't comparable, in which case sort by name |
| 2195 | members.sort(key=lambda t: t[0]) |
| 2196 | cls = etype(name, members, module=module, boundary=boundary or KEEP) |
| 2197 | return cls |