I needed to create an enum to represent the ISO country codes. The country code data comes from a json file which can be obtained from: https://github.com/lukes/ISO-3166-Cou
How about this?
data = json.load(open('slim-2.json'))
CountryCode = enum.Enum('CountryCode', [
(x['alpha-2'], int(x['country-code'])) for x in data
])
CountryCode._names = {x['alpha-2']: x['name'] for x in data}
CountryCode.__str__ = lambda self: self._names[self.name]
CountryCode.choices = lambda: ((e.value, e.name) for e in CountryCode)
[...data[i]... for i in range(len(data))]
with [...x... for x in data]
; You can itearte sequence (list, data
in the code) without using indexes.CountryCode.attr = ...
consistently; instead of mixing CountryCode.attr = ...
and setattr(CountryCode, 'attr', ...)
.